RecyclerView提供了一种插拔式的体验,高度解耦,异常灵活,通过设置它提供的不同LayoutManager,ItemDecoration,ItemAnimator,实现令人瞠目的效果.从它的类名上看,RecyclerView所包含的意义是,我只管Recycler View,也就是RecyclerView只管回收与复用View,其他的我们自己可以去设置,可以看出高度的解耦,给我们充分的自由,所以我们才可以轻松地通过控件实现ListView,GridView,瀑布流等效果.
想要控制其显示方式,通过布局管理器LayoutManager
想要控制Item的间隔(可绘制),通过ItemDecoration
想控制Item的增删动画,通过ItemAnimation
想要控制点击事件、长按事件,功能自己写.
rv_main.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));//设置分割线
adapter.setOnItemClickListener(new CoastListAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
}
@Override
public void onItemLongClick(View view, final int position) {
}
});
RecyclerView的adapter继承自RecycleView.Adapter
public class CoastListAdapter extends RecyclerView.Adapter<CoastListAdapter.ViewHolder> {
private Context mContext;
private List<Coast> mCoastList;
public CoastListAdapter(Context context) {
this.mContext = context;
mCoastList = new ArrayList<>();
}
public void setData(List<Coast> coast) {
this.mCoastList = coast;
}
//自定义itemClickListener
public interface OnItemClickListener {
void onItemClick(View view, int position);
void onItemLongClick(View view, int position);
}
private OnItemClickListener mListener;
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.mListener = onItemClickListener;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {//Create View Holder
return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_coast, parent, false));
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.coast_data.setText(mCoastList.get(position).coastData);
holder.coast_money.setText(mCoastList.get(position).coastMoney + "");
holder.coast_title.setText(mCoastList.get(position).coastTitle);
if (mListener != null) {
final int pos = holder.getLayoutPosition();
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onItemClick(holder.itemView, pos);
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
mListener.onItemLongClick(holder.itemView, pos);
return false;
}
});
}
}
@Override
public int getItemCount() {//Total item count
return mCoastList.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
private TextView coast_data;
private TextView coast_title;
private TextView coast_money;
private ViewHolder(View itemView) {
super(itemView);
coast_data = (TextView) itemView.findViewById(R.id.tv_coast_date);
coast_title = (TextView) itemView.findViewById(R.id.tv_coast_title);
coast_money = (TextView) itemView.findViewById(R.id.tv_coast_money);
}
}
}
没有设置LayoutManager会出现以下错误:
No layout manager attached; skipping layout
要想快速滑动到某个位置,RecyclerView提供了四种方式
1.scrollBy(int x, int y)
需要自己计算移动的距离
2.scrollToPosition(int position)
这个方法是跳转到指定向,但是要注意,只是这个条目显示出来就可以了,可以是从上面,也可以是从下面,不一定是置顶的.
3.smoothScrollToPosition(int position)
和scrollToPosition相似,不过有个过渡的滑动效果.
4.scrollToPositionWithOffset(int position, int offset)
这个方法是移动到指定项,并且指定该项,不过,这个方法在LinearLayoutManager类里面,使用方法如下:
((LinearLayoutManager) mRecyclerView.getRecyclerView().getLayoutManager()).scrollToPositionWithOffset(position, 0);