Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [1] put this as a private class within whatever activity or fragment you use for your ListView
- // based on http://stackoverflow.com/questions/8166497/custom-adapter-for-list-view
- private class ListAdapter extends BaseAdapter {
- public ListAdapter(Context context) {
- super(context);
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- View v = convertView;
- if (v == null) {
- LayoutInflater vi = LayoutInflater.from(getContext());
- v = vi.inflate(R.layout.list_item, null);
- // see [2] for list_item
- }
- // at this point you acquire whatever data you need at [position]
- // from whatever database or array or whatever you have
- // so if it's title and image URI,
- String title = ...;
- URI imageUri = ...;
- TextView titleView = (TextView)v.findViewById(R.id.title);
- titleView.setText(title);
- ImageView thumbnailView = (ImageView)v.findViewById(R.id.image);
- thumbnailView.setImageUri(imageUri);
- return v;
- }
- [2] res/layout/list_item.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- orientation="horizontal"
- android:layout_width="match_parent"
- android:layout_height="@dimen/list_item_height"
- android:background="@drawable/card_style_background">
- <ImageView android:id="@+id/image"
- android:layout_width="wrap_content"
- android:layout_height="match_parent"/>
- <TextView android:id="@+id/title"
- android:layout_width="match_parent"
- android:layout_height="match_parent"/>
- </LinearLayout>
- [3] res/drawable/card_style_background.xml
- <?xml version="1.0" encoding="utf-8"?>
- <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
- <item>
- <shape android:shape="rectangle" >
- <solid android:color="@color/card_shadow" />
- <corners android:radius="1.0dp" />
- </shape>
- </item>
- <item android:bottom="1dp">
- <shape android:shape="rectangle" >
- <solid android:color="@android:color/white" />
- <stroke
- android:width="1.0px"
- android:color="@color/card_border" />
- <corners android:radius="1.0dp" />
- </shape>
- </item>
- </layer-list>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement