I am creating a layout like WhatsApp and I have used viewPager2. It is swiping perfectly but when I click on a specific tab it does not go there. and if I click tabs from right to left it's moving but from left to right it does not move. I tried so many solution but nothing seems to work.
MainActivity:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.addTab(tabLayout.newTab().setText("Camera"));
tabLayout.addTab(tabLayout.newTab().setText("Chats"));
tabLayout.addTab(tabLayout.newTab().setText("Status"));
tabLayout.addTab(tabLayout.newTab().setText("Calls"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager2 viewPager = (ViewPager2) findViewById(R.id.pager);
viewPager.setOffscreenPageLimit(3);
final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), getLifecycle());
viewPager.setAdapter(adapter);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
super.onPageScrolled(position, positionOffset, positionOffsetPixels);
tabLayout.selectTab(tabLayout.getTabAt(position));
}
});
}
}
pagerAdapter:
public class PagerAdapter extends FragmentStateAdapter {
public PagerAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
super(fragmentManager, lifecycle);
}
@NonNull
@Override
public Fragment createFragment(int position) {
switch(position) {
case 0:
CameraFragment cameraFragment = new CameraFragment();
return cameraFragment;
case 1:
ChatsFragment chatsFragment = new ChatsFragment();
return chatsFragment;
case 2:
StatusFragment statusFragment = new StatusFragment();
return statusFragment;
case 3:
CallsFragment callsFragment = new CallsFragment();
return callsFragment;
default:
return new ChatsFragment();
}
}
@Override
public int getItemCount() {
return 4;
}
}
CodePudding user response:
Use TabLayoutMediator
instade of OnTabSelectedListener
Make changes as below
ArrayList<String> title = new ArrayList<>(); // add this before onCreate Method
//---------After onCreate
title.add("Camera");
title.add("Camera");
title.add("Camera");
title.add("Camera");
new TabLayoutMediator(tabLayout, viewPager, (tab, position) -> tab.setText(title.get(position))).attach(); //title.get(position) -> here add arraylist of title.
and remove these below codes
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
super.onPageScrolled(position, positionOffset, positionOffsetPixels);
tabLayout.selectTab(tabLayout.getTabAt(position));
}
});