如何在底部导航视图片段中使用viewpager设置TabLayout?

时间:2019-05-26 15:04:28

标签: java android android-fragments android-viewpager androidx

我有一个bottom Navigation view导航视图,其中包含5个片段,在其中一个片段中,我需要一个卡片滑块(基本上是一个视图分页器,其选项卡布局可在两张卡片之间来回滑动)尝试了很多解决方案,但没有任何效果,我最成功的尝试是应用程序没有崩溃并且寻呼机片段未显示。

问::如何设置在底部导航视图的片段内有两个选项卡的视图分页器?

这是我的主要片段的XML和JAVA:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:calendarview="http://schemas.android.com/apk/res-auto"
    xmlns:weekview="http://schemas.android.com/apk/res-auto"
    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"
    tools:context=".Fragments.HealthRecordFragment">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="12dp"
        android:orientation="vertical">          
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">                
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
                <androidx.viewpager.widget.ViewPager
                    android:id="@+id/viewpager"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
                    <androidx.viewpager.widget.PagerTitleStrip
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="bottom" />
                        <com.google.android.material.tabs.TabLayout
                            android:id="@+id/tabs"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="top" />
                   </androidx.viewpager.widget.ViewPager>
                </LinearLayout>              
        </LinearLayout>            
    </LinearLayout>
</FrameLayout>

private TabLayout tabLayout;
private ViewPager viewPager;
/*******/
viewPager = (ViewPager) view.findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) view.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
/*******/
private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
    TabOne tabFirst = new TabFirst();
    TabTwo tabSecond = new TabSecond();
    adapter.addFragment(tabFirst, "tab title 1");
    adapter.addFragment(tabSecond, "tab title 2");
    viewPager.setAdapter(adapter);
}

这是我的视图分页器的第一个标签(XML和JAVA):

<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".Tabs.TabOne">

    <androidx.cardview.widget.CardView
        app:cardUseCompatPadding="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">         
        <TextView
            android:text="this is tab 1"              
            android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />          
    </androidx.cardview.widget.CardView>
</FrameLayout>

public class TabOne extends Fragment {
    public TabOne() {
        // Required empty public constructor
    }

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

        return view;
    }
}

这是我的视图分页器的第二个标签(XML和JAVA):

<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".Tabs.TabTwo">

    <androidx.cardview.widget.CardView
        app:cardUseCompatPadding="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">         
        <TextView
            android:text="this is tab 2"              
            android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />          
    </androidx.cardview.widget.CardView>
</FrameLayout>

public class TabTwo extends Fragment {
    public TabTwo() {
        // Required empty public constructor
    }

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

        return view;
    }
}

0 个答案:

没有答案