Android ListView with Custom Adapter and ViewHolder
In this Android list view sample, it demonstrates list view using custom array adapter with view holder. ViewHolder makes the list view to load faster by caching the views. If it look up the view every time by calling the findViewById()
, it will be very slow.
activity_main.xml, layout file for the main activity.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@+id/item_list" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>
list_item.xml, layout file for row items in the list view.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/row_item" android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center"/> </LinearLayout>
MainActivity.java, in this main activity’s onCreate method will initialize the list view adapter.
import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.Toast; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Initializing list view with the custom adapter ArrayList <Item> itemList = new ArrayList<Item>(); ItemArrayAdapter itemArrayAdapter = new ItemArrayAdapter(this, R.layout.list_item, itemList); listView = (ListView) findViewById(R.id.item_list); listView.setAdapter(itemArrayAdapter); // Populating list items for(int i=0; i<100; i++) { itemList.add(new Item("Item " + i)); } // Set up list item onclick listener setUpListItemClickListener(); } private void setUpListItemClickListener() { listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(getBaseContext(), "item " + position + " clicked", Toast.LENGTH_SHORT).show(); } }); } }
Item.java, a very simple item class for creating items in the list view.
public class Item { private String name; public Item(String n) { name = n; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
ItemArrayAdapter.java, the custom list view adapter with viewholder pattern.
import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.TextView; import java.util.ArrayList; public class ItemArrayAdapter extends ArrayAdapter<Item> { private int listItemLayout; public ItemArrayAdapter(Context context, int layoutId, ArrayList<Item> itemList) { super(context, layoutId, itemList); listItemLayout = layoutId; } @Override public View getView(int position, View convertView, ViewGroup parent) { // Get the data item for this position Item item = getItem(position); // Check if an existing view is being reused, otherwise inflate the view ViewHolder viewHolder; if (convertView == null) { viewHolder = new ViewHolder(); LayoutInflater inflater = LayoutInflater.from(getContext()); convertView = inflater.inflate(listItemLayout, parent, false); viewHolder.item = (TextView) convertView.findViewById(R.id.row_item); convertView.setTag(viewHolder); // view lookup cache stored in tag } else { viewHolder = (ViewHolder) convertView.getTag(); } // Populate the data into the template view using the data object viewHolder.item.setText(item.getName()); // Return the completed view to render on screen return convertView; } // The ViewHolder, only one item for simplicity and demonstration purposes, you can put all the views inside a row of the list into this ViewHolder private static class ViewHolder { TextView item; } }
Search within Codexpedia
Custom Search
Search the entire web
Custom Search
Related Posts