RecyclerView为多布局样式,实现上拉分页加载
adapter
public class PictrueAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<PictrueBean> list;
private Context context;
private LayoutInflater inflater;
private OnItemClickListener onItemClickListener;
public PictrueAdapter(List<PictrueBean> list, Context context, OnItemClickListener onItemClickListener) {
this.list = list;
this.context = context;
inflater = LayoutInflater.from(context);
this.onItemClickListener = onItemClickListener;
}
//设置监听
public interface OnItemClickListener {
void onItemClickListener(View view, int position);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = null;
RecyclerView.ViewHolder viewHolder = null;
//判断是哪种样式,加载哪种布局
switch (viewType) {
case 0:
view = inflater.inflate(R.layout.pictrue_adapter_layout, parent, false);
viewHolder = new MyViewHolder(view);
break;
case 1:
view = inflater.inflate(R.layout.pictrue_item_layout, parent, false);
viewHolder = new MyViewHolder2(view);
break;
case 2:
view=inflater.inflate(R.layout.footer_layout,parent,false);
viewHolder=new FooterViewHolder(view);
break;
}
return viewHolder;
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
//判断不同的样式,显示不同的内容]
if(holder instanceof PictrueAdapter.FooterViewHolder){
((FooterViewHolder) holder).button.setText("正在加载10条...");
}else
if (holder instanceof MyViewHolder) {
((MyViewHolder) holder).tvTitle.setText(list.get(position).getTitle());
((MyViewHolder) holder).tvCount.setText(list.get(position).getImgcount());
((MyViewHolder) holder).tvComment.setText(list.get(position).getReviewcount() + "");
Picasso.with(context).load(list.get(position).getBigimgsrc()).error(R.drawable.default_news).placeholder(R.drawable.default_news).into(((MyViewHolder) holder).iv);
} else if (holder instanceof MyViewHolder2) {
((MyViewHolder2) holder).tvTitle.setText(list.get(position).getTitle());
((MyViewHolder2) holder).tvCount.setText(list.get(position).getImgcount());
((MyViewHolder2) holder).tvComment.setText(list.get(position).getReviewcount() + "");
String img = list.get(position).getSmallimgsrc();
String[] imgs = img.split("\\|");
Picasso.with(context).load(imgs[0]).error(R.drawable.default_news).placeholder(R.drawable.default_news).into(((MyViewHolder2) holder).iv1);
Picasso.with(context).load(imgs[1]).error(R.drawable.default_news).placeholder(R.drawable.default_news).into(((MyViewHolder2) holder).iv2);
Picasso.with(context).load(imgs[2]).error(R.drawable.default_news).placeholder(R.drawable.default_news).into(((MyViewHolder2) holder).iv3);
}
//设置点击item监听
if (onItemClickListener != null) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = holder.getLayoutPosition();
onItemClickListener.onItemClickListener(holder.itemView, pos);
}
});
}
}
//设置不用的样式
@Override
public int getItemViewType(int position) {
if (position == getItemCount() - 1) {
return 2;
} else
if (position % 4 == 0) {
return 0;
} else {
return 1;
}
}
@Override
public int getItemCount() {
return list == null ? 0 : list.size()+1;
}
class MyViewHolder extends RecyclerView.ViewHolder {
ImageView iv;
TextView tvTitle, tvComment, tvCount;
public MyViewHolder(View itemView) {
super(itemView);
iv = ((ImageView) itemView.findViewById(R.id.pictrue_iv));
tvComment = ((TextView) itemView.findViewById(R.id.pictrue_comment));
tvTitle = ((TextView) itemView.findViewById(R.id.pictrue_title));
tvCount = ((TextView) itemView.findViewById(R.id.pictrue_count));
}
}
class MyViewHolder2 extends RecyclerView.ViewHolder {
ImageView iv1, iv2, iv3;
TextView tvTitle, tvComment, tvCount;
public MyViewHolder2(View itemView) {
super(itemView);
iv1 = ((ImageView) itemView.findViewById(R.id.pictrue_item_iv1));
iv2 = ((ImageView) itemView.findViewById(R.id.pictrue_item_iv2));
iv3 = ((ImageView) itemView.findViewById(R.id.pictrue_item_iv3));
tvComment = ((TextView) itemView.findViewById(R.id.pictrue_item_comment));
tvTitle = ((TextView) itemView.findViewById(R.id.pictrue_item_title));
tvCount = ((TextView) itemView.findViewById(R.id.pictrue_item_count));
}
}
class FooterViewHolder extends RecyclerView.ViewHolder {
Button button;
public FooterViewHolder(View itemView) {
super(itemView);
button = (Button) itemView.findViewById(R.id.footer_btn);
}
}
}
java代码:
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if(newState==RecyclerView.SCROLL_STATE_IDLE) {
int lastPosition = -1;
RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
if (manager instanceof LinearLayoutManager) {
lastPosition = ((LinearLayoutManager) manager).findLastVisibleItemPosition();
} else if (manager instanceof GridLayoutManager) {
lastPosition = ((GridLayoutManager) manager).findLastVisibleItemPosition();
} else if (manager instanceof StaggeredGridLayoutManager) {
int[] lastPositions = new int[((StaggeredGridLayoutManager) manager).getSpanCount()];
((StaggeredGridLayoutManager) manager).findLastVisibleItemPositions(lastPositions);
lastPosition = findMax(lastPositions);
}
if (lastPosition == ((LinearLayoutManager) manager).getItemCount()-1) {
initData(list.get(list.size() - 1).getId());
}
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
swip.setEnabled(manager.findFirstCompletelyVisibleItemPosition()==0);
}
});
swip.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
handler.sendEmptyMessageDelayed(0,3000);
}
});
private int findMax(int[] lastPositions){
int max=lastPositions[0];
for (int elem:lastPositions){
if(elem>max){
max=elem;
}
}
return max;
}
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
list.clear();
initData(0);
swip.setRefreshing(false);
}
};