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 Context; private LayoutInflater inflater; private Button try_glasses; private ImageView collect; public GoodsAdapter (Context 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 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 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 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 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.

Related Article

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.