程序员

android RecyclerView的使用

2017-12-19  本文已影响0人  幸运星32

写在前头

使用方法

<android.support.v7.widget.RecyclerView
         android:id="@+id/recyclerView"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:scrollbars="vertical" />
 LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);
 mListView.setLayoutManager(layoutManager);
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));
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);
        }
    }
上一篇下一篇

猜你喜欢

热点阅读