相关视频教程在某站上面(🔍浩宇软件开发)
1. 涉及到的技术点
- TabLayout的使用
- ViewPager2的使用
- TabLayout+ViewPager2配合实现左右滑动切换页面
2. 代码实现过程
温馨提示:上一集中,已经把TabLayout+ViewPager2布局已经写好了,只是没有数据填充,看不到任何效果
- 准备数据,编写分类数据实体,取名叫TitleInfo.java
ublic class TitleInfo {
private String title;
private String py_title;
public TitleInfo(String title, String py_title) {
this.title = title;
this.py_title = py_title;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPy_title() {
return py_title;
}
public void setPy_title(String py_title) {
this.py_title = py_title;
}
}
- 初始化分类数据
//初始化title数据
titles.add(new TitleInfo("推荐", "top"));
titles.add(new TitleInfo("国内", "guonei"));
titles.add(new TitleInfo("国际", "guoji"));
titles.add(new TitleInfo("娱乐", "yule"));
titles.add(new TitleInfo("体育", "tiyu"));
titles.add(new TitleInfo("军事", "junshi"));
titles.add(new TitleInfo("科技", "keji"));
titles.add(new TitleInfo("财经", "caijing"));
titles.add(new TitleInfo("游戏", "youxi"));
titles.add(new TitleInfo("汽车", "qiche"));
titles.add(new TitleInfo("健康", "jiankang"));
- viewPager 需要设置一个adapter
viewPager.setAdapter(new FragmentStateAdapter(this) {
@NonNull
@Override
public Fragment createFragment(int position) {
String title = titles.get(position).getPy_title();
TabNewsFragment tabNewsFragment = TabNewsFragment.newInstance(title);
return tabNewsFragment;
}
@Override
public int getItemCount() {
return titles.size();
}
});
- TabLayout点击事件
tab_layout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
//设置viewPager选中当前页
viewPager.setCurrentItem(tab.getPosition(), false);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
- TabLayout和viewPager关联在一起
TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(tab_layout, viewPager, new TabLayoutMediator.TabConfigurationStrategy() {
@Override
public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
tab.setText(titles.get(position).getTitle());
}
});
//这句话一定不能少
tabLayoutMediator.attach();
- 主页MainActivity.java全部实现过程
public class MainActivity extends AppCompatActivity {
// private String[] titles = {"娱乐", "军事", "教育", "文化", "将康", "财经", "体育", "汽车", "科技"};
private List<TitleInfo> titles = new ArrayList<>();
private TabLayout tab_layout;
private ViewPager2 viewPager;
private NavigationView nav_view;
private TextView tv_username;
private TextView tv_nickname;
private ImageView btn_open_drawerLayout;
private DrawerLayout drawer_layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化title数据
titles.add(new TitleInfo("推荐", "top"));
titles.add(new TitleInfo("国内", "guonei"));
titles.add(new TitleInfo("国际", "guoji"));
titles.add(new TitleInfo("娱乐", "yule"));
titles.add(new TitleInfo("体育", "tiyu"));
titles.add(new TitleInfo("军事", "junshi"));
titles.add(new TitleInfo("科技", "keji"));
titles.add(new TitleInfo("财经", "caijing"));
titles.add(new TitleInfo("游戏", "youxi"));
titles.add(new TitleInfo("汽车", "qiche"));
titles.add(new TitleInfo("健康", "jiankang"));
//初始化控件
tab_layout = findViewById(R.id.tab_layout);
viewPager = findViewById(R.id.viewPager);
nav_view = findViewById(R.id.nav_view);
btn_open_drawerLayout = findViewById(R.id.btn_open_drawerLayout);
drawer_layout = findViewById(R.id.drawer_layout);
//注意事项, 不能直接findViewById
// tv_username=findViewById(R.id.nav_view);
// tv_username = nav_view.getHeaderView(0).findViewById(R.id.tv_username);
tv_username = nav_view.getHeaderView(0).findViewById(R.id.tv_username);
tv_nickname = nav_view.getHeaderView(0).findViewById(R.id.tv_nickname);
//btn_open_drawerLayout打开抽屉
btn_open_drawerLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawer_layout.open();
}
});
//nav_view点击事件
nav_view.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
if (item.getItemId() == R.id.nav_history) {
//跳转到历史记录
Intent intent = new Intent(MainActivity.this, HistoryListActivity.class);
startActivity(intent);
} else if (item.getItemId() == R.id.nav_update_pwd) {
//判断是否登录
UserInfo userInfo = UserInfo.getUserInfo();
if (null != userInfo) {
startActivity(new Intent(MainActivity.this, UpdatePwdActivity.class));
} else {
Toast.makeText(MainActivity.this, "请先登录~~", Toast.LENGTH_SHORT).show();
}
} else if (item.getItemId() == R.id.nav_about) {
startActivity(new Intent(MainActivity.this, AboutActivity.class));
} else if (item.getItemId() == R.id.nav_exit) {
UserInfo userInfo = UserInfo.getUserInfo();
if (null!=userInfo){
new AlertDialog.Builder(MainActivity.this).setTitle("温馨提示").setMessage("确认要退出登录吗?").setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(MainActivity.this, LoginActivity.class));
UserInfo.setUserInfo(null);
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.show();
}else {
Toast.makeText(MainActivity.this, "请先登录~~", Toast.LENGTH_SHORT).show();
}
}
return true;
}
});
//viewPager 需要设置一个adapter
viewPager.setAdapter(new FragmentStateAdapter(this) {
@NonNull
@Override
public Fragment createFragment(int position) {
String title = titles.get(position).getPy_title();
TabNewsFragment tabNewsFragment = TabNewsFragment.newInstance(title);
return tabNewsFragment;
}
@Override
public int getItemCount() {
return titles.size();
}
});
//tab_layout点击事件
tab_layout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
//设置viewPager选中当前页
viewPager.setCurrentItem(tab.getPosition(), false);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
//tab_layout和viewPager关联在一起
TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(tab_layout, viewPager, new TabLayoutMediator.TabConfigurationStrategy() {
@Override
public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
tab.setText(titles.get(position).getTitle());
}
});
//这句话一定不能少
tabLayoutMediator.attach();
}
@Override
protected void onResume() {
super.onResume();
UserInfo userInfo = UserInfo.getUserInfo();
if (null != userInfo) {
tv_username.setText(userInfo.getUsername());
tv_nickname.setText(userInfo.getNickname());
} else {
tv_username.setText("请登录");
tv_nickname.setText("");
//登录点击事件
tv_username.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
}
});
}
}
}
温馨提示:其中涉及到的TabNewsFragment.java类,在下集说明