一,PageAdapter
package com.bwei.month_lianxi1.adapter; import android.support.annotation.NonNull; import android.support.v4.view.PagerAdapter; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import java.util.List; public class MyViewPager extends PagerAdapter { private List<ImageView> list; public MyViewPager(List<ImageView> list) { this.list = list; } @Override public int getCount() { return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(@NonNull View view, @NonNull Object object) { return view==object; } @NonNull @Override public Object instantiateItem(@NonNull ViewGroup container, int position) { position=position%list.size(); ImageView imageView = list.get(position); container.addView(imageView); return imageView; } @Override public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) { container.removeView((ImageView)object); } }
二, extends BaseAdapter ShangPin_Adapter
package com.bwei.month_lianxi1.adapter; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.bwei.month_lianxi1.R; import com.bwei.month_lianxi1.app.MyApp; import com.bwei.month_lianxi1.bean.ShangPin; import com.nostra13.universalimageloader.core.ImageLoader; import java.util.List; public class MyShangPin extends BaseAdapter{ private Context context; private List<ShangPin.DataBean> list; public MyShangPin(Context context, List<ShangPin.DataBean> list) { this.context = context; this.list = list; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if(convertView==null){ convertView=View.inflate(context, R.layout.shangpin_base,null); holder=new ViewHolder(); holder.imageView=convertView.findViewById(R.id.shangpin_mybase); holder.text1=convertView.findViewById(R.id.shangpin_mybase_text1); holder.text2=convertView.findViewById(R.id.shangpin_mybase_text2); convertView.setTag(holder); }else{ holder= (ViewHolder) convertView.getTag(); } //赋值 holder.text1.setText(list.get(position).getTitle()); holder.text2.setText(list.get(position).getPrice()+""); //获取图片地址 String images = list.get(position).getImages(); //截取图片地址 String pin_url=images.split("\\|")[0]; ImageLoader.getInstance().displayImage(pin_url,holder.imageView, MyApp.getOptions()); return convertView; } class ViewHolder{ ImageView imageView; TextView text1,text2; } }
三,IOS_Adapter
package com.bwei.month_lianxi1.adapter; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.bwei.month_lianxi1.R; import com.bwei.month_lianxi1.app.MyApp; import com.bwei.month_lianxi1.bean.Ios; import com.nostra13.universalimageloader.core.ImageLoader; import java.util.List; public class MyIosBase extends BaseAdapter { private Context context; private List<Ios.ResultsBean> list; public MyIosBase(Context context, List<Ios.ResultsBean> list) { this.context = context; this.list = list; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if(convertView==null){ convertView=View.inflate(context, R.layout.ios_mybase,null); holder=new ViewHolder(); holder.imageView=convertView.findViewById(R.id.ios_imageview); holder.textView=convertView.findViewById(R.id.ios_textview); convertView.setTag(holder); }else{ holder= (ViewHolder) convertView.getTag(); } //赋值 holder.textView.setText(list.get(position).getDesc()); //获取图片地址 List<String> images = list.get(position).getImages(); if(images!=null&&images.size()>0){ String pic_url = images.get(0);//图片地址 ImageLoader.getInstance().displayImage(pic_url,holder.imageView, MyApp.getOptions()); } return convertView; } class ViewHolder{ ImageView imageView; TextView textView; } }
四,FragmentPageAdapter
package com.bwei.month_lianxi1.adapter; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import java.util.List; public class MyFragmentAdapter2 extends FragmentPagerAdapter{ private List<Fragment> list; public MyFragmentAdapter2(FragmentManager fm, List<Fragment> list) { super(fm); this.list = list; } //获取条目 @Override public Fragment getItem(int position) { return list.get(position); } //获取数量 @Override public int getCount() { return list.size(); } @Nullable @Override public CharSequence getPageTitle(int position) { return list.get(position).getArguments().getString("title"); } }