我们都知道RecyclerView有多种布局管理器,所以用起来也很方便 下面就是一个RecyclerView点击之后动态切换布局的一个Demo
实现思路
- 其实实现这个效果并不难,需要一些小逻辑,初始化一个Boolean值通过这个Boolean值来切换布局管理器
- 适配器有参构造,传入三个值,(1.集合,2.上下文,3.boolean值)
- 在适配器中的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>