RecyclerView实现点击切换

RecyclerView实现点击切换_第1张图片RecyclerView实现点击切换_第2张图片

1.定义全局的布尔值

boolean b=false;//实现通过判断切换


2.实现三个视图

 
<LinearLayout
    android:id="@+id/li"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <Button
        android:id="@+id/add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="添加"/>
    <Button
        android:id="@+id/del"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="删除"/>
    <Button
        android:id="@+id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="List"/>
    <Button
        android:id="@+id/grid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Grid"/>
    <Button
        android:id="@+id/flow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="flow"/>
LinearLayout>

<android.support.v7.widget.RecyclerView
    android:id="@+id/id_recyclerview"
    android:divider="#ffff0000"
    android:dividerHeight="10dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

3.定义一个按钮,按钮的点击事件:

@OnClick({R.id.sel, R.id.choose})
public void onViewClicked(View view) {
    switch (view.getId()) {
        case R.id.sel:
            break;
        case R.id.choose:
            if(b==false){
                mRecyclerView.setLayoutManager(new GridLayoutManager(this,4));
                mRecyclerView.addItemDecoration(new DividerGridItemDecoration(this));
                b=true;
            }else{
                mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
                mRecyclerView.setAdapter(mAdapter = new HomeAdapter());
                mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
                b=false;
            }
            break;
    }
}


你可能感兴趣的:(RecyclerView,点击切换)