Android viewpager之view的重用

//原理:1.public Object instantiateItem(ViewGroup container, int position)
// 2.public void destroyItem(ViewGroup container, int position, Object object)
//用一个链表,把移除出去的view保存起来,在初始view的时候,判断链表之中是否有待重用的view,,有就重用,没有就创建;标红的地方注意下
//view之中布局了RecyclerView,可以通过View.setTag().然后getTag()得到Adapter,从而达到刷新Recyclerview的目的
package com.uninew.adapter;

import java.util.LinkedList;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.view.PagerAdapter;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.uninew.base.RVBaseAdapter;
import com.uninew.base.RVBaseAdapter.OnItemClickListener;
import com.uninew.contant.Constants;
import com.uninew.usersbook.R;
import com.uninew.util.LogTool;

public class RecyclerPagerAdapter extends PagerAdapter {
private LinkedList<View> pages;
private LayoutInflater layoutInflater;
private RVBaseAdapter rvBaseAdapter;
private OnItemClickListener onItemClickListener;
public RecyclerPagerAdapter(Context context) {
super();
pages = new LinkedList<View>();
layoutInflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
// TODO Auto-generated method stub
if (Constants.contentslists != null ) {
return Constants.contentslists.size();
}
//页面可以无限大,都是重用view的
return 0;
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1;//return false,影响显示
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
LogTool.i("-->destroyItem: ");
if (object != null) {
container.removeView((View) object);//注意移除,否则already attach to a parent
pages.addLast((View) object);
}
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub

View page = null;
if (pages.size() > 0) {
LogTool.i("-->instantiateItem: recycler: " + position);
page = pages.getFirst();
pages.clear();//注意清除,否则会重复获取相同的view,会报错
rvBaseAdapter = (ContentAdapter) page.getTag();//小技巧获得 //Recyclerview的adapter可刷新view
rvBaseAdapter.clear();
rvBaseAdapter.addAll(Constants.contentslists.get(position - 1));
rvBaseAdapter.notifyDataSetChanged();
} else {
LogTool.i("-->instantiateItem: layoutInflater: " + position);
page = layoutInflater.inflate(R.layout.layout_recyclerview,
container, false);
RecyclerView recyclerView = (RecyclerView) page
.findViewById(R.id.rv);
page.setTag(recyclerView);
LinearLayoutManager llLayoutManager = new LinearLayoutManager(
container.getContext(), LinearLayoutManager.VERTICAL,
false);
ContentAdapter contentAdapter = new ContentAdapter();
contentAdapter
.addAll(Constants.contentslists.get(position - 1));
rvBaseAdapter = contentAdapter;
page.setTag(rvBaseAdapter);//小技巧获得Recyclerview的adapter可刷新view
recyclerView.setAdapter(rvBaseAdapter);
recyclerView.setLayoutManager(llLayoutManager);
}
container.addView(page);
return page;
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值