Android ListView Item shows a number of different layouts

Source: Internet
Author: User


1. This blog post is not a point of knowledge. All that is used is the way in which the system has been provided to us. This is a recently used, feel very practical, hereby contribute.


First you need to define, the number of view to show in the ListView, and the layout type of the view


Private final int type_1 = 0; Type 1
Private final int type_2 = 1; Type 2


Private final int view_type = 2; Total number of layouts


@Override
public int Getitemviewtype (int position) {


if (position = = 0 | | position = = weeklycount + 1 | | position = weeklycount + Tastecount + 2)
return type_2;
Else
return type_1;
}


public int Getviewtypecount () {
return view_type;
}



/-------------------------------------------------------------------------------------******------------------- ------------------------------------------------/



@Override
Public View GetView (final int position, View Convertview, ViewGroup parent) {
Viewholder viewholder = null;
ViewHolder2 viewholderf = null;
int type = Getitemviewtype (position);
if (Convertview = = null) {
Switch (type) {//initialization of resources used in each layout
Case Type_1:
Viewholder = new Viewholder ();
Convertview = view.inflate (context, r.layout.item_ranking_user_top, null);
Viewholder.txtview_votesize = (TextView) Convertview.findviewbyid (r.id.txtview_votesize);
Viewholder.txtview_fontnumber = (TextView) Convertview.findviewbyid (R.id.txtview_fontnumber);
Viewholder.username = (TextView) Convertview.findviewbyid (r.id.txtview_username);
Viewholder.imgbtn_attention = (ImageButton) Convertview.findviewbyid (r.id.imgbtn_attention);
Viewholder.imgcrown = (ImageView) Convertview.findviewbyid (R.id.img_crown);
Viewholder.iv_userheade = (circularimage) Convertview.findviewbyid (R.id.img_userheader);
Viewholder.cellcontainer = (relativelayout) Convertview.findviewbyid (R.id.fragment_container);
Convertview.settag (Viewholder);
Break
Case type_2:
Viewholderf = new ViewHolder2 ();
Convertview = view.inflate (context, r.layout.item_ranking_title, null);
Viewholderf.txtview_catagory = (TextView) Convertview.findviewbyid (r.id.txtview_catagory);
Viewholderf.imgview_rankingtitle = (ImageView) Convertview.findviewbyid (r.id.imgview_rankingtitle);
Convertview.settag (Viewholderf);
Break
}


} else {//cache with complex

Switch (type) {
Case Type_1:
Viewholder = (Viewholder) convertview.gettag ();
Break
Case type_2:
Viewholderf = (ViewHolder2) convertview.gettag ();
Break
}
}


Switch (type) {
Case Type_1:

Break

Case type_2:

Break ///... Assigning content to each layout needs to be displayed


The others, however, have been described, and have not been written so that it is cumbersome to show a different layout in a ListView. Just use the other way. has not written himself, this project is used, so I found a bit of information to see a bit.


The real in the following project




1 Project background is that there is a leaderboard, three different objects. Weekly ranking, heat ranking, the most popular ranking. Because the object is different, so it is very troublesome to deal with, a bunch of judgment statements. Next Monday, need to review code, otherwise, will be approved OH.

Here the title background is a veiw, category show is a category


Speaking of this, just put, this simple algorithm to say, how to calculate, the title in the Listvew in the specific position.



Here is almost, suddenly found Getveiwcount () and Getviewtype () with the title of this simple location algorithm, use up, less, a lot of trouble. It was a smooth solution to this classification problem.
















Android ListView Item shows a number of different layouts

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.