首页 > 学院 > 开发设计 > 正文

RecyclerView简单使用

2019-11-09 18:08:01
字体:
来源:转载
供稿:网友

RecyclerView是谷歌V7包下新增的控件,用来替代ListView的使用,他可以通过设置LayoutManager来快速实现listview、gridview、瀑布流的效果,而且还可以设置横向和纵向显示,添加动画效果也非常简单(自带了ItemAnimation,可以设置加载和移除时的动画,方便做出各种动态浏览的效果),也是官方推荐使用的

引用:

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

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

简单使用:

mRecyclerView = findView(R.id.id_recyclerview);

//设置布局管理器

 mRecyclerView.setLayoutManager(new LinearLayoutManager(this));//类listview//mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));//类gridview//mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, OrientationHelper.VERTICAL));//类瀑布流

//设置adapter

mRecyclerView.setAdapter(adapter)

//点击事件

需要自定义,有3中方法,目前以在adapter中添加点击为例

adapter:

/** * Created by hp on 2017/2/7. */public class MyListAdapter extends RecyclerView.Adapter<MyListAdapter.MyRecyclerListHolder> {    PRivate Context mContext;    private List<String> mDatas;    private LayoutInflater inflater;    public MyListAdapter(Context context, List<String> datas){        this. mContext=context;        this. mDatas=datas;        inflater=LayoutInflater. from(mContext);    }
//添加一个点击事件的接口
    public interface OnItemClickLitener{        void onItemClick(View view, int position);        void onItemLongClick(View view , int position);    }    private OnItemClickLitener mOnItemClickLitener;    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)    {        this.mOnItemClickLitener = mOnItemClickLitener;    }    @Override    public MyRecyclerListHolder onCreateViewHolder(ViewGroup parent, int viewType) {        View view = inflater.inflate(R.layout.item_home1, parent, false);        MyRecyclerListHolder holder= new MyRecyclerListHolder(view);        return holder;    }    @Override    public void onBindViewHolder(final MyRecyclerListHolder holder, int position) {        holder.tv.setText(mDatas.get(position));        holder.bb.setText(mDatas.get(position));        // 如果设置了回调,则设置点击事件        if (mOnItemClickLitener != null)        {
             //holder.itemView.其中iteamView是系统个提供的,表示对应的iteam一整栏,也就是说点击事件绑定的是整个iteam栏
             holder.itemView.setOnClickListener(new View.OnClickListener()
             //holder.tv.setOnClickListener这个点击事件就绑定为对应的textview控件了
            {                @Override                public void onClick(View v)                {                    int pos = holder.getPosition();                    mOnItemClickLitener.onItemClick(holder.itemView, pos);                }            });        }    }    @Override    public int getItemCount() {        return mDatas.size();    }    class MyRecyclerListHolder extends ViewHolder{        TextView tv;        Button bb;        public MyRecyclerListHolder(View itemView) {            super(itemView);            tv=(TextView) itemView.findViewById(R.id. id_num);            bb = (Button) itemView.findViewById(R.id.bb);        }    }}

-----------------------------------------------------------------------------------------------------------------------------------------------------

RecyclerView的分割线:

通过该方法添加分割线: mRecyclerView.addItemDecoration() 

//该分割线是系统默认的,你可以在theme.xml中找到该属性的使用情况

mRecyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL_LIST));

添加默认分割线:高度为2px,颜色为灰色mRecyclerView.addItemDecoration(new RecycleViewDivider(mContext, LinearLayoutManager.VERTICAL));添加自定义分割线:可自定义分割线drawablemRecyclerView.addItemDecoration(new RecycleViewDivider(    mContext, LinearLayoutManager.VERTICAL, R.drawable.divider_mileage));

添加自定义分割线:可自定义分割线高度和颜色mRecyclerView.addItemDecoration(new RecycleViewDivider(    mContext, LinearLayoutManager.VERTICAL, 10, getResources().getColor(R.color.divide_gray_color)));

万能分割线登场:public class RecycleViewDivider extends RecyclerView.ItemDecoration {    private Paint mPaint;    private Drawable mDivider;    private int mDividerHeight = 2;//分割线高度,默认为1px    private int mOrientation;//列表的方向:LinearLayoutManager.VERTICAL或LinearLayoutManager.HORIZONTAL    private static final int[] ATTRS = new int[]{android.R.attr.listDivider};    /**     * 默认分割线:高度为2px,颜色为灰色     *     * @param context     * @param orientation 列表方向     */    public RecycleViewDivider(Context context, int orientation) {        if (orientation != LinearLayoutManager.VERTICAL && orientation != LinearLayoutManager.HORIZONTAL) {            throw new IllegalArgumentException("请输入正确的参数!");        }        mOrientation = orientation;        final TypedArray a = context.obtainStyledAttributes(ATTRS);        mDivider = a.getDrawable(0);        a.recycle();    }    /**     * 自定义分割线     *     * @param context     * @param orientation 列表方向     * @param drawableId  分割线图片     */    public RecycleViewDivider(Context context, int orientation, int drawableId) {        this(context, orientation);        mDivider = ContextCompat.getDrawable(context, drawableId);        mDividerHeight = mDivider.getIntrinsicHeight();    }    /**     * 自定义分割线     *     * @param context     * @param orientation   列表方向     * @param dividerHeight 分割线高度     * @param dividerColor  分割线颜色     */    public RecycleViewDivider(Context context, int orientation, int dividerHeight, int dividerColor) {        this(context, orientation);        mDividerHeight = dividerHeight;        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);        mPaint.setColor(dividerColor);        mPaint.setStyle(Paint.Style.FILL);    }    //获取分割线尺寸    @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 onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {        super.onDraw(c, parent, state);        if (mOrientation == LinearLayoutManager.VERTICAL) {            drawVertical(c, parent);        } else {            drawHorizontal(c, parent);        }    }    //绘制横向 item 分割线    private void drawHorizontal(Canvas canvas, RecyclerView parent) {        final int left = parent.getPaddingLeft();        final int right = parent.getMeasuredWidth() - parent.getPaddingRight();        final int childSize = parent.getChildCount();        for (int i = 0; i < childSize; i++) {            final View child = parent.getChildAt(i);            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();            final int top = child.getBottom() + layoutParams.bottomMargin;            final int bottom = top + mDividerHeight;            if (mDivider != null) {                mDivider.setBounds(left, top, right, bottom);                mDivider.draw(canvas);            }            if (mPaint != null) {                canvas.drawRect(left, top, right, bottom, mPaint);            }        }    }    //绘制纵向 item 分割线    private void drawVertical(Canvas canvas, RecyclerView parent) {        final int top = parent.getPaddingTop();        final int bottom = parent.getMeasuredHeight() - parent.getPaddingBottom();        final int childSize = parent.getChildCount();        for (int i = 0; i < childSize; i++) {            final View child = parent.getChildAt(i);            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();            final int left = child.getRight() + layoutParams.rightMargin;            final int right = left + mDividerHeight;            if (mDivider != null) {                mDivider.setBounds(left, top, right, bottom);                mDivider.draw(canvas);            }            if (mPaint != null) {                canvas.drawRect(left, top, right, bottom, mPaint);            }        }    }附:自定的drawable文件一份<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle">    <size android:height="20dp" />    <solid android:color="#ff992900" /></shape>


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表