标签:des android http io ar os java sp for
============问题描述============
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============
============解决方案2============
============解决方案3============
ListView中嵌套ViewPager(ViewPager是item的一部分),并且存在判断位置的小白点。
标签:des android http io ar os java sp for
原文地址:http://www.cnblogs.com/shaochuyun57/p/4083154.html