ListView中嵌套ViewPager(ViewPager是item的一部分),并且存在判断位置的小白点。
============问题描述============
被bug困扰了好久:
1.白点会出现混乱,有的时候是正常的,有的时候会不动(如果界面只有一个item,白点可以正常切换,有两个item就会不动)。
2.当有两个item时,如果滑动上一个item图片与下一个item一样时,下面的item的图片就会消失。
本人新手,贴上代码,求各位大神指导。
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.ViewPager; 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; import 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 viewPager; /** * 装点点的ImageView数组 */ private ImageView[] tips; /** * 装ImageView数组 */ private ImageView[] mImageViews; //存放图片的集合 private List<ImageView> listImg; /** * 图片资源id */ private int[] imgIdArray; // 上下文 private Context context; private LayoutInflater inflater; private Button try_glasses; private ImageView collect; public GoodsAdapter(Context context) { // TODO Auto-generated constructor stub this.context = context; this.inflater = LayoutInflater.from(context); } @Override public int getCount() { // TODO Auto-generated method stub return 6; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return position; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = inflater.inflate(R.layout.item_goods, null); //放置白点的LinearLayout区域 LinearLayout group = (LinearLayout) view.findViewById(R.id.viewGroup); //获取viewPager控件 viewPager = (MyViewPager) view.findViewById(R.id.viewPager); // 载入图片资源ID imgIdArray = new int[] { R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e }; // 将白点加入到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); } //初始化图片集合 listImg = new ArrayList<ImageView>(); // 将图片装载到数组中 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); } //适配器设置 viewPager.setAdapter(new MyAdapter()); viewPager.setOnPageChangeListener(new OnPageChangeListener() { //滑动完成调用 @Override public void onPageSelected(int arg0) { // TODO Auto-generated method stub Log.i("Glasses", "1111111"); setImageBackground(arg0 % mImageViews.length); } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO Auto-generated method stub } /** * 设置选中的白点的背景变色 * * @param selectItems */ private void setImageBackground(int selectItems) { for (int i = 0; i < tips.length; i++) { if (i == selectItems) { tips[i].setBackgroundResource(R.drawable.page_indicator_focused); } else { tips[i].setBackgroundResource(R.drawable.page_indicator_unfocused); } } } @Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub } }); //设置viewPager的起始位置 viewPager.setCurrentItem(0); return view; } public class MyAdapter extends PagerAdapter { @Override public int getCount() { return listImg.size(); } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } @Override public void destroyItem(View container, int position, Object object) { ((ViewPager) container).removeView(listImg.get(position)); } /** * 载入图片进去,用当前的position 除以 图片数组长度取余数是关键 */ @Override public Object instantiateItem(View container, int position) { ViewGroup group = (ViewGroup) listImg.get(position % listImg.size()).getParent(); //判断是否存在图片,如果存在先移除,后添加 if (group != null) { group.removeView(listImg.get(position)); } ((ViewPager) container).addView(listImg.get(position % listImg.size())); return listImg.get(position % listImg.size()); } } }
============解决方案1============
我能想象到你的需求,但是这样listView更新时需要加载的东西太多了,建议将图片的显示放在listView item的点击事件里面。
============解决方案2============
这需求太生猛了,listView每个Item建立一个viewPager.......,逻辑太复杂。
建议考虑更改交互方案.
如果方案不能改动的话,建议不用listview,需要几个PageView就用代码创建几个,通过addView加到主窗体中,这样逻辑好控制一些
============解决方案3============
那就优化下代码,ListView的item显示一个简介,点击item弹出一个popupwindow,在popupwindow中显示你的图片什么的。