标签:
代码如下:
public class MyViewPagerAdapter extends PagerAdapter { //显示的数据 private List<DataBean> datas = null; private LinkedList<View> mViewCache = null; private Context mContext ; private LayoutInflater mLayoutInflater = null; public MyViewPagerAdapter(List<DataBean> datas, Context context) { super(); this.datas = datas; this.mContext = context ; this.mLayoutInflater = LayoutInflater.from(mContext) ; this.mViewCache = new LinkedList<>(); } @Override public int getCount() { Log.e("test","getCount "); return this.datas.size(); } @Override public int getItemPosition(Object object) { Log.e("test","getItemPosition "); return super.getItemPosition(object); } @Override public Object instantiateItem(ViewGroup container, int position) { Log.e("test","instantiateItem " + position); ViewHolder viewHolder = null; View convertView = null; if(mViewCache.size() == 0){ convertView = this.mLayoutInflater.inflate(R.layout.viewadapter_item_layout , null ,false); TextView textView = (TextView)convertView.findViewById(R.id.view_pager_item_textview); viewHolder = new ViewHolder(); viewHolder.textView = textView; convertView.setTag(viewHolder); }else { convertView = mViewCache.removeFirst(); viewHolder = (ViewHolder)convertView.getTag(); } viewHolder.textView.setText(datas.get(position).title); viewHolder.textView.setTextColor(Color.YELLOW); viewHolder.textView.setBackgroundColor(Color.GRAY); container.addView(convertView ,ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT ); return convertView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { Log.e("test","destroyItem " + position); View contentView = (View) object; container.removeView(contentView); this.mViewCache.add(contentView); } @Override public boolean isViewFromObject(View view, Object o) { Log.e("test","isViewFromObject "); return view == o; } public final class ViewHolder{ public TextView textView ; } }
标签:
原文地址:http://blog.csdn.net/kent_todo/article/details/44779791