Android.widget.ListView
lastmod 2018-08-17created 2016-10-148 edits
A view that shows items in a vertically scrolling list. The items come from the ListAdapter associated with this view.
Custom ListView
To customize a ListView directly, go through the following steps.
- Write the Layout XML for one item (ROW).
- Write a struct (class) that holds the information for one item.
- Write an adapter class that extends ArrayAdapter or BaseAdapter, to attach to the ListView with
setAdapter(). Importantly, when extending BaseAdapter, you must at least override and implementgetItem(),getView(), andgetCount(). (Internally,getCount()is used to check whether items exist.) - To add a click event for items, use
setOnItemClickListener(). 1 - To improve performance, use a #ViewHolder. An example is shown below.
/** 하나의 어플리케이션 정보. */
public static class AppInfo {
/** 패키지명(Package name). */
public String pkgname;
/** 어플리케이션 명칭. */
public String name;
/** 어플리케이션 아이콘. */
public Bitmap icon;
}
/** 리스트뷰에 장착시키기 위한 어댑터(Adapter) 클래스. */
public class AppInfoAdapter extends ArrayAdapter<AppInfo> {
/** 어댑터의 getView() 성능을 높이기 위한 ViewHolder. */
private class ViewHolder {
/** 아이콘 이미지뷰. */
public ImageView iconview;
/** 어플리케이션 명칭. */
public TextView appname;
/** 어플리케이션 추가 설명. */
public TextView appinfo;
}
public AppInfoAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public AppInfoAdapter(Context context, int textViewResourceId, List<AppInfo> objects) {
super(context, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final AppInfo kCurrentItem = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(FavoriteAppPopup.this).inflate(R.layout.listitem_favoriteapp, null);
// Register view objects.
ViewHolder holder = new ViewHolder();
holder.iconview = (ImageView) convertView.findViewById(R.id.favoriteapp_listitem__icon_img);
holder.appname = (TextView) convertView.findViewById(R.id.favoriteapp_listitem__title_label);
holder.appinfo = (TextView) convertView.findViewById(R.id.favoriteapp_listitem__comment_label);
convertView.setTag(holder);
}
final ViewHolder kCurrentHolder = (ViewHolder) convertView.getTag();
if (kCurrentHolder == null) {
Log.d(FavoriteAppPopup.class.toString(), "kCurrentHolder is null.");
} else {
kCurrentHolder.iconview.setImageBitmap(kCurrentItem.icon);
kCurrentHolder.appname.setText(kCurrentItem.name);
kCurrentHolder.appinfo.setText(kCurrentItem.pkgname);
kCurrentHolder.appinfo.setVisibility(View.INVISIBLE);
}
// return super.getView(position, convertView, parent);
return convertView;
}
}
// ....
/** 리스트뷰를 설치한다. */
private void setupListView() {
_listview = (ListView) findViewById(R.id.favoriteapp_popup__apps_listview);
_lsitadapter = new AppInfoAdapter(this, R.layout.listitem_favoriteapp);
_lsitadapter.addAll(getLocalAppInfoList());
_lsitadapter.addAll(requestAppInfoList());
_listview.setAdapter(_lsitadapter);
_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final AppInfo kCurrentItem = _lsitadapter.getItem(position);
// 어플이 존재한다면 실행하고, 그렇지 않다면 마켓으로 이동한다.
if (kCurrentItem != null) {
IntentHelper.runApplicationOrJumpMarket(FavoriteAppPopup.this, kCurrentItem.pkgname);
}
}
});
_lsitadapter.notifyDataSetChanged();
}
After adding (add) user input to the ArrayAdapter, you must call the notifyDataSetChanged() method, which tells it to check for changes.
A ListView that displays one string
A ListView that works much like a combo box.
ListView list_view = (ListView) findViewById(kOptionListViewId);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
adapter.add("Test1");
adapter.add("Test2");
adapter.add("Test3");
list_view.setAdapter(adapter);
In the source above, android.R.layout.simple_list_item_1, passed to the constructor of the adapter (ArrayAdapter), is a predefined layout for ListView. The following predefined layouts exist.
-
android.R.layout.simple_list_item_1; -
android.R.layout.simple_list_item_2; -
android.R.layout.simple_list_item_activated_1; -
android.R.layout.simple_list_item_activated_2; -
android.R.layout.simple_list_item_checked; -
android.R.layout.simple_list_item_multiple_choice; -
android.R.layout.simple_list_item_single_choice;
A ListView that displays two strings
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
HashMap<String,String> item;
// 이부분은 예를 들기위하여 풀어 썼으며 리소스를 이용하기 위해서는 for 문 등을 이용하면 된다.
item = new HashMap<String,String>();
item.put("item 1", "Title 1");
item.put("item 2", "description 1");
list.add(item);
item = new HashMap<String,String>();
item.put("item 1","Title 2");
item.put("item 2","another description here");
list.add(item);
simpleAdapter = new SimpleAdapter(this, list, android.R.layout.simple_list_item_2, new String[] {"item 1","item 2"}, new int[] {android.R.id.text1, android.R.id.text2});
setListAdapter(simpleAdapter);
How to remove the Bounce effect from a list view or scroll view
How to remove the bounce effect (the list springing back slightly). In Android, it refers to the effect where the Edge at the end glows when you reach the end of the view.
ListView speed issues (Making ListView Scrolling Smooth)
ViewHolder
A ViewHolder is, as the name says, an object that keeps views as if plugged into a holder. When drawing each Row, findViewById is called to change the properties of the widgets inside it, and the ViewHolder is used to reduce this high cost.
See also
Favorite site
- Android ListView optimization/performance improvement
- Kunny's Android: Leave displaying large amounts of data to me! ListView 2
- [Recommended] Android ListView, ListActivity and ListFragment - Tutorial 3
- [Recommended] http://atin.tistory.com/379
- http://myelf.egloos.com/2723978
- http://www.androidpub.com/41674
- Duplicate getView calls on the Adapter when implementing an Android ListView
- Alphabetic FastScroll ListView - similar to Contacts (how to enable FastScroll and how to show the first letter of the alphabet while FastScrolling)
- Making a header in a list
- Stackoverflow - How to use fast scroll in android?
- How to add Alphabetical section indexer with first alphabet at right vertical in Listview android
References
-
http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html ↩
-
Android_listview_howtouse.pdf ↩
-
Android_listview_list-activity_and_list-fragment_tutorial.pdf ↩