Display multiple views in the android listview

Source: Internet
Author: User

1. Scenario

When using listview, we often encounter scenarios where multiple views need to be displayed in a listview. For example, in the chat interface of the chat software, the messages sent by ourselves are displayed as their portraits on the left, messages sent by others are displayed on the right.

2. Common listview Methods
2.1 commonly used adapters of listview include arrayadapter, simpleadapter, and cursoradapter. Detailed usage is provided in the apidemo project of the android SDK sample.

2.2 When we need to display complex data and resources in the listview view and process complex events, we can inherit the baseadapter and rewrite the methods in the baseadapter to achieve our goal, it is mainly the getview (INT position, view convertview, viewgroup parent) method. Detailed usage is provided in the apidemo project of the android SDK sample.

2.3 when we need to display multiple views in the listview, we still use the method that inherits the baseadapter. Note the getitemviewtype (INT position) and getviewtypecount () methods.

3. Implementation of multiple views in listview
3.1 Implementation steps
3.1.1 define an attempt type constant
We have defined three types:

/*** Sent message */Private Static final int type_send = 0;/*** received message */Private Static final int type_receive = type_send + 1; /*** image */Private Static final int type_pic = type_receive + 1;

3.1.2 override the getitemviewtype (INT position) and getviewtypecount () Methods

public int getItemViewType(int position) {int type = super.getItemViewType(position);try{type = Integer.parseInt(data.get(position).get("type"));} catch (Exception e){e.printStackTrace();}System.out.println("getItemViewType::" + position + " is " + type);return type;}public int getViewTypeCount() {System.out.println("getViewTypeCount is " + 3);return 3;}

The custom view type of the corresponding item is returned by getitemviewtype, and the total number of view types returned by getviewtypecount.
Note: The value returned by getviewtypecount must be greater than the constant value of the view type. In an array, getviewtypecount returns the length of the array, and getitemviewtype returns (a constant defined in 3.1.1) is the subscript of the array.
3.1.2 override the getview Method

public View getView(int position, View convertView, ViewGroup parent) {System.out.println("getView::" + position);int type = TYPE_SEND;try{type = Integer.parseInt(data.get(position).get("type"));} catch (Exception e){e.printStackTrace();}ViewHolder holder = null;if (convertView == null){System.out.println("getView::convertView is null");holder = new ViewHolder();switch (type){case TYPE_SEND:convertView = View.inflate(getBaseContext(),R.layout.listitem_send, null);holder.text = (TextView) convertView.findViewById(R.id.message);break;case TYPE_RECEIVE:convertView = View.inflate(getBaseContext(),R.layout.listitem_receive, null);holder.text = (TextView) convertView.findViewById(R.id.message);break;case TYPE_PIC:convertView = new ImageView(getBaseContext());((ImageView) convertView).setImageResource(R.drawable.icon);break;}convertView.setTag(holder);}else{System.out.println("getView::convertView not null");holder = (ViewHolder) convertView.getTag();}if (type != TYPE_PIC){String msg = data.get(position).get("content");holder.text.setText(msg);}return convertView;}

This part is similar to 2.2. The difference is that when convertview is empty, the corresponding view layout needs to be initialized Based on The View type corresponding to the current item data. Other methods such as getcount and getitem are rewritten as follows.

3.2 Process Analysis

In the parent class abslistview of listview, there is a variable recyclebin mrecycler, which is used to store the view corresponding to a display item layout. Actually stored in arraylist <View> []. The length of this array is the return value of getviewtypecount. Recyclebin
Is an internal class of abslistview.
 
When listview executes the setadapter method, mrecycler resets and the getviewtypecount method is called.

When a listview item is to be displayed, the getitemviewtype method is called and the cached view is obtained through mrecycler search based on the returned value. This is also the reason why the returned value of getviewtypecount is greater than the defined constant value of the view type. Otherwise, an array out-of-bounds exception occurs.
 
Call the getview method. The cached view is passed to the convertview parameter of the getview method (For details, refer to the obtainview method of the abslistview ).
When the passed convertview parameter is null, You need to initialize the Layout Based on The View type of the item.

Finally, fill the display item with data.

Download source code:

Http://download.csdn.net/detail/clarketang/3694460

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.