ListView that mixes simple_list_item_1 and simple_list_item_2

柔情痞子 提交于 2019-12-11 05:46:56

问题


I want a ListView which displays name and info about each item using simple_list_item_2, so it looks something like this:

SWITZERLAND
a country with many delicious cheeses

BRITAIN
famous for its clocktower

AUSTRALIA
in Australia, everything is upside down!

The text in caps is meant to represent the large black text in each ListView element and the lower caps text represents the grey, small type of that element.

However, I want the user to be able to add his own countries, so the first element would show up as

ADD NEW COUNTRY...

Without any small-type text. I can use simple_list_item_2 and simply set the small text to "", but this aligns the large text awkwardly. I want this special "add new country" element to look exactly like simple_list_item_1.

However, the decision to use simple_list_item_1 or simple_list_item_2 is made in the initialization of my adapter:

MyAdapter adapter = new MyAdapter(
            this,
            android.R.layout.simple_list_item_2, 
            myListOfListViewElements);

And then this decision is realized when I populate the ListView:

myListView.setAdapter(adapter);

So how can I use both kinds of simple_list_item in one ListView?


回答1:


You could always your "Add new country" as a header view by calling addHeaderView(View view) on the ListView before you call setAdapter(adapter) / setListAdapter(adapter). Attach an onClick listener to that header view to capture any clicks

That way your "Add new country" will show as the first entry in your listview




回答2:


For the specific case where you're dealing with just a single 'special' item, @dymmeh's answer is definitely a good and easy to implement approach. For the more generic case, where you need to deal with multiple different items that need not necessarily sit at the top of the ListView, you can refer to the following two methods in an adapter:

  • getItemViewType(int position)
  • getViewTypeCount()

Based on the return value of the first method, Android will feed you the desired convertView in the adapter's getView(...) method. A common approach is to simply return the layout ids as type (e.g. R.layout.row_layout) , but you can really use anything you like, provided the values uniquely identify a type.

After that, you'll need to do a little bit of extra work to inflate the correct layouts when constructing your ViewHolder/RowWrapper. For each view type, you'll need to pick the right layout if convertView == null. There will be at least one view type (which is the default case) and at most getViewTypeCount(). An example illustrating the idea can be found here.

More details on the view type methods:

  • getViewTypeCount and getItemViewType methods of ArrayAdapter

On a side note: I'd actually consider not making the 'add...' option part of the ListView, but rather position it on top of or above it so that it's conveniently accessible; i.e. no need to scroll back to the top first.



来源:https://stackoverflow.com/questions/13149458/listview-that-mixes-simple-list-item-1-and-simple-list-item-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!