RecyclerView多布局的简单实现

一、在做项目时,我们经常会遇到如下面的 

RecyclerView多布局的简单实现_第1张图片            RecyclerView多布局的简单实现_第2张图片

在一个窗口中,不同行的布局不同,有的是一行一个条目,有的是一行两个条目,还有四个的,还有更多,这里是数量不同,还有的是不同行里条目数量相同而布局不同,都可以用recyclerview的多布局来实现,下面就简单实现布局和数量都不同的recycle人view的多布局实例

1、使用recyclerview要到包

    在gradle添加这个依赖同步就行

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

2、在mainactivity的xml布局里直接写上recyclerview,如下

 



    

3、在mainactivity里findViewById找到这个控件mainactivity代码如下:

 

public class MainActivity extends AppCompatActivity {
    protected RecyclerView mRecy;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_main);
        initView();
        initData();
    }
    private void initData() {
        //下面的4代表的一行的size是4
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 4);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            //返回position对应的条目所占的size
            @Override
            public int getSpanSize(int position) {
                if (position < 4)
                    //这里返回4,指的是当position满足上面条件时,一个条目占得size是4
                    //也就是说这个条目占一行,因为上面设置的一行的size是4
                    return 4;
                else if (3 <= position && position < 6)
                    //这里返回2,指的是当position满足上面条件时,一个条目占得size是2
                    // 也就是说这个条目占半行,因为上面设置的一行的size是4
                    return 2;
                else
                    //这里返回1,指的是当position满足上面条件时,一个条目占得size是1
                    // 也就是说这个条目占1/4行,因为上面设置的一行的size是4
                    return 1;
            }
        });
        //用来添加分割线
        //mRecy.addItemDecoration();
        //设置管理
        mRecy.setLayoutManager(gridLayoutManager);
        MyRecyclerAdapter adapter = new MyRecyclerAdapter();
        //设置适配器
        mRecy.setAdapter(adapter);
    }
    private void initView() {
        mRecy = (RecyclerView) findViewById(R.id.recy);
    }
}

4、下面是适配器

 

public class MyRecyclerAdapter extends RecyclerView.Adapter {
    /*上面的ViewHolder是这个适配器必要的泛型,必须有。布局里有几种Type,下面就要写几个自定义的
    ViewHlder,这些自定义的ViewHolder都要继承于RecyclerView.ViewHolder。*/
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        RecyclerView.ViewHolder viewHolder = null;
        //根据viewType生成viewHolder
        switch (viewType) {
            case 0:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_type1, null);
                viewHolder = new VH(view);
                break;
            case 1:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_type2, null);
                viewHolder = new VH1(view);
                break;
            case 2:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_type3, null);
                viewHolder = new VH2(view);
                break;
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        //根据条目的类型给holder中的控件填充数据
        int itemViewType = getItemViewType(position);
        switch (itemViewType) {
            case 0:
                VH vh = (VH) holder;
                vh.mTextView.setText("类型1");
                break;
            case 1:
                VH1 vh1 = (VH1) holder;
                vh1.mTextView.setText("类型2");
                vh1.mImageView.setImageResource(R.drawable.a);
                break;
            case 2:
                VH2 vh2 = (VH2) holder;
                vh2.mTextView.setText("类型3");
                break;
        }
    }

    @Override
    public int getItemCount() {
        //获取条目数,模拟数据,这里是写死的
        return 30;
    }

    @Override
    public int getItemViewType(int position) {
        //跟据position对应的条目返回去对应的样式(Type)
        if (position < 4) {
            return 0;
        } else if (4 <= position && position < 6) {
            return 1;
        } else return 2;
    }
}

5、这里写几个简单的ViewHolder

第一个ViewHolder

 
public class VH extends RecyclerView.ViewHolder {
    TextView mTextView;
    public VH(View itemView) {
        super(itemView);
        mTextView = (TextView) itemView.findViewById(R.id.tv_type1);
    }
}

其xml布局为


    

第二个ViewHolder

 
public class VH1 extends RecyclerView.ViewHolder {
    ImageView mImageView;
    TextView mTextView;
    public VH1(View itemView) {
        super(itemView);
        mImageView= (ImageView) itemView.findViewById(R.id.iv_type2);
        mTextView= (TextView) itemView.findViewById(R.id.tv_type2);
    }
}

其xml布局为

 



    

    

 

第三个ViewHoler

 

 
 
public class VH2 extends RecyclerView.ViewHolder {
    TextView mTextView;
    public VH2(View itemView) {
        super(itemView);
        mTextView= (TextView) itemView.findViewById(R.id.tv_type3);
    }
}

其xml布局为



    
 

6、总结:按上面的思路就能简单的实现recyclerview的多布局,这个控件在开发过程中使用频率还是非常高的,希望通过这次的简单实例能让你们学会!

附加:给listview形式的Recycler添加分割线

 

/**
 * 给 list 形式 recylerView 添加分割线
 * @return
 */
public static void addItemDecoration(Context context, RecyclerView listView) {
    SimpleRecyclerViewItemDecoration itemDecoration = new SimpleRecyclerViewItemDecoration(context, SimpleRecyclerViewItemDecoration.VERTICAL_LIST, context.getResources().getDrawable(R.drawable.form_dividing_line));
    itemDecoration.setHeight(1);
    listView.addItemDecoration(itemDecoration);
}

 

自定义的分割线:

 

public class SimpleRecyclerViewItemDecoration 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 mWidth;
    private int mHeight;
    private int mOrientation;

    public SimpleRecyclerViewItemDecoration(Context context, int orientation) {
        final TypedArray a = context.obtainStyledAttributes(ATTRS);
        mDivider = a.getDrawable(0);
        a.recycle();
        setOrientation(orientation);
    }
    /**
     * 新增:支持自定义dividerDrawable
     *
     * @param context
     * @param orientation
     * @param dividerDrawable
     */

    public SimpleRecyclerViewItemDecoration(Context context, int orientation, Drawable dividerDrawable) {
        mDivider = dividerDrawable;
        setOrientation(orientation);
    }
    public void setOrientation(int orientation) {
        if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
            throw new IllegalArgumentException("invalid orientation");
        }
        mOrientation = orientation;
    }
    /**
     * 新增:支持手动为无高宽的drawable制定宽度
     * @param width
     */
    public void setWidth(int width) {
        this.mWidth = width;
    }

    /**
     * 新增:支持手动为无高宽的drawable制定高度
     * @param height
     */
    public void setHeight(int height) {
        this.mHeight = height;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        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 +
                    Math.round(ViewCompat.getTranslationY(child));
            final int bottom = top + getDividerHeight();
            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 +
                    Math.round(ViewCompat.getTranslationX(child));
            final int right = left + getDividerWidth();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        if (mOrientation == VERTICAL_LIST) {
            outRect.set(0, 0, 0, getDividerHeight());
        } else {
            outRect.set(0, 0, getDividerWidth(), 0);
        }
    }

    private int getDividerWidth() {
        return mWidth > 0 ? mWidth : mDivider.getIntrinsicWidth();
    }
    private int getDividerHeight() {
        return mHeight > 0 ? mHeight : mDivider.getIntrinsicHeight();
    }
}

 

你可能感兴趣的:(View,utils,android)