2.5.1 Implementing Multiple Layouts for ListView Items
Category Android Basic Tutorial
Introduction to This Section:
>
This section is the final part of the ListView subsection, bringing you the implementation of multiple layout items in ListView. What is a ListView item with multiple layouts? For example, in a chat list like QQ:
>
Suppose it is made with a ListView, then there would be two different items on a single ListView, right? One on the left and one on the right, hehe, this section will teach you how to implement multiple layouts for ListView!
1. Key Points Explanation:
>
Override the getItemViewType() method to correspond to which category the view is, as well as the getViewTypeCount() method to return the total number of categories! Then in getView, call getItemViewType to obtain the corresponding category and load the corresponding view!
2. Code Implementation:
>
Here, we will directly use the two layouts from the previous section, and then write another Adapter to override the key points mentioned:
MutiLayoutAdapter.java :
/**
* Created by Jay on 2015/9/23 0023.
*/
public class MutiLayoutAdapter extends BaseAdapter {
// Define two type identifiers
private static final int TYPE_BOOK = 0;
private static final int TYPE_APP = 1;
private Context mContext;
private ArrayList<Object> mData = null;
public MutiLayoutAdapter(Context mContext, ArrayList<Object> mData) {
this.mContext = mContext;
this.mData = mData;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
// Core of multiple layouts, determine the type through this
@Override
public int getItemViewType(int position) {
if (mData.get(position) instanceof App) {
return TYPE_APP;
} else if (mData.get(position) instanceof Book) {
return TYPE_BOOK;
} else {
return super.getItemViewType(position);
}
}
// Number of types
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);
ViewHolder1 holder1 = null;
ViewHolder2 holder2 = null;
if (convertView == null) {
switch (type) {
case TYPE_APP:
holder1 = new ViewHolder1();
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_one, parent, false);
holder1.img_icon = (ImageView) convertView.findViewById(R.id.img_icon);
holder1.txt_aname = (TextView) convertView.findViewById(R.id.txt_aname);
convertView.setTag(R.id.Tag_APP, holder1);
break;
case TYPE_BOOK:
holder2 = new ViewHolder2();
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_two, parent, false);
holder2.txt_bname = (TextView) convertView.findViewById(R.id.txt_bname);
holder2.txt_bauthor = (TextView) convertView.findViewById(R.id.txt_bauthor);
convertView.setTag(R.id.Tag_Book, holder2);
break;
}
} else {
switch (type) {
case TYPE_APP:
holder1 = (ViewHolder1) convertView.getTag(R.id.Tag_APP);
break;
case TYPE_BOOK:
holder2 = (ViewHolder2) convertView.getTag(R.id.Tag_Book);
break;
}
}
Object obj = mData.get(position);
// Set the values of the controls
switch (type) {
case TYPE_APP:
App app = (App) obj;
if (app != null) {
holder1.img_icon.setImageResource(app.getaIcon());
holder1.txt_aname.setText(app.getaName());
}
break;
case TYPE_BOOK:
Book book = (Book) obj;
if (book != null) {
holder2.txt_bname.setText(book.getbName());
holder2.txt_bauthor.setText(book.getbAuthor());
}
break;
}
return convertView;
}
// Two different ViewHolders
private static class ViewHolder1 {
ImageView img_icon;
TextView txt_aname;
}
private static class ViewHolder2 {
TextView txt_bname;
TextView txt_bauthor;
}
}
>
There is one thing to note here, convertView.setTag(R.id.Tag_APP, holder1); we usually just use setTag(Object) directly, this is an overloaded method of setTag, with the parameter being a unique key and mData.add(new Book("The First Line of Code", "Guo Lin")); break; case TYPE_APP: mData.add(new App(R.mipmap.iv_icon_baidu, "Baidu")); break; }
list_content = (ListView) findViewById(R.id.list_content); myAdapter = new MutiLayoutAdapter(MainActivity.this, mData); list_content.setAdapter(myAdapter); }
The above randomly generates 0 and 1, if 0, add a Book object to the collection, if 1, add an App object!
3. Code Download:
Summary of This Section:
>
Well, this section explained to everyone how to implement multiple layouts for ListView items, which is just the overriding of two methods, then make a judgment in getView() and set different layouts ~ the code is very simple ~
Let's put an end to the knowledge of ListView for now, of course, the knowledge of ListView is not limited to this, asynchronous loading, optimization and so on, we will learn in the advanced part ~ that's all, thank you ~
-1.0.1 Latest Android Basic Tutorial Catalog for 2015
-1.1 Background and System Architecture Analysis
-1.2 Development Environment Construction
-1.2.1 Developing Android APP with Eclipse + ADT + SDK
-1.2.2 Developing Android APP with Android Studio
-1.3 Solving SDK Update Issues
-1.4 Genymotion Emulator Installation
-1.5.1 Git Tutorial on Basic Operations of Local Repositories
-1.5.2 Git on Using GitHub to Set Up Remote Repositories
-1.6 How to Play with 9 (Nine Sister) Pictures
-1.7 Interface Prototype Design
-1.8 Project Related Analysis (Various Files, Resource Access)
-1.9 Android Program Signing and Packaging
-1.11 Decompiling APK to Get Code & Resources
-2.1 The Concept of View and ViewGroup
-2.2.1 LinearLayout (Linear Layout)
-2.2.2 RelativeLayout (Relative Layout)
-2.2.3 TableLayout (Table Layout)
-2.2.4 FrameLayout (Frame Layout)
-2.2.5 GridLayout (Grid Layout)
-2.2.6 AbsoluteLayout (Absolute Layout)
-2.3.1 TextView (Text Box) Detailed Explanation
-2.3.2 EditText (Input Box) Detailed Explanation
-2.3.3 Button (Button) and ImageButton (Image Button)
-2.3.5 RadioButton (Radio Button) & Checkbox (Checkbox)
-2.3.6 ToggleButton (Toggle Button) and Switch (Switch)
-2.3.7 ProgressBar (Progress Bar)
-2.3.9 RatingBar (Star Rating Bar)
-2.4.1 ScrollView (Scroll View)
-2.4.2 Date & Time Components (Part 1)
-2.4.3 Date & Time Components (Part 2)
-[2.4.4 Basic Explanation of Adapter](android-tutorial-adapter
4.4.2 Further Exploration of ContentProvider - Document Provider
5.2.1 Detailed Explanation of Fragment Example - Implementation of Bottom Navigation Bar (Method 1)
5.2.2 Detailed Explanation of Fragment Example - Implementation of Bottom Navigation Bar (Method 2)
5.2.3 Detailed Explanation of Fragment Example - Implementation of Bottom Navigation Bar (Method 3)
5.2.4 Detailed Explanation of Fragment Example - Bottom Navigation Bar + ViewPager for Page Swiping
6.2 Data Storage and Access - Saving User Preferences with SharedPreferences
7.1.1 Things to Learn in Android Network Programming and Study of Http Protocol
[8.
11.0 "2015 Latest Android Basic Tutorial" Concludes with a Celebration~
12.2 DrySister - A Girl Viewing App (First Edition) - 2. Parsing Backend Data
12.4 DrySister - A Girl Viewing App (First Edition) - 4. Adding Data Caching (Incorporating SQLite)