Recyclerview 是谷歌官方推荐的新的列表控件,网上关于recyclerview的用法也颇有深度,她有自带的SwipeRefreshLayout 可很好的提供下拉刷新,唯独没有上拉加载控件,
需要使用的一般都采用其他的方法,这里,有一篇关于RecyclerView的上拉加载的文章,
直接上代码:
/**
-
Created by Kevin_zhou on 2016/11/11.
*/
public class TulingAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {private static final int TYPE_ZIJI_ITEM = 0; //普通Item
private static final int TYPE_FOOTER_ITEM = 1; //底部FooterView
private static final int TYPE_TULING = 2;//上拉加载更多
public static final int PULLUP_LOAD_MORE = 1;
//正在加载中
public static final int LOADING_MORE = 2;
//默认为0
private int load_more_status = 1;public List<TulingBean> list;
private OnItemClickListener mClickListener;public void setData(List<TulingBean> list){
this.list = list;
}//创建新View,被LayoutManager所调用
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
//如果viewType是普通item返回普通的布局,否则是底部布局并返回
if (viewType == TYPE_ZIJI_ITEM) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout
.cardview_recyclerview_item, viewGroup, false);
final ZiJiViewHolder vh = new ZiJiViewHolder(view);
if (mClickListener != null) {
vh.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mClickListener.onItemClick(vh.itemView, vh.getLayoutPosition());
}
});
}
return vh;
} else if (viewType == TYPE_TULING) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.char_tuling, viewGroup, false);
final TuLingViewHolder holder = new TuLingViewHolder(view);
if (mClickListener != null) {
holder.itemView.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View view) { mClickListener.onItemClick(holder.itemView, holder.getLayoutPosition()); } }); } return holder; } else { View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout .recyclerview_footer_view, viewGroup, false); FooterViewHolder vh = new FooterViewHolder(view); return vh; }
}
//将数据与界面进行绑定的操作
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
if (viewHolder instanceof ZiJiViewHolder) {
((ZiJiViewHolder) viewHolder).contentTv.setText(list.get(position).getContent());
} else if (viewHolder instanceof TuLingViewHolder){
((TuLingViewHolder) viewHolder).contentTv.setText(list.get(position).getContent());
}
else if (viewHolder instanceof FooterViewHolder) {
FooterViewHolder footViewHolder = (FooterViewHolder) viewHolder;
switch (load_more_status) {
case PULLUP_LOAD_MORE:
footViewHolder.foot_view_item_tv.setVisibility(View.VISIBLE);
footViewHolder.foot_view_item_tv.setText("上拉加载更多");
footViewHolder.pb.setVisibility(View.GONE);
break;
case LOADING_MORE:
footViewHolder.foot_view_item_tv.setVisibility(View.GONE);
footViewHolder.pb.setVisibility(View.VISIBLE);
break;
}
}
}@Override
public int getItemCount() {
//+1是加入底部的加载布局项
return list.size() + 1;
}public int getItemViewType(int position) {
// 如果position+1等于整个布局所有数总和就是底部布局
if (position + 1 == getItemCount()) {
return TYPE_FOOTER_ITEM;
} else {
if (list.get(position).getType() == 1){
return TYPE_TULING;
}else {
return TYPE_ZIJI_ITEM;
}
}
}//自定义的ViewHolder,持有每个Item的的所有界面元素
public static class ZiJiViewHolder extends RecyclerView.ViewHolder {
public TextView contentTv;
public ImageView iv;public ZiJiViewHolder(View view) { super(view); contentTv = (TextView) view.findViewById(R.id.tv_chat_me); iv = (ImageView) view.findViewById(R.id.iv_chat_me); }
}
//图灵布局
public static class TuLingViewHolder extends RecyclerView.ViewHolder {
public TextView contentTv;
public ImageView iv;public TuLingViewHolder(View view) { super(view); contentTv = (TextView) view.findViewById(R.id.tv_chat_tu); iv = (ImageView) view.findViewById(R.id.iv_chat_tu); }
}
/**
-
底部FooterView布局
*/
public static class FooterViewHolder extends RecyclerView.ViewHolder {
public TextView foot_view_item_tv;
public ProgressBar pb;public FooterViewHolder(View view) {
super(view);
pb = (ProgressBar) view.findViewById(R.id.progress_view);
foot_view_item_tv = (TextView) view.findViewById(R.id.tv_content);
}
}
public void setMoreStatus(int status){
load_more_status=status;
notifyDataSetChanged();
}public void setOnItemClickListener(OnItemClickListener listener) {
mClickListener = listener;
} -
public interface OnItemClickListener {
public void onItemClick(View itemView, int pos);
}
}
上面是RecyclerView适配器,就是对RecyclerView滚动监听,请看下面如何使用
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
private int lastVisibleItem;
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (!swipeRefreshWidget.isRefreshing()) {
if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 ==
adapter.getItemCount()) {
swipeRefreshWidget.setEnabled(false);
adapter.setMoreStatus(PullMoreRecyclerAdapter.LOADING_MORE);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
getDatas();
swipeRefreshWidget.setEnabled(true);
adapter.setMoreStatus(PullMoreRecyclerAdapter.PULLUP_LOAD_MORE);
adapter.notifyDataSetChanged();
}
}, 3000);
}
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
lastVisibleItem = mLayoutManager.findLastVisibleItemPosition();
}
});
如有疑问,欢迎提出。
网友评论