मुझे पता है कि इसका पहले से ही उत्तर दिया गया है ... लेकिन मैं एक और पूरा उदाहरण देना चाहता था।
मेरे उदाहरण में, हमारी कस्टम सूची दृश्य को प्रदर्शित करने वाली सूची गतिविधि को विकल्प एक्टिविटी कहा जाता है, क्योंकि मेरी प्रोजेक्ट में यह गतिविधि मेरे ऐप को नियंत्रित करने के लिए सेट किए गए विभिन्न विकल्पों को प्रदर्शित करने जा रही है। दो सूची आइटम प्रकार हैं, एक सूची आइटम प्रकार में केवल एक टेक्स्ट व्यू है और दूसरी सूची आइटम प्रकार में केवल एक बटन है। आप प्रत्येक सूची आइटम प्रकार के अंदर अपने पसंदीदा विजेट डाल सकते हैं, लेकिन मैंने यह उदाहरण सरल रखा है।
getItemView विधि चेकों को देखने के लिए जो सूची आइटम होना चाहिए प्रकार 1 या टाइप 2 मेरी स्थिर ints मैं ऊपर परिभाषित, पहले 5 सूची आइटम सूची आइटम प्रकार 1, और पिछले 5 सूची हो जाएगा के अनुसार आइटम सूची आइटम प्रकार 2 होगा। इसलिए यदि आप इसे संकलित और चलाते हैं, तो आपके पास एक सूची दृश्य होगा जिसमें पांच आइटम होंगे जिनमें केवल एक बटन होगा, और फिर पांच आइटम जिनमें केवल टेक्स्ट व्यू होगा।
नीचे गतिविधि कोड, गतिविधि xml फ़ाइल, और प्रत्येक सूची आइटम प्रकार के लिए एक xml फ़ाइल है।
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>
स्रोत
2014-04-17 06:09:56