Adding different types of items as a list

Is there a good tutorial or link that shows how to add different items to the list?
For example, one with two text lines and a check box , the other that you just click on and something will appear. All I have now is each element of the list, this is the same two-line textual representation and a checkbox ...
Or , if there is a way to add 1 row at a time with a different layout through R.layout.xxx ?

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mRoot = inflater.inflate(R.layout.frag_settings, container, false); mItems = getResources().getStringArray(R.array.setting_items); mItemDescription = getResources().getStringArray(R.array.setting_item_descriptions); mItemListView = (ListView) mRoot.findViewById(R.id.lvMainListView); ArrayAdapter<String> lvRowTitle = new ArrayAdapter<String>(getActivity(), R.layout.setting_twolinetext_checkbox, R.id.tvRowTitle, mItems); mItemListView.setAdapter(lvRowTitle); ArrayAdapter<String> lvRowDesc = new ArrayAdapter<String>(getActivity(), R.layout.setting_twolinetext_checkbox, R.id.tvRowDesc, mItemDescription); mItemListView.setAdapter(lvRowDesc); return mRoot; 
+4
source share
3 answers

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> 
+4
source

You have two options to do this:

  • Create a type and check its type and return a view related to that type.
  • BaseAdapter has two methods for checking the various elements in it, getItemViewType(int position) and getViewTypeCount() . Do your things there.

Check out this tutorial:

Multiple row listview

+2
source

You must create your own class that extends the BaseAdapter . I recommend watching ListView World , it will help you understand everything you need to know about working with ListView.

+1
source

Source: https://habr.com/ru/post/1490741/


All Articles