Nested viewpager in listview (viewpager is a part of the item), and there is a small white point to determine the position.

Source: Internet
Author: User

=============== Problem description ====================


It has been plagued by bugs for a long time:
1. There will be confusion in white points, sometimes it is normal, sometimes it will not move (if there is only one item on the interface, white points can be switched normally, there will be two items will not move ).
2. When there are two items, if you slide the previous item image like the next item, the image of the following item will disappear.
I am a newbie. I am going to post the code and ask for your guidance.
Package COM. glasses. adapter; import Java. util. arraylist; import Java. util. hashmap; import Java. util. list; import Java. util. map; import Org. apache. HTTP. header; import Org. JSON. jsonarray; import Org. JSON. jsonexception; import Org. JSON. jsonobject; import android. content. context; import android. content. intent; import android. OS. message; import android. support. v4.view. pageradapter; import android. support. v4.view. vie Wpager; import android. support. v4.view. viewpager. onpagechangelistener; import android. util. log; import android. view. layoutinflater; import android. view. view; import android. view. view. onclicklistener; import android. view. viewgroup; import android. view. viewgroup. layoutparams; import android. widget. baseadapter; import android. widget. button; import android. widget. imageview; import android. widget. linearlayout; imp ORT android. widget. toast; import COM. example. activity. r; import COM. glasses. activity. tryactivity; import COM. glasses. utils. myviewpager; import COM. glasses. utils. networkutil; import COM. loopj. android. HTTP. asynchttpclient; import COM. loopj. android. HTTP. jsonhttpresponsehandler; import COM. loopj. android. HTTP. requestparams; public class goodsadapter extends baseadapter {/*** viewpager */private myviewpager vi Ewpager;/*** Installation Point imageview array */private imageview [] tips;/*** installation imageview array */private imageview [] mimageviews; // Private list <imageview> listimg;/*** image resource ID */private int [] imgidarray; // context private context; private layoutinflater Inflater; private button try_glasses; private imageview collect; Public goodsadapter (context) {// todo auto-generated constructor stubthis. context = Co Ntext; this. inflater = layoutinflater. from (context) ;}@ overridepublic int getcount () {// todo auto-generated method stubreturn 6 ;}@ overridepublic object getitem (INT position) {// todo auto-generated method stubreturn position;} @ overridepublic long getitemid (INT position) {// todo auto-generated method stubreturn position;} @ overridepublic view getview (INT position, view convertview, viewgroup parent) {View = Inflater. inflate (R. layout. item_goods, null); // place the linearlayout area of the white point linearlayout group = (linearlayout) view. findviewbyid (R. id. viewgroup); // obtain the viewpager control viewpager = (myviewpager) view. findviewbyid (R. id. viewpager); // load the image resource idimgidarray = new int [] {R. drawable. a, R. drawable. b, R. drawable. c, R. drawable. d, R. drawable. e}; // Add white points to linearlayout. Tips = new imageview [imgidarray. length]; for (int I = 0; I <tips. length; I ++) {imageview = new imageview (context); imageview. setlayoutparams (New layoutparams (10, 10); Tips [I] = imageview; if (I = 0) {tips [I]. setbackgroundresource (R. drawable. page_indicator_focused);} else {tips [I]. setbackgroundresource (R. drawable. page_indicator_unfocused);} group. addview (imageview);} // initialize the image set listimg = new arraylist <imageview> (); // load the image to the array mimageviews = New imageview [imgidarray. length]; for (INT I = 0; I <mimageviews. length; I ++) {imageview = new imageview (context); imageview. setbackgroundresource (imgidarray [I]); // imageview. setimageresource (imgidarray [I]); mimageviews [I] = imageview; listimg. add (imageview);} // adapter sets viewpager. setadapter (New myadapter (); viewpager. setonpagechangelistener (New onpagechangelistener () {// call to complete sliding @ overridepublic Vo Id onpageselected (INT arg0) {// todo auto-generated method stublog. I ("glasses", "1111111"); setimagebackground (arg0% mimageviews. length) ;}@ overridepublic void onpagescrolled (INT arg0, float arg1, int arg2) {// todo auto-generated method stub}/*** set the background color of the selected white point ** @ Param selectitems */private void setimagebackground (INT selectitems) {for (INT I = 0; I <tips. length; I ++) {if (I = selectitems) {Ti PS [I]. setbackgroundresource (R. drawable. page_indicator_focused);} else {tips [I]. setbackgroundresource (R. drawable. page_indicator_unfocused) ;}}@ overridepublic void onpagescrollstatechanged (INT arg0) {// todo auto-generated method stub}); // set the start position of viewpager. setcurrentitem (0); Return view;} public class myadapter extends pageradapter {@ overridepublic int getcount () {return listimg. size () ;}@ over Ridepublic Boolean isviewfromobject (view arg0, object arg1) {return arg0 = arg1;} @ overridepublic void destroyitem (view container, int position, object) {(viewpager) container ). removeview (listimg. get (position);}/*** load the image and divide the current position by the length of the Image array to obtain the remainder. */@ overridepublic object instantiateitem (view container, int position) {viewgroup group = (viewgroup) listimg. get (position % listimg. size (). Getparent (); // determines whether an image exists. If yes, remove it first and then add If (group! = NULL) {group. removeview (listimg. get (position);} (viewpager) container ). addview (listimg. get (position % listimg. size (); Return listimg. get (position % listimg. size ());}}}

============= Solution 1 ======================


I can imagine your needs, but there are too many things to be loaded when the listview is updated. We recommend that you put the image display in the Click Event of the listview item.

============= Solution 2 ======================


This requirement is too strong. The logic of creating a viewpager... for each item in listview is too complicated.
We recommend that you change the interaction scheme.
If the solution cannot be changed, we recommend that you do not use listview. You need to create several pageviews using code and add them to the main form through addview. This makes the logic better controlled.

============= Solution 3 ======================


Reply by referencing crazy9599 on the 4th floor:
Quote: reference the reply from lionfresh on the second floor:

I can imagine your needs, but there are too many things to be loaded when the listview is updated. We recommend that you put the image display in the Click Event of the listview item.
= The requirement seems to be difficult to change. Isn't it possible to achieve this? I don't know if it is a code issue.

Then optimize the code. The listview item displays an introduction. Click the item to bring up a popupwindow, and your image or something will be displayed in the popupwindow.

Nested viewpager in listview (viewpager is a part of the item), and there is a small white point to determine the position.

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.