首页 > 解决方案 > 片段之间的通信?视图模型?界面?

问题描述

我有2个片段,一个是MAIN,另一个是SUB,问题是:MAIN FRAGMENT有一个Tablayout + ViewPager(例如:tabitem1,tabitem2,tabitem3),而SUB FRAGMENT有4个CardView这个卡片应该重定向根据选择的 CardView 查看到 TABITEM;

这是主要片段

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;
    }

这是子片段

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;
}

家庭片段中的 XML

<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" />

以及显示带有卡片视图的类别的选项卡内容之一

<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>

标签: javaandroidandroid-fragments

解决方案


您可以尝试通过以下方式进行交流parentFragment

   cardViewOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((HomeFragment) getParentFragment()).viewPager.setCurrentItem(1, false);
            }
        });

推荐阅读