Android ListView irregular items i.e. different item runs

Source: Internet
Author: User

In Android, the ListView item is a process that is constantly being polled for swap positions, so

That is the picture reference address http://www.cnblogs.com/mybkn/archive/2012/05/23/2514681.html Thank you.

In development, we encounter different requirements for each of the ListView in the possession. This time if you use the method of the previously customized baseadapter. When scrolling, the item layout is out of place, that is, the first item is the red XML layout in the original background, and when scrolling to the top of the second item, the top item layout may not be the original.

In this case, if you are still in the original way, when you just swipe to the first item disappears, you will get the first item to the bottom.

However, if you implement the following method, the system will determine when you swipe the DE, the current first vanishing type, not the type I want to fill will not take him off the first line, to fill the tail, but instead of calling GetView tell the ListView to add the item view is empty, you go back to the new one.

When returning to the top, the system again to determine that the type of bin is not my bottom type, it will not be the bottom of the item moved to the front, but the re-use of the just.

How can we solve this problem?

We need to do these things:

1) rewrite Getviewtypecount () – How many different layouts the method returns

2) rewrite getitemviewtype (int) – returns the corresponding item according to position

3) Create the correct convertview in GetView based on the type of view item

    PrivateRunrankadapter Runrankadapter;  PublicArraylist<view>ListViews;  PublicViewpager Mpager; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Gson Gson=NewGson (); ArrayList<String> runrank=NewArraylist<string>();  for(inti =0;i< -; i++) {Runrank.add ("DSDF");             } ListView Lv_runrank; Runrankadapter=NewRunrankadapter ( This, Runrank); Lv_runrank=(ListView) Findviewbyid (R.ID.LISTVIEW1); Lv_runrank.setadapter (runrankadapter);

classRunrankadapter extends Baseadapter {ArrayList<String>ls;        Context Mcontext; LinearLayout LinearLayout=NULL;        Layoutinflater Inflater;        TextView Tex; FinalintView_type =3; FinalintType_1 =0; Finalinttype_2 =1; FinalintType_3 =2;  PublicRunrankadapter (Context context, ArrayList<String>list) {ls=list; Mcontext=context; } @Override Public intGetCount () {returnls.size (); } @Override PublicObject GetItem (intposition) {            returnLs.Get(position); } @Override Public LongGetitemid (intposition) {            return 0; }        //Each convert view calls this method to get the currently required view style@Override Public intGetitemviewtype (intposition) {            intp =position; if(p = =0)                returntype_1; Else if(p = =1)                returntype_2; Else                returnType_3; } @Override Public intGetviewtypecount () {return 3; } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {ViewHolder1 Holder1=NULL; ViewHolder2 Holder2=NULL; ViewHolder3 Holder3=NULL; intType =Getitemviewtype (position); if(Convertview = =NULL) {Inflater= Layoutinflater. from(Mcontext); //determine the layout of new by the current desired style                Switch(type) { CaseType_1:convertview=Inflater.inflate (R.layout.item_one, parent,false);//holder1 = new ViewHolder1 ();//Holder1.rank1 = (TextView) Convertview//. Findviewbyid (R.id.tv_list1_rank);//holder1.time1 = (TextView) Convertview//. Findviewbyid (r.id.tv_list1_time);//Convertview.settag (holder1);                     Break;  CaseType_2:convertview=Inflater.inflate (r.layout.item_two, parent,false);//holder2 = new ViewHolder2 ();//holder2.rank2 = (TextView) Convertview//. Findviewbyid (R.id.tv_list2_rank);//holder2.time2 = (TextView) Convertview//. Findviewbyid (r.id.tv_list2_time);//Convertview.settag (holder2);                     Break;  CaseType_3:convertview=Inflater.inflate (r.layout.item_3, parent,false);//Holder3 = new ViewHolder3 ();//Holder3.rank3 = (TextView) Convertview//. Findviewbyid (R.id.tv_list3_rank);//Holder3.time3 = (TextView) Convertview//. Findviewbyid (r.id.tv_list3_time);//Convertview.settag (HOLDER3);                     Break; default:                     Break; }            } Else {//switch (type) {//Case Type_1://Holder1 = (ViewHolder1) convertview.gettag ();//Break ;//Case type_2://Holder2 = (ViewHolder2) convertview.gettag ();//Break ;//Case Type_3://Holder3 = (ViewHolder3) convertview.gettag ();//Break ;//                }            }            //Set up Resources//switch (type) {//Case Type_1://Holder1.rank1.setText ("" + (position + 1));//Holder1.time1.setText (ls.get (position). Get ("Time"). ToString ());//Break ;//Case type_2://Holder2.rank2.setText ("" + (position + 1));//Holder2.time2.setText (ls.get (position). Get ("Time"). ToString ());//Holder2.totaldistance2.setText (ls.get (position)//Break ;//Case Type_3://Holder3.rank3.setText ("" + (position + 1));//Holder3.time3.setText (ls.get (position). Get ("Time"). ToString ());//Break ;//            }            returnConvertview; }         Public classViewHolder1 {TextView rank1;        TextView time1; }         Public classViewHolder2 {TextView rank2;        TextView time2; }         Public classViewHolder3 {TextView rank3;        TextView Time3; }    }
Adapter

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.