Advertisement
Guest User

Untitled

a guest
Jun 20th, 2013
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. [1] put this as a private class within whatever activity or fragment you use for your ListView
  2. // based on http://stackoverflow.com/questions/8166497/custom-adapter-for-list-view
  3. private class ListAdapter extends BaseAdapter {
  4. public ListAdapter(Context context) {
  5. super(context);
  6. }
  7. @Override
  8. public View getView(int position, View convertView, ViewGroup parent) {
  9. View v = convertView;
  10.  
  11. if (v == null) {
  12. LayoutInflater vi = LayoutInflater.from(getContext());
  13. v = vi.inflate(R.layout.list_item, null);
  14. // see [2] for list_item
  15. }
  16.  
  17. // at this point you acquire whatever data you need at [position]
  18. // from whatever database or array or whatever you have
  19. // so if it's title and image URI,
  20. String title = ...;
  21. URI imageUri = ...;
  22.  
  23. TextView titleView = (TextView)v.findViewById(R.id.title);
  24. titleView.setText(title);
  25.  
  26. ImageView thumbnailView = (ImageView)v.findViewById(R.id.image);
  27. thumbnailView.setImageUri(imageUri);
  28.  
  29. return v;
  30. }
  31.  
  32. [2] res/layout/list_item.xml
  33.  
  34. <?xml version="1.0" encoding="utf-8"?>
  35. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  36. orientation="horizontal"
  37. android:layout_width="match_parent"
  38. android:layout_height="@dimen/list_item_height"
  39. android:background="@drawable/card_style_background">
  40. <ImageView android:id="@+id/image"
  41. android:layout_width="wrap_content"
  42. android:layout_height="match_parent"/>
  43. <TextView android:id="@+id/title"
  44. android:layout_width="match_parent"
  45. android:layout_height="match_parent"/>
  46. </LinearLayout>
  47.  
  48. [3] res/drawable/card_style_background.xml
  49. <?xml version="1.0" encoding="utf-8"?>
  50. <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
  51. <item>
  52. <shape android:shape="rectangle" >
  53. <solid android:color="@color/card_shadow" />
  54.  
  55. <corners android:radius="1.0dp" />
  56. </shape>
  57. </item>
  58. <item android:bottom="1dp">
  59. <shape android:shape="rectangle" >
  60. <solid android:color="@android:color/white" />
  61.  
  62. <stroke
  63. android:width="1.0px"
  64. android:color="@color/card_border" />
  65.  
  66. <corners android:radius="1.0dp" />
  67. </shape>
  68. </item>
  69. </layer-list>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement