1. 程式人生 > >RecyclerView實現點選切換

RecyclerView實現點選切換

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;
    }
}