In my example, the list activity displaying our own view of the list is called OptionsActivity, because in my project this action will display various parameters that my user can configure to control my application. There are two types of list items, one type of list item has only a TextView, and the second type of list item has only a button. You can put any widgets that you like inside each type of list item, but I just saved this example.
The getItemView method checks which list items should be of type 1 or type 2. According to my static ints, which I defined at the top, the first 5 items of the list will be items of list 1, and the last 5 items of the list will be the type of items in list 2. Therefore, if you have it compile and run, you will have a ListView, in which there are five elements that contain only the button, and then five elements that contain only the TextView.
Below is the activity code, the xml activity file and the xml file for each type of list.
OptionsActivity.java:
public class OptionsActivity extends ListActivity { private static final int LIST_ITEM_TYPE_1 = 0; private static final int LIST_ITEM_TYPE_2 = 1; private static final int LIST_ITEM_TYPE_COUNT = 2; private static final int LIST_ITEM_COUNT = 10; // The first five list items will be list item type 1 // and the last five will be list item type 2 private static final int LIST_ITEM_TYPE_1_COUNT = 5; private MyCustomAdapter mAdapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mAdapter = new MyCustomAdapter(); for (int i = 0; i < LIST_ITEM_COUNT; i++) { if (i < LIST_ITEM_TYPE_1_COUNT) mAdapter.addItem("item type 1"); else mAdapter.addItem("item type 2"); } setListAdapter(mAdapter); } private class MyCustomAdapter extends BaseAdapter { private ArrayList<String> mData = new ArrayList<String>(); private LayoutInflater mInflater; public MyCustomAdapter() { mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); } public void addItem(final String item) { mData.add(item); notifyDataSetChanged(); } @Override public int getItemViewType(int position) { if(position < LIST_ITEM_TYPE_1_COUNT) return LIST_ITEM_TYPE_1; else return LIST_ITEM_TYPE_2; } @Override public int getViewTypeCount() { return LIST_ITEM_TYPE_COUNT; } @Override public int getCount() { return mData.size(); } @Override public String getItem(int position) { return mData.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; int type = getItemViewType(position); if (convertView == null) { holder = new ViewHolder(); switch(type) { case LIST_ITEM_TYPE_1: convertView = mInflater.inflate(R.layout.list_item_type1, null); holder.textView = (TextView)convertView.findViewById(R.id.list_item_type1_text_view); break; case LIST_ITEM_TYPE_2: convertView = mInflater.inflate(R.layout.list_item_type2, null); holder.textView = (TextView)convertView.findViewById(R.id.list_item_type2_button); break; } convertView.setTag(holder); } else { holder = (ViewHolder)convertView.getTag(); } holder.textView.setText(mData.get(position)); return convertView; } } public static class ViewHolder { public TextView textView; } }
activity_options.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/optionsList" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout>
list_item_type_1.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/list_item_type1_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/list_item_type1_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Text goes here" /> </LinearLayout>
list_item_type2.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/list_item_type2_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/list_item_type2_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button text goes here" /> </LinearLayout>