写在前头
- RecyclerView 是V7包出的Widget,相比ListView 使用更加灵活,对于适配不同样式的Item,RecyclerView更加方便。RecyclerView的加载数据性能比ListView要更高一些。RecyclerView的ItemClick需要自己在Adapter中定义,这点做的不如ListView方便。Divider做的也没有ListView那么方便。
使用方法
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
- 为RecyclerView根据需要设置合适的LayoutManager(必须,否则内容显示不出来)
如
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);
mListView.setLayoutManager(layoutManager);
- 编写需要的Adapter 继承至 RecyclerView.Adapter(其实使用RecyclerVIew关键点就在于Adapter)
public class MyAdapter extends RecyclerView.Adapter {
ArrayList<Model> models;
Context mContext;
MyAdapter(Context context,ArrayList<Model> models){
mContext = context;
this.models = models;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_list,null);
return new CircleHolder(itemView);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder!=null && holder instanceof CircleHolder){
MyHolder myHolder = (MyHolder) holder;
myHolder.setView(models.get(position));
}
}
@Override
public int getItemViewType(int position) {
//这里可以设置Item的不同类型,提供给onCreateViewHolder函数,获取不同的ViewHolder
return super.getItemViewType(position);
}
@Override
public int getItemCount() {
return models.size();
}
}
public class MyHolder extends RecyclerView.ViewHolder {
public MyHolder(View itemView) {
super(itemView);
}
public void setView(Model model){
}
}
mListView.setAdapter(new MyAdapter(getActivity(),models));
- 给RecyclerView设置ItemDecoration
mListView.addItemDecoration(new SimpleDividerItemDecoration(this,2));
- 自定义ItemDecoration
public class SimpleDividerItemDecoration extends RecyclerView.ItemDecoration {
private Drawable mDivider; //分割线Drawable
private int mDividerHeight; //分割线高度
/**
* 使用line_divider中定义好的颜色
* @param context
* @param dividerHeight 分割线高度
*/
public SimpleDividerItemDecoration(Context context, int dividerHeight) {
mDivider = ContextCompat.getDrawable(context, R.drawable.line_divider);
mDividerHeight = dividerHeight;
}
/**
*
* @param context
* @param divider 分割线Drawable
* @param dividerHeight 分割线高度
*/
public SimpleDividerItemDecoration(Context context, Drawable divider, int dividerHeight){
if(divider == null){
mDivider = ContextCompat.getDrawable(context, R.drawable.line_divider);
}else{
mDivider = divider;
}
mDividerHeight = dividerHeight;
}
//获取分割线尺寸
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
outRect.set(0, 0, 0, mDividerHeight);
}
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
int top = child.getBottom() + params.bottomMargin;
int bottom = top + mDividerHeight;
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
网友评论