片段中的片段选项卡无法正常工作

时间:2019-03-31 05:34:02

标签: java android android-fragments

我有一个活动,其中包含一个带有三个项目的bottomNavigationBar,在单击时会切换三个片段。因此,我希望在我的第一个片段中添加另外三个片段,并添加一个appBarLayout

我已完成所有设置,但问题是viewPagerappBarLayout中的片段无法正常工作。切换appBarLayout图标后,似乎失去了与bottomBar内部片段的连接。我得到什么。错误吗?

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:background="@color/colorPrimary"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true">
        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:tint="@android:color/white"
            android:layout_marginEnd="20dp"
            android:layout_marginRight="20dp"
            android:src="@drawable/transactions"
            android:contentDescription="@string/logo" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="15dp"
            android:ellipsize="end"
            android:text="@string/_1_dollar_pay"
            android:textColor="@android:color/white"
            android:textSize="16sp"
            app:fontFamily="@font/carter_one" />

    </android.support.v7.widget.Toolbar>
    <View
        android:id="@+id/shadow"
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:layout_below="@+id/toolbar"
        android:background="@color/colorPrimary"/>
    <RelativeLayout
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <View
            android:id="@+id/view"
            android:layout_marginBottom="5dp"
            android:layout_width="match_parent"
            android:layout_height="4dp"
            android:layout_above="@id/bottom_nav"
            android:background="@drawable/shadow"/>
        <FrameLayout
            android:id="@+id/container"
            android:layout_above="@+id/view"
            android:layout_marginTop="65dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </FrameLayout>

        <android.support.design.widget.BottomNavigationView
            android:id="@+id/bottom_nav"
            android:background="@android:color/white"
            android:layout_alignParentBottom="true"
            app:menu="@menu/bottom_nav_bar"
            android:theme="@style/Widget.BottomNavigationView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </android.support.design.widget.BottomNavigationView>
    </RelativeLayout>
</RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
    private HomeFragment homeFragment;
    private TransactionsFragment transactionsFragment;
    private AccountFragment accountFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        mAuth = FirebaseAuth.getInstance();
        currentUser =  mAuth.getCurrentUser();
        BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_nav);
        bottomNavigationView.setOnNavigationItemSelectedListener(this);
        homeFragment = new HomeFragment();
        transactionsFragment = new TransactionsFragment();
        accountFragment = new AccountFragment();
        getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.fade_in, R.anim.fade_out).replace(R.id.container, homeFragment).commit();
 }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
        if(id == R.id.home){
            getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.fade_in, R.anim.fade_out).replace(R.id.container, homeFragment).commit();
            return true;
        } else if(id==R.id.transactions){
            getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.fade_in, R.anim.fade_out).replace(R.id.container, transactionsFragment).commit();
            return true;
        }else if(id == R.id.account){
            getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.fade_in, R.anim.fade_out).replace(R.id.container, accountFragment).commit();
            return true;
        }
        return false;
    }
}

fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".fragments.HomeFragment">

    <!-- TODO: Update blank fragment layout -->
<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
            app:tabTextColor="@android:color/tertiary_text_dark"
            android:layout_width="match_parent"
            app:tabIndicatorHeight="5dp"
            app:tabIndicatorColor="@color/colorPrimary"
            android:background="@android:color/white"
            app:tabGravity="fill"
            app:tabMode="fixed"
            android:minHeight="?attr/actionBarSize"
            app:tabSelectedTextColor="@color/colorPrimary"
            app:tabTextAppearance="@style/CustomTabTextAppearance"
            android:layout_height="wrap_content">
        </android.support.design.widget.TabLayout>
        <android.support.v4.view.ViewPager
            android:layout_below="@+id/tabLayout"
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </android.support.v4.view.ViewPager>
        <View
            android:layout_marginTop="45.5dp"
            android:layout_width="match_parent"
            android:layout_height="4dp"
            android:background="@drawable/shadow"/>
    </RelativeLayout>
</android.support.design.widget.CoordinatorLayout>

</FrameLayout>

HomeFragment.java

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


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_home, container, false);

        TabLayout tabLayout = view.findViewById(R.id.tabLayout);
        tabLayout.addTab(tabLayout.newTab().setText("DASHBOARD"));
        tabLayout.addTab(tabLayout.newTab().setText("SETTINGS"));
        tabLayout.addTab(tabLayout.newTab().setText("APP INFO"));
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        final ViewPager viewPager = view.findViewById(R.id.viewPager);
        PagerAdapter pagerAdapter = new PagerAdapter(getFragmentManager(), tabLayout.getTabCount());
        viewPager.setAdapter(pagerAdapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        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) {

            }
        });
        return view;
    }


}

PagerAdapter.java

public class PagerAdapter extends FragmentStatePagerAdapter {
    int noOfTabs;
    public PagerAdapter(FragmentManager fm, int noOfTabs) {
        super(fm);
        this.noOfTabs = noOfTabs;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0:
                return new Tab1();
            case 1:
                return new Tab2();
            case 2:
                return new Tab3();
            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        return noOfTabs;
    }
}

1 个答案:

答案 0 :(得分:0)

在创建新的getChildFragmentManager()时使用PagerAdapter

PagerAdapter pagerAdapter = new PagerAdapter(getChildFragmentManager(), tabLayout.getTabCount());
相关问题