recyclerview是近年来取代listview的新控件,但是在实际开发中也有些问题需要我们注意的。
1.item复用问题
当我们的item比较多,recyclerview超出了一个屏幕需要滑动时,做上面的item的属性可能就会出现在屏幕下最新出现的一个item上,就和listview上出现的情况一样,以前listview我们一般是设置一个tag,而recyclerview自身内部封装的ViewHolderr就能实现item的回收和复用功能,我们是要做的是在你所有的 if 判断后加 else, 就能解决服用问题。
2.recyclerview实现复杂布局
当一个首页有轮播图、消息、列表等等控件时,一个个的排版略有生硬,而且在做页面滑动时,也会有冲突,当我们把整个页面看成一个recyclerview,里面所有的布局都是一个个item,如下:
adapter:
//onCreateViewHolder 3种布局
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType){
case TYPE_HEAD1:
return new HolderTypeHead1(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_head1, parent, false));
case TYPE_HEAD2:
return new HolderTypeHead2(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_head2, parent, false));
case TYPE_TYPE3:
return new HolderType3(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recyclerview, parent, false));
default:
Log.d("error","viewholder is null");
return null;
}
}
//初始化
public class HolderTypeHead1 extends RecyclerView.ViewHolder {
public HolderTypeHead1(View itemView) {
super(itemView);
}
}
/**
* Head2
*/
public class HolderTypeHead2 extends RecyclerView.ViewHolder {
public TextView textveiw1;
public ImageView image1;
public HolderTypeHead2(View itemView) {
super(itemView);
textveiw1 = (TextView) itemView.findViewById(R.id.textveiw1);
image1 = (ImageView) itemView.findViewById(R.id.image1);
}
}
/**
* Head3
*/
public static class HolderTypeArticle3 extends RecyclerView.ViewHolder {
public TextView text_contect;
public TextView text_date;
public HolderTypeArticle3(View itemView) {
super(itemView);
text_contect = (TextView) itemView.findViewById(R.id.text_contect);
text_date = (TextView) itemView.findViewById(R.id.text_date);
initListener(itemView);
}
//点击事件
private void initListener(View itemView) {
lin1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Log.i("MyTest", "poistion"+getAdapterPosition());
}
});
}
//数据绑定
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof HolderTypeHead1){
bindTypeHead1((HolderTypeHead1) holder, position);
}
else if (holder instanceof HolderTypeHead2){
bindTypeHead2((HolderTypeHead2) holder, position);
}
else if (holder instanceof HolderTypeArticle3){
bindType3((HolderTypeArticle3) holder, position);
}
}
//eg:
private void bindTypeHead1(HolderTypeHead1 holder, int position){
}
@Override
public int getItemCount() {
//传入的数据源 + 2(item1、item2)
return reBeanlist.size()+2;
}
//item TYPE 判断
@Override
public int getItemViewType(int position) {
if (position == 0){
return TYPE_HEAD1;
}else if (position == 1){
return TYPE_HEAD2;
}else {
return TYPE_TYPE3;
}
}
3.下拉刷新
recyclerview刷新或加载时,一般会传入一个 page ,刷新时为1、加载 +1
现在recyclerview外面套一个 SwipeRefreshLayout,
//初始化
swipeRefreshLayout.setColorSchemeResources(R.color.colorAccent,
R.color.colorAccent, R.color.colorPrimary, R.color.colorPrimaryDark);
//下拉刷新
private void downRefresh() {
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
Toast.makeText(getContext(), "正在刷新", Toast.LENGTH_SHORT).show();
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
beanlist.clear();
page = 1;
loadData(page);
setAdapter();
swipeRefreshLayout.setRefreshing(false);
}
}, 1500);
}
});
}
//加载时要先判断是否滑到最后一个item
recyclerveiw.setOnScrollListener(new RecyclerView.OnScrollListener() {
int lastVisibleItem;
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
//判断RecyclerView的状态 是空闲时,同时,是最后一个可见的ITEM时才加载
if(newState==RecyclerView.SCROLL_STATE_IDLE&&lastVisibleItem+1==adapter.getItemCount()){
swipeRefreshLayout.setRefreshing(true);
//设置正在加载更多
adapter.changeMoreStatus(adapter.LOADING_MORE);
//获取数据
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
// beanlist.clear();
List<Bean> beanlist2 = new ArrayList<Bean>();
if (page < 3) {
page = page + 1;
for (int i = 0; i< 10; i++) {
RankBean.Rankweekbean bean = new RankBean.Rankweekbean();
bean.setTlitle("这是一个名为X"+i+"的人");
beanlist2.add(bean);
}
adapter.AddFooterItem(beanlist2);
//设置回到上拉加载更多(notifyDataSetChanged)
adapter.changeMoreStatus(adapter.PULLUP_LOAD_MORE);
} else {
//adapter.changeMoreStatus(adapter.NO_LOAD_MORE);
Toast.makeText(getContext(), "没有更多数据啦!", Toast.LENGTH_SHORT).show();
adapter.changeMoreStatus(adapter.NO_LOAD_MORE);
}
swipeRefreshLayout.setRefreshing(false);
}
}, 2000);
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
lastVisibleItem=layoutManager.findLastVisibleItemPosition();
}
});