I have an activity with tablayout and four fragments. This works fine and i can swipe between the fragments. But now i need a tablayout within one of this fragments for a submenu. So there will be two tablayout on top of each other. I thought i can do the same like in activity in the fragment. But it doesn't work how i hoped :D
Know is my question, is this possible or i need something else to get a submenu in a fragment?
MainActivity:
public class MainActivity extends AppCompatActivity implements TabLayoutMediator.TabConfigurationStrategy{
ViewPager2 viewPager2;
TabLayout tabLayout;
ArrayList<String> titles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager2 = findViewById(R.id.vp2MainMenu);
tabLayout = findViewById(R.id.tlMainMenu);
titles = new ArrayList<String>();
titles.add("Dashboard");
titles.add("Logs");
titles.add("Chat");
titles.add("FAQ");
setViewPagerAdapter();
new TabLayoutMediator(tabLayout, viewPager2, this).attach();
}
public void setViewPagerAdapter() {
TabMainAdapter tabMainAdapter = new
TabMainAdapter(this);
ArrayList<Fragment> fragmentList = new ArrayList<>(); //creates an ArrayList of Fragments
fragmentList.add(new DashboardFragment());
fragmentList.add(new LogsFragment());
fragmentList.add(new ChatFragment());
fragmentList.add(new FaqFragment());
tabMainAdapter.setData(fragmentList); //sets the data for the adapter
viewPager2.setAdapter(tabMainAdapter);
}
@Override
public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
tab.setText(titles.get(position));
}
public class TabMainAdapter extends FragmentStateAdapter {
private ArrayList<Fragment> fragments;
public TabMainAdapter(@NonNull FragmentActivity fragmentActivity) {
super(fragmentActivity);
}
@NonNull
@Override
public Fragment createFragment(int position) {
return fragments.get(position);
}
@Override
public int getItemCount() {
return fragments.size();
}
public void setData(ArrayList<Fragment> fragments) {
this.fragments = fragments;
}
}
}
activity_man.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tlMainMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"
app:layout_constraintTop_toTopOf="parent"
android:background="@color/black" />
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/vp2MainMenu"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@id/tlMainMenu"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
"Main" fragment with tablayout and the submenu fragment:
public class LogsFragment extends Fragment implements TabLayoutMediator.TabConfigurationStrategy{
//global variables
ViewPager2 viewPager2;
TabLayout tabLayout;
ArrayList<String> titles;
public LogsFragment() {
// Required empty public constructor
}
public static LogsFragment newInstance(String param1, String param2) {
LogsFragment fragment = new LogsFragment();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setContentView(R.layout.fragment_logs);
viewPager2 = getActivity().findViewById(R.id.viewpager);
tabLayout = getActivity().findViewById(R.id.result_tabs);
titles = new ArrayList<String>();
titles.add("Shop");
titles.add("Tribe");
setViewPagerAdapter();
new TabLayoutMediator(tabLayout, viewPager2, this).attach();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.fragment_logs,container,false);
return rootview;
}
public void setViewPagerAdapter() {
TabLogsAdapter tabMainAdapter = new
TabLogsAdapter(getActivity());
ArrayList<Fragment> fragmentList = new ArrayList<>(); //creates an ArrayList of Fragments
fragmentList.add(new LogsShopFragment());
fragmentList.add(new LogsTribeFragment());
//and more fragment for the submenu
tabMainAdapter.setData(fragmentList); //sets the data for the adapter
viewPager2.setAdapter(tabMainAdapter);
}
@Override
public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
tab.setText(titles.get(position));
}
public class TabLogsAdapter extends FragmentStateAdapter {
private ArrayList<Fragment> fragments;
public TabLogsAdapter(@NonNull FragmentActivity fragmentActivity) {
super(fragmentActivity);
}
@NonNull
@Override
public Fragment createFragment(int position) {
return fragments.get(position);
}
@Override
public int getItemCount() {
return fragments.size();
}
public void setData(ArrayList<Fragment> fragments) {
this.fragments = fragments;
}
}
}
fragment_logs.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
tools:ignore="MissingConstraints">
<com.google.android.material.tabs.TabLayout
android:id="@+id/result_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"
app:layout_constraintTop_toTopOf="parent"
android:background="@color/black" />
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@id/result_tabs"
app:layout_constraintBottom_toBottomOf="parent"/>
</com.google.android.material.appbar.AppBarLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</RelativeLayout>
The result i get, when i start the app: After starting app
This looks first fine. But if i swipe or click on logs, chat or faq, i get this: After swipe to another fragment
Where i have my mistake?