viewpager2+tablayout+recyclerview+fragment实现页面的切换
时间: 2023-09-15 19:16:48 浏览: 229
这个实现需要以下步骤:
1. 在布局文件中添加ViewPager2和TabLayout:
```xml
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed" />
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
```
2. 在代码中初始化ViewPager2和TabLayout:
```java
ViewPager2 viewPager = findViewById(R.id.view_pager);
TabLayout tabLayout = findViewById(R.id.tab_layout);
viewPager.setAdapter(new MyPagerAdapter(this));
new TabLayoutMediator(tabLayout, viewPager,
(tab, position) -> tab.setText("Tab " + (position + 1))
).attach();
```
3. 创建PagerAdapter,用于管理Fragment:
```java
public class MyPagerAdapter extends FragmentStateAdapter {
public MyPagerAdapter(FragmentActivity fa) {
super(fa);
}
@Override
public Fragment createFragment(int position) {
return new MyFragment(position + 1);
}
@Override
public int getItemCount() {
return 3;
}
}
```
4. 创建Fragment,用于显示RecyclerView:
```java
public class MyFragment extends Fragment {
private int mTabNumber;
public MyFragment(int tabNumber) {
mTabNumber = tabNumber;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_my, container, false);
RecyclerView recyclerView = rootView.findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(new MyRecyclerViewAdapter(mTabNumber));
return rootView;
}
}
```
5. 创建RecyclerViewAdapter,用于显示数据:
```java
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {
private int mTabNumber;
public MyRecyclerViewAdapter(int tabNumber) {
mTabNumber = tabNumber;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_my, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.mTextView.setText("Tab " + mTabNumber + ", Item " + (position + 1));
}
@Override
public int getItemCount() {
return 10;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
mTextView = itemView.findViewById(R.id.text_view);
}
}
}
```
6. 创建RecyclerView的item布局文件和Fragment的布局文件:
item_my.xml:
```xml
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp" />
```
fragment_my.xml:
```xml
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
```
这样就完成了实现。每个Tab都显示一个RecyclerView,切换Tab时切换RecyclerView。
阅读全文