android studio :有一个页面,左侧有四个RecyclerView页面的button按钮,右侧是4个不同的fragment页面,要求:写一个RecyclerViewadapter,使按不同的button按钮,右侧显示不同的fragment页面
时间: 2024-03-09 21:49:20 浏览: 122
好的,这是一个比较典型的RecyclerView+Fragment的组合使用场景。
首先,你需要在Activity中定义四个Button和一个FrameLayout用于承载Fragment,代码类似于:
```java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private RecyclerView recyclerView;
private Button btn1;
private Button btn2;
private Button btn3;
private Button btn4;
private FrameLayout container;
private Fragment1 fragment1;
private Fragment2 fragment2;
private Fragment3 fragment3;
private Fragment4 fragment4;
private RecyclerViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化控件
recyclerView = findViewById(R.id.recycler_view);
btn1 = findViewById(R.id.btn1);
btn2 = findViewById(R.id.btn2);
btn3 = findViewById(R.id.btn3);
btn4 = findViewById(R.id.btn4);
container = findViewById(R.id.container);
// 设置按钮点击事件
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
// 初始化Fragment
fragment1 = new Fragment1();
fragment2 = new Fragment2();
fragment3 = new Fragment3();
fragment4 = new Fragment4();
// 设置默认显示的Fragment
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment1).commit();
// 初始化RecyclerView
adapter = new RecyclerViewAdapter(this);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn1:
// 切换到Fragment1
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment1).commit();
break;
case R.id.btn2:
// 切换到Fragment2
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment2).commit();
break;
case R.id.btn3:
// 切换到Fragment3
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment3).commit();
break;
case R.id.btn4:
// 切换到Fragment4
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment4).commit();
break;
default:
break;
}
}
}
```
接着,你需要编写RecyclerViewAdapter,代码类似于:
```java
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private Context context;
public RecyclerViewAdapter(Context context) {
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_recycler_view, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.btn.setText("按钮" + (position + 1));
}
@Override
public int getItemCount() {
return 4;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private Button btn;
public ViewHolder(View itemView) {
super(itemView);
btn = itemView.findViewById(R.id.btn);
}
}
}
```
最后,你需要在每个Fragment中实现相应的布局和逻辑即可。这里给出Fragment1的代码示例:
```java
public class Fragment1 extends Fragment {
private TextView tvTitle;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1, container, false);
tvTitle = view.findViewById(R.id.tv_title);
return view;
}
@Override
public void onResume() {
super.onResume();
tvTitle.setText("Fragment1");
}
}
```
其中,fragment1.xml布局文件中只需要包含一个TextView即可。
这样,当你点击左侧的RecyclerView中的按钮时,右侧会显示相应的Fragment页面。
阅读全文