Android implementation of the ListView adapter multiple layout methods

Source: Internet
Author: User

These two days in the implementation of a module of the leaderboard function, look at the UI and transduction, feel a bit depressed, because usually when using the ListView, children are only one way of layout, and this time there are two kinds. So specialized research, found that rewrite adapter getitemviewtype () and Getviewtypecount () method can achieve a variety of layout, the implementation of their own to share the next.
steps:
1. rewrite Getviewtypecount () – returns how many different layouts you have;
2. rewrite Getitemviewtype (int) – Returns the view type ID by position;
3. create the correct convertview in GetView based on the type of view item.

The adapter I use for the ListView are:

 PackageCom.eebbk.syncenglish.view;ImportJava.util.List;ImportCom.eebbk.bbksn.BBKSn;ImportCom.eebbk.englishwords.bean.UserCrownRankVo;ImportCom.eebbk.englishwords.data.config.pojo.UserInfo;ImportCom.eebbk.open.common.util.ListUtils;ImportCOM.EEBBK.SYNCENGLISH.R;ImportCom.eebbk.syncenglish.util.Utils;ImportCom.nostra13.universalimageloader.core.DisplayImageOptions;ImportCom.nostra13.universalimageloader.core.ImageLoader;ImportAndroid.annotation.SuppressLint;ImportAndroid.content.Context;ImportAndroid.graphics.Bitmap;ImportAndroid.text.TextUtils;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.webkit.WebView.FindListener;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.ImageView;ImportAndroid.widget.RelativeLayout;ImportAndroid.widget.TextView; Public  class userranklistadapter extends baseadapter {    Private Static Final intRant_num_0 =0;Private Static Final intRant_num_1 =1;Private Static Final intRant_num_2 =2;Private Static Final intRant_num_3 =3;Private Static Final intRant_num_4 =4;Private Static Final intRant_num_5 =5;Private Static Final intRant_num_6 =6;Private Static Final intRant_num_7 =7;Private Static Final intRant_num_8 =8;Private Static Final intRant_num_9 =9;/** top three times * *    Private Static Final intType_top =0;/** Other rankings * /    Private Static Final intType_other =1;PrivateContext Mcontext =NULL;PrivateList<usercrownrankvo> musercrownranklist =NULL;PrivateLayoutinflater Minflater =NULL;PrivateString Mmachineid =NULL;PrivateDisplayimageoptions options; Public void setusercrownranklist(list<usercrownrankvo> usercrownranklist) { This. musercrownranklist = usercrownranklist;    Notifydatasetchanged (); } Public Userranklistadapter(context context, list<usercrownrankvo> usercrownranklist) { This. Mcontext = Context; This. musercrownranklist = usercrownranklist;        Minflater = Layoutinflater.from (Mcontext);        Mmachineid = BBKSN.GETBBKSN (); Options =NewDisplayimageoptions.builder (). Cacheinmemory (true). Cacheondisc (true). Cacheondisk (true). Considerexifparams (true). Bitmapconfig (Bitmap.Config.ARGB_8888). showimageonloading (R.drawable.default_head). build (); }@Override     Public int Getitemviewtype(intPosition) {if(Position <3) {returnType_top; }Else{returnType_other; }    }@Override     Public int Getviewtypecount() {return 2; }@Override     Public int GetCount() {returnListutils.isempty (musercrownranklist)?0: Musercrownranklist.size (); }@Override     PublicObjectGetItem(intPosition) {returnListutils.isempty (musercrownranklist)?NULL: Musercrownranklist.get (position); }@Override     Public Long Getitemid(intPosition) {returnPosition }@SuppressLint("Inflateparams")@Override     PublicViewGetView(intPosition, View Convertview, ViewGroup parent) {Viewholder holder;intViewType = Getitemviewtype (position);if(Convertview = =NULL) {holder =NewViewholder ();if(ViewType = = type_top) {Convertview = Minflater.inflate (R.layout.layout_rank_list_top_item,NULL); }Else{Convertview = Minflater.inflate (R.layout.layout_rank_list_item,NULL);            } Holder.itembgiv = (ImageView) Convertview.findviewbyid (r.id.item_bg_id);            Holder.ranklayout = (relativelayout) Convertview.findviewbyid (r.id.userrank_rank_layout_id);            Holder.nicknametv = (TextView) Convertview.findviewbyid (r.id.user_nickname_id);            HOLDER.BEANNUMTV = (TextView) Convertview.findviewbyid (r.id.user_bean_num_id);            HOLDER.CROWNNUMTV = (TextView) Convertview.findviewbyid (r.id.user_crown_num_id);            Holder.rankiv1 = (ImageView) Convertview.findviewbyid (r.id.rank_iv_id_1);            Holder.rankiv2 = (ImageView) Convertview.findviewbyid (r.id.rank_iv_id_2);            Holder.headiconiv = (ImageView) Convertview.findviewbyid (r.id.user_head_icon_id);        Convertview.settag (holder); }Else{holder = (Viewholder) convertview.gettag (); }if(Listutils.isempty (musercrownranklist) | | position >= musercrownranklist.size ()) {returnConvertview; } usercrownrankvo Usercrownrankvo = Musercrownranklist.get (position);intRanknum = Usercrownrankvo.getranknum (); String nameStr = Usercrownrankvo.getuseralias ();if(! Textutils.isempty (Mmachineid) && Usercrownrankvo.getmachineid (). equal (Mmachineid)) {UserInfo UserInfo = Utils.getuserinfo (Mcontext);if(UserInfo! =NULL&&!            Textutils.isempty (Userinfo.mnickname)) {nameStr = Userinfo.mnickname;        }} Setuserheadicon (Holder.headiconiv,usercrownrankvo.getmachineid (), usercrownrankvo.getheadportrait ());        Holder.nickNameTv.setText (NAMESTR); Holder.beanNumTv.setText (Usercrownrankvo.getdiligencebeannum () +""); Holder.crownNumTv.setText (Usercrownrankvo.getcrown () +"");if(ViewType = = Type_other)            {holder.rankLayout.setVisibility (view.visible);        Setuserrankpic (Holder.rankiv1,holder.rankiv2,ranknum); }Else{Setitembackground (holder.itembgiv,ranknum);if(Ranknum = =1) {Holder.nickNameTv.setTextColor (0XFFFF05DD); Holder.crownNumTv.setTextColor (0XFFFF05DD); }Else if(Ranknum = =2) {Holder.nickNameTv.setTextColor (0xffa405ff); Holder.crownNumTv.setTextColor (0xffa405ff); }Else if(Ranknum = =3) {Holder.nickNameTv.setTextColor (0xff0da4fe); Holder.crownNumTv.setTextColor (0xff0da4fe); }        }returnConvertview; }Private void Setuserrankpic(ImageView Ranknumiv1,imageview RankNumIv2,intUserrank) {if(Userrank <Ten) {ranknumiv2.setvisibility (view.gone);intNumpicid = Getranknumpicid (Userrank);        Ranknumiv1.setbackgroundresource (NUMPICID); }Else{ranknumiv2.setvisibility (view.visible);intNUMPICID1 = Getranknumpicid (userrank/Ten);intNUMPICID2 = Getranknumpicid (userrank%Ten);            Ranknumiv1.setbackgroundresource (NUMPICID1);        Ranknumiv2.setbackgroundresource (NUMPICID2); }    }Private void Setuserheadicon(ImageView headiconiv,string userid,string headiconurl) {if(!        Textutils.isempty (Mmachineid) && userid.equal (Mmachineid)) {Setmyheadiconiv (Headiconiv); }Else{Setuserotherheadiconiv (Headiconiv,headiconurl); }    }Private void Setuserotherheadiconiv(ImageView Headiconiv, String headiconurl)    {imageloader.getinstance (). DisplayImage (Headiconurl, Headiconiv, Options); }Private void Setmyheadiconiv(ImageView Headiconiv) {UserInfo UserInfo = Utils.getuserinfo (Mcontext);if(UserInfo! =NULL) {Bitmap Headbitmap = utils.getuserheadpic (UserInfo);if(Headbitmap! =NULL) {Headiconiv.setimagebitmap (Utils.getbitmapwithbackground (Headbitmap)); }Else{Headiconiv.setimageresource (r.drawable.default_head); }        }Else{Headiconiv.setimageresource (r.drawable.default_head); }    }Private void Setitembackground(ImageView Itembgiv,intRank) {intRId = R.drawable.user_rank_item_no_other;Switch(rank) { Case 1: RId = r.drawable.user_rank_item_no_1; Break; Case 2: RId = r.drawable.user_rank_item_no_2; Break; Case 3: RId = R.drawable.user_rank_item_no_3; Break;default: Break;    } itembgiv.setbackgroundresource (RId);        } class Viewholder {ImageView itembgiv;        TextView Nicknametv;        TextView BEANNUMTV;        TextView CROWNNUMTV;        Relativelayout ranklayout;        ImageView RankIv1;        ImageView RankIv2;    ImageView Headiconiv; }Private int Getranknumpicid(intNUM) {intNumpicid =0;Switch(num) { CaseRant_num_0:numpicid = R.drawable.rank_0; Break; CaseRant_num_1:numpicid = R.drawable.rank_1; Break; CaseRant_num_2:numpicid = r.drawable.rank_2; Break; CaseRant_num_3:numpicid = R.drawable.rank_3; Break; CaseRant_num_4:numpicid = R.drawable.rank_4; Break; CaseRant_num_5:numpicid = R.drawable.rank_5; Break; CaseRant_num_6:numpicid = R.drawable.rank_6; Break; CaseRant_num_7:numpicid = r.drawable.rank_7; Break; CaseRant_num_8:numpicid = R.drawable.rank_8; Break; CaseRant_num_9:numpicid = R.drawable.rank_9; Break;default: Break; }returnNumpicid; }}

It is important to note that the type in Getitemviewtype () must be counted from 0, and let getviewtypecount>getitemviewtype, otherwise it will be used when the return of the wrong, this is my trip to the pit of one of the harvest. At first I wrote, Type_top = 1, Type_other = 2, and the result encountered an array out of bounds exception. So write Type_top = 0, Type_other = 1.

Final effect:

Write more superficial, welcome comments together to discuss!

Android implementation of the ListView adapter multiple layout methods

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.