RecyclerView的简单使用

添加依赖:

compile 'com.android.support:recyclerview-v7:21.0.3'

在layout文件中添加recyclerview:


在代码中设置adapter以及设置divider:

recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.addItemDecoration(new VirticalDivider());
adapter = new DrawerListAdapter(getContext());
recyclerView.setAdapter(adapter);

这里的VirticalDivider会找不到,不过不要着急,这个是用来给RecyclerView设置divider的,下面会讲到。

实现Adapter

recyclerview的adapter会有一个官方的ViewHolder

public class DrawerListAdapter extends RecyclerView.Adapter {

    private ArrayList data = new ArrayList<>();
    private Context context;

    public DrawerListAdapter(Context context) {
        this.context = context;
    }

    public void updateData(List names) {
        data.clear();
        if (ArrayUtils.isNotEmpty(names)) {
            data.addAll(names);
        }
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.layout_item_drawer_menu, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ApkName name = data.get(position);
        holder.tvTitle.setText(name.name);

    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.drawer_item_title)
        TextView tvTitle;
        @BindView(R.id.drawer_item_icon)
        ImageView ivIcon;
        @BindView(R.id.drawer_item_container)
        ViewGroup container;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}

adapter需要继承RecyclerView.Adapter并且有一个RecyclerView.ViewHolder的子类作为泛型。
这里的ViewHolder需要继承RecyclerView.ViewHolder,并且需要实现一个构造方法。
onCreateViewHolder里需要实例化每一行的view。
onBindViewHolder就是实现view与数据的绑定了。

给recyclerview添加divider

只需要实现onDraw方法就好了,会在这个方法里根据for循环来一次把所有的divider都画出来了。

public class VirticalDivider extends RecyclerView.ItemDecoration {

    public static final int DEVIDER_HEIGHT = 1;

    @Override
    public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(canvas, parent, state);
        int dividerLeft = parent.getPaddingLeft();
        int dividerRight = parent.getWidth() - parent.getPaddingRight();

        int childCount = parent.getChildCount();
        Paint paint = new Paint();
        paint.setColor(Color.GRAY);
        //for循环在每一个item后面画一条线,最后一个不画,i < childCount -1
        for (int i = 0; i < childCount -1; i++) {
            View child = parent.getChildAt(i);

            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

            int dividerTop = child.getBottom() + params.bottomMargin;
            int dividerBottom = dividerTop + DEVIDER_HEIGHT;
            //设置矩形的大小
            Rect rect = new Rect(dividerLeft, dividerTop, dividerRight, dividerBottom);
            //画一个矩形
            canvas.drawRect(rect, paint);
        }
    }
}

其实还有一些其他的写法:
from:https://gist.github.com/alexfu/0f464fc3742f134ccd1e#file-divideritemdecoration-java

public class DividerItemDecoration extends RecyclerView.ItemDecoration {

    private static final int[] ATTRS = new int[]{
            android.R.attr.listDivider
    };

    public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;

    public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;

    private Drawable mDivider;

    private int mOrientation;

    public DividerItemDecoration(Context context, int orientation) {
        final TypedArray a = context.obtainStyledAttributes(ATTRS);
        mDivider = a.getDrawable(0);
        a.recycle();
        setOrientation(orientation);
    }

    public void setOrientation(int orientation) {
        if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
            throw new IllegalArgumentException("invalid orientation");
        }
        mOrientation = orientation;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent) {
        if (mOrientation == VERTICAL_LIST) {
            drawVertical(c, parent);
        } else {
            drawHorizontal(c, parent);
        }
    }

    public void drawVertical(Canvas c, RecyclerView parent) {
        final int left = parent.getPaddingLeft();
        final int right = parent.getWidth() - parent.getPaddingRight();

        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                    .getLayoutParams();
            final int top = child.getBottom() + params.bottomMargin;
            final int bottom = top + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    public void drawHorizontal(Canvas c, RecyclerView parent) {
        final int top = parent.getPaddingTop();
        final int bottom = parent.getHeight() - parent.getPaddingBottom();

        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                    .getLayoutParams();
            final int left = child.getRight() + params.rightMargin;
            final int right = left + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
        if (mOrientation == VERTICAL_LIST) {
            outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
        } else {
            outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
        }
    }
}

这里基本就能满足一些常见用法。

todo:

  • 各个LayoutManager的用法以及实现。
  • recyclerview丰富的动画的使用。

你可能感兴趣的:(RecyclerView的简单使用)