RecyclerView切换布局

我们都知道RecyclerView有多种布局管理器,所以用起来也很方便 下面就是一个RecyclerView点击之后动态切换布局的一个Demo

实现思路

  1. 其实实现这个效果并不难,需要一些小逻辑,初始化一个Boolean值通过这个Boolean值来切换布局管理器
  2. 适配器有参构造,传入三个值,(1.集合,2.上下文,3.boolean值)
  3. 在适配器中的MyRecyclerAdapter里边判断true的时候和false的时候加载不同的布局就OK了

依赖

implementation 'com.android.support:recyclerview-v7:28.0.0'

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn"
        android:text="切换"
        android:textSize="30sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

MainActivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private RecyclerView mRecyclerView;
    private Boolean mQieHuan = true;
    private List<String> list = new ArrayList<>();
    private Button mBtn;
    private MyRecyclerAdapter myRecyclerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        for (int i = 0; i < 30; i++) {
            list.add("数据" + i);
        }
        mRecyclerView = findViewById(R.id.recycler);
        mBtn = findViewById(R.id.btn);
        mBtn.setOnClickListener(this);
        setMyAdapter(mQieHuan);
    }

    @Override
    public void onClick(View view) {
        if(view.getId()==R.id.btn){
            if(mQieHuan == false){
                mQieHuan = true;
                mBtn.setText("网格");
            }else{
                mQieHuan = false;
                mBtn.setText("条目");
            }
            setMyAdapter(mQieHuan);
        }
    }

    public void setMyAdapter(boolean mQieHuan){
        if(mQieHuan==false){
            LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
            mRecyclerView.setLayoutManager(linearLayoutManager);
            myRecyclerAdapter = new MyRecyclerAdapter(list, this, mQieHuan);
            mRecyclerView.setAdapter(myRecyclerAdapter);
        }else if(mQieHuan==true){
            GridLayoutManager gridLayoutManager=new GridLayoutManager(this,2,LinearLayoutManager.VERTICAL,false);
            mRecyclerView.setLayoutManager(gridLayoutManager);
            myRecyclerAdapter = new MyRecyclerAdapter(list, this, mQieHuan);
            mRecyclerView.setAdapter(myRecyclerAdapter);
        }
    }
}

MyRecyclerAdapter

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.MyHolder> {
    private List<String> list;
    private Context context;
    private Boolean QieHuan;
    private View inflate;

    public MyRecyclerAdapter(List<String> list, Context context, Boolean qieHuan) {
        this.list = list;
        this.context = context;
        QieHuan = qieHuan;
    }

    @NonNull
    @Override
    public MyRecyclerAdapter.MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if(QieHuan==false){
            inflate = LayoutInflater.from(context).inflate(R.layout.item_one, null);
        }else{
            inflate = LayoutInflater.from(context).inflate(R.layout.item_two,null);
        }
        MyHolder myHolder = new MyHolder(inflate);
        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyRecyclerAdapter.MyHolder myHolder, int i) {
            myHolder.item_img.setImageResource(R.drawable.ic_launcher_background);
            myHolder.item_title.setText(list.get(i));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyHolder extends RecyclerView.ViewHolder {
        public TextView item_title;
        public ImageView item_img;
        public MyHolder(@NonNull View itemView) {
            super(itemView);

            item_img = itemView.findViewById(R.id.item_img);
            item_title = itemView.findViewById(R.id.item_title);
        }
    }
}

item_one.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/item_img"
        android:layout_width="150dp"
        android:layout_height="150dp" />

    <TextView
        android:id="@+id/item_title"
        android:textSize="35sp"
        android:layout_width="match_parent"
        android:layout_height="150dp" />

</LinearLayout>

item_two.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/item_img"
        android:layout_width="150dp"
        android:layout_gravity="center_horizontal"
        android:layout_height="150dp" />

    <TextView
        android:id="@+id/item_title"
        android:textSize="35sp"
        android:gravity="center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值