============問題描述============java
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============android
============解決方案2============apache
============解決方案3============json