In order for certain list items to specify a different view, it is necessary to create, in fact, the markup itself for each type of item - how they will look and in the adapter to override two methods:
getItemViewType(position)
- contains a condition by which you can determine what kind of item to display in the current position. Returns the ID of this item.
getViewTypeCount()
- returns the number of items of various types.
Create a simple list with two types of items: item_normal.xml
- the general view and item_big.xml
- the view with a larger font (the font size is set directly in the markup). I will not invent the markings themselves - they have to implement what items for each type will look like.
Custom adapter (the simplest example based on an ArrayAdapter
is only the parts that are important for understanding):
public class CustomAdapter extends ArrayAdapter { private LayoutInflater mInflater; private static final int TYPE_1 = 0; // ΠΈΠ΄Π΅Π½ΡΠΈΡΠΈΠΊΠ°ΡΠΎΡ Π°ΠΉΡΠ΅ΠΌΠ° ΠΏΠ΅ΡΠ²ΠΎΠ³ΠΎ ΡΠΈΠΏΠ° private static final int TYPE_2 = 1; // ΠΈΠ΄Π΅Π½ΡΠΈΡΠΈΠΊΠ°ΡΠΎΡ Π°ΠΉΡΠ΅ΠΌΠ° Π²ΡΠΎΡΠΎΠ³ΠΎ ΡΠΈΠΏΠ° private ArrayList mItems; public CustomAdapter(Context context, ArrayList items) { super(context, items); mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); mItems = items; } public View getView(int position, View convertView, ViewGroup parent) { // ΠΏΠΎΠ»ΡΡΠ°Π΅ΠΌ ΠΈΠ΄Π΅Π½ΡΠΈΡΠΈΠΊΠ°ΡΠΎΡ Π²ΠΈΠ΄Π° Π°ΠΉΡΠ΅ΠΌΠ° Π² ΡΠ΅ΠΊΡΡΠ΅ΠΉ ΠΏΠΎΠ·ΠΈΡΠΈΠΈ int type = getItemViewType(position); View v = convertView; //ΠΈΠ½ΡΠ»Π΅ΠΉΡΠΈΠΌ ΡΠ°Π·ΠΌΠ΅ΡΠΊΡ Π°ΠΉΡΠ΅ΠΌΠ° Π΄Π»Ρ ΡΠ΅ΠΊΡΡΠ΅ΠΉ ΠΏΠΎΠ·ΠΈΡΠΈΠΈ switch (type) { case TYPE_1: v = mInflater.inflate(R.layout.item_normal, null); break; case TYPE_2: v = mInflater.inflate(R.layout.item_big, null); break; default: } // ΠΡΠ»ΠΈ Π°ΠΉΡΠ΅ΠΌΡ ΡΠ°Π·Π½ΡΡ
Π²ΠΈΠ΄ΠΎΠ² ΡΠΎΠ΄Π΅ΡΠΆΠ°Ρ ΡΠ°Π·Π½ΡΠ΅ Π΄Π°Π½Π½ΡΠ΅ Π΄Π»Ρ ΠΎΡΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ // ΡΠΎ ΡΠ°Π·Π΄Π΅Π»ΡΠ΅ΠΌ Π±ΠΈΠ½Π΄ΠΈΠ½Π³: // // switch (type) // { // case TYPE_1: // Π±ΠΈΠ½Π΄ΠΈΠ½Π³ Π΄Π°Π½Π½ΡΡ
Π² Π°ΠΉΡΠ΅ΠΌ ΠΏΠ΅ΡΠ²ΠΎΠ³ΠΎ Π²ΠΈΠ΄Π°. // // break; // case TYPE_2: // Π±ΠΈΠ½Π΄ΠΈΠ½Π³ Π΄Π°Π½Π½ΡΡ
Π² Π°ΠΉΡΠ΅ΠΌ Π²ΡΠΎΡΠΎΠ³ΠΎ Π²ΠΈΠ΄Π°. // // break; // } // // Π’Π°ΠΊ ΠΊΠ°ΠΊ Π² Π²Π°ΡΠ΅ΠΌ ΡΠ»ΡΡΠ°Π΅ Π΄Π°Π½Π½ΡΠ΅ Π² Π°ΠΉΡΠ΅ΠΌΠ°Ρ
Π½Π΅ ΠΌΠ΅Π½ΡΡΡΡΡ, ΡΠΎ Π΄Π»Ρ ΠΎΠ±ΠΎΠΈΡ
// Π±ΠΈΠ½Π΄ΠΈΠ½Π³ ΠΎΠ΄ΠΈΠ½Π°ΠΊΠΎΠ²ΡΠΉ ΠΈ Π΅Π³ΠΎ Π½Π΅ Π½ΡΠΆΠ½ΠΎ ΡΠ°Π·Π΄Π΅Π»ΡΡΡ. String text = mItems.get(position); // ΠΈΠ·Π²Π»Π΅ΠΊΠ°Π΅ΠΌ Π΄Π°Π½Π½ΡΠ΅ TextView tv = (TextView) v.findViewById(R.id.text); tv.setText(text); // ΡΠ°Π·ΠΌΠ΅ΡΠ°Π΅ΠΌ Π² Π°ΠΉΡΠ΅ΠΌ return v; } @Override public int getItemViewType(int position) { // ΠΠ΄Π΅ΡΡ Π½ΡΠΆΠ½ΠΎ ΠΎΠΏΡΠ΅Π΄Π΅Π»ΠΈΡΡ Π²ΡΠΎΡΡΡ Π²ΠΈΠ΄ΠΈΠΌΡΡ ΠΏΠΎΠ·ΠΈΡΠΈΡ // ΠΈ Π²Π΅ΡΠ½ΡΡΡ ΠΈΠ΄Π΅Π½ΡΠΈΡΠΈΠΊΠ°ΡΠΎΡ Π°ΠΉΡΠ΅ΠΌΠ° Π²ΡΠΎΡΠΎΠ³ΠΎ ΡΠΈΠΏΠ° // ΠΈΠ½Π°ΡΠ΅ - Π°ΠΉΡΠ΅ΠΌΠ° ΠΏΠ΅ΡΠ²ΠΎΠ³ΠΎ ΡΠΈΠΏΠ° // ΠΠ°Π΄ΠΎ ΠΏΠΎΠ΄ΡΠΌΠ°ΡΡ. return (twoPosition) ? TYPE_2 : TYPE_1; } @Override public int getViewTypeCount() { return 2; // Π΄Π²Π° ΡΠ°Π·Π»ΠΈΡΠ½ΡΡ
Π²ΠΈΠ΄Π° Π°ΠΉΡΠ΅ΠΌΠΎΠ² } }
Since in your case, the appearance of different items is no different than anything other than the color and size of the font, you specify all these differences directly in the markup - you do not need to bind the data separately.
In more complex cases, you can use any independent data for different types of items, which can contain completely different markup and share their content through a switch - case
in a commented block.
In a real adapter it is better to use holders and other optimizations.