Communication between fragments?

  android, android-fragments, java

I have 2 fragments, one is the MAIN and the other is the SUB, the problem is:
The MAIN FRAGMENT has a Tablayout + ViewPager(Ex: tabitem1, tabitem2, tabitem3), and the SUB FRAGMENT has 4 CardView this cards should redirect the View to the TABITEM according to the CardView selected;

THIS IS THE MAIN FRAGMENT

public class HomeFragment extends Fragment {

    private TabLayout tabLayout;
    private ViewPager viewPager;
    private Button buttonLocation;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);

        tabLayout = view.findViewById(R.id.tabLayoutHome);
        viewPager = view.findViewById(R.id.viewPagerHome);
       
        //Set Adapter PageAdapter and glue it together
        viewPager.setAdapter(new FragmentAdapterHome(getChildFragmentManager(), FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT, view.getContext()));
        // 2 - Glue TabLayout and ViewPager together
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setupWithViewPager(viewPager);


        return view;
    }

THIS IS THE SUBFRAGMENT

public class AllCategoriesFragment extends Fragment {
    private CardView cardViewOne, cardViewTwo, cardViewTree, cardViewFour;
   
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.sub_fragment_all_categories, container, false);

        cardViewOne = view.findViewById(R.id.cardViewOne);
        cardViewTwo = view.findViewById(R.id.cardViewTwo);
        cardViewTree = view.findViewById(R.id.cardViewTree);
        cardViewFour = view.findViewById(R.id.cardViewFour);
        setCadViewsIntent(view);



        return view;
    }

    public void setCadViewsIntent(View view) {
        cardViewOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

               //The Cardview REDIRECT the tabitem to the selected

            }
        });

        cardViewTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               
            }
        });
        //AND SO ON...EACH CARD LED TO THE SPECIFIC CATEGORY EX: TABITEM 1, TABITEM 2;
}

THE XML IN THE HOMEFRAGMENT

<com.google.android.material.tabs.TabLayout
            android:id="@+id/tabLayoutHome"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="visible"
            app:tabGravity="start"
            app:tabIndicatorColor="#00FFFFFF"
            app:tabIndicatorFullWidth="true"
            app:tabIndicatorGravity="bottom"
            app:tabIndicatorHeight="0dp"
            app:tabMinWidth="150dp"
            app:tabMode="scrollable"
            app:tabSelectedTextColor="#000000"
            app:tabTextColor="@color/grey">

            <com.google.android.material.tabs.TabItem
                android:id="@+id/tabItem2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Tab2" />

            <com.google.android.material.tabs.TabItem
                android:id="@+id/tabItem3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Tab3" />

            <com.google.android.material.tabs.TabItem
                android:id="@+id/tabItem4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Tab4" />
        </com.google.android.material.tabs.TabLayout>

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewPagerHome"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

AND ONE OF THE TABS CONTENT THAT SHOWS THE CATEGORIES WITH CARDVIEWS

<LinearLayout
            android:id="@+id/linearLayoutCardsView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="0dp">

            <androidx.cardview.widget.CardView
                android:id="@+id/cardViewOne"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:clickable="true"
                android:focusable="true"
                app:cardCornerRadius="5dp"
                app:cardElevation="10dp">

                <ImageView
                    android:id="@+id/imageView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="end"
                    android:orientation="horizontal">

                    <androidx.cardview.widget.CardView
                        android:layout_width="150dp"
                        android:layout_height="match_parent"
                        app:cardBackgroundColor="#CC000000"
                        app:cardCornerRadius="0dp" />
                </LinearLayout>

            </androidx.cardview.widget.CardView>
</LinearLayout>

Source: Android Questions

LEAVE A COMMENT