片段内的ViewPager不显示内容

时间:2019-09-20 08:14:30

标签: android android-fragments android-viewpager fragment

im正在开发一个具有底部导航功能的android应用,该应用可以打开3个不同的片段而无需使用viewpager。一个片段内部有一个viewpager,可打开2个片段。这两个片段是相同的片段,每个片段内部都有一个recyclerview。我的问题是,当我运行该应用程序时,除viewpager之外的所有内容均按预期运行。我尝试将片段放入viewpager而不是它中,该片段有效。我什至尝试将viewpager放在一个空的活动中,它也可以正常工作。

这是我的MainActivity,它具有BottomNavigation及其功能:

public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
    private androidx.appcompat.widget.Toolbar toolbar;
    private BottomNavigationView navView;

    private Fragment fragment1 = new HomeFragment();
    private Fragment fragment2 = new SearchFragment();
    private Fragment fragment3 = new SettingsFragment();
    private FragmentManager fm = getSupportFragmentManager();
    private Fragment active = fragment1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        toolbar = findViewById(R.id.action_bar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("Home");

        fm.beginTransaction().add(R.id.main_container, fragment3, "settings").hide(fragment3).commit();
        fm.beginTransaction().add(R.id.main_container, fragment2, "search").hide(fragment2).commit();
        fm.beginTransaction().add(R.id.main_container, fragment1, "home").commit();

        navView = findViewById(R.id.nav_view);
        navView.setOnNavigationItemSelectedListener(this);
    }


    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            case R.id.navigation_home:
                fm.beginTransaction().hide(active).show(fragment1).addToBackStack("home").commit();
                active = fragment1;
                toolbar.setTitle("Home");
                return true;

            case R.id.navigation_search:
                fm.beginTransaction().hide(active).show(fragment2).addToBackStack("search").commit();
                active = fragment2;
                toolbar.setTitle("Search");
                return true;

            case R.id.navigation_settings:
                fm.beginTransaction().hide(active)
                    .show(fragment3).addToBackStack("settings").commit();
                active = fragment3;
                toolbar.setTitle("Settings");
                return true;
        }
        return false;
    }

    public void setCurrentPage(Fragment fragment, int position) {
        if(fm.findFragmentByTag("playlist"+position) != null){
            fm.beginTransaction().remove(fragment).hide(active).add(R.id.main_container, fragment, "playlist"+position).addToBackStack(null).commit();
            active = fragment;
        } else {
            fm.beginTransaction().hide(active).add(R.id.main_container, fragment, "playlist"+position).addToBackStack(null).commit();
            active = fragment;
        }
    }
}

这是MainActivityLayout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    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"
    tools:context=".MainActivity">
    <include
    layout="@layout/action_bar"
    android:id="@+id/action_bar"/>

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="56dp"
        android:layout_marginBottom="70dp"
        android:id="@+id/main_container_parent">
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:id="@+id/main_container"/>
    </androidx.core.widget.NestedScrollView>


    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/windowBackground"
        android:layout_alignParentBottom="true"
        app:menu="@menu/bottom_nav_menu"
        app:itemTextColor="@color/bottom_nav_color"
        app:itemIconTint="@color/bottom_nav_color"
        android:elevation="20dp"/>
</RelativeLayout>

这是我实现ViewPager的片段:

public class HomeFragment extends Fragment {

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

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        PlaylistsPagerAdapter playlistsPagerAdapter = new PlaylistsPagerAdapter(getChildFragmentManager());
        playlistsPagerAdapter.addFragment(new PlaylistsFragment(),"private");
        playlistsPagerAdapter.addFragment(new PlaylistsFragment(),"public");
        ViewPager viewPager = view.findViewById(R.id.pager_home);
        viewPager.setAdapter(playlistsPagerAdapter);
        TabLayout tabLayout = view.findViewById(R.id.tab_layout_home);
        tabLayout.setupWithViewPager(viewPager);
        setHasOptionsMenu(false);
    }

    public class PlaylistsPagerAdapter extends FragmentStatePagerAdapter {
        ArrayList<Fragment> mFragmentList = new ArrayList<>();
        ArrayList<String> mFragmentListTitle = new ArrayList<>();

        public void addFragment(Fragment playlistsFragment, String title) {
            mFragmentListTitle.add(title);
            mFragmentList.add(playlistsFragment);
        }

        public PlaylistsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @NonNull
        @Override
        public Fragment getItem(int i) {
            return mFragmentList.get(i);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentListTitle.get(position);
        }
    }
}

这是HomeFragmentLayout:

<?xml version="1.0" encoding="utf-8"?>
<androidx.viewpager.widget.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager_home"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tab_layout_home"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</androidx.viewpager.widget.ViewPager>

这是播放列表片段:

public class PlaylistsFragment extends Fragment {
    private static final String TAG = "PlaylistsFragment";

    private ArrayList<String> mPlaylistName = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_playlists, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        initNameBitmap(view);
    }

    private void initNameBitmap(View view){
        mPlaylistName.add("Playlist1");
        mPlaylistName.add("Playlist2");
        mPlaylistName.add("Playlist3");
        mPlaylistName.add("Playlist4");

        initRecyclerView(view);
    }

    private void initRecyclerView(View view){
        RecyclerView recyclerView = view.findViewById(R.id.recycler_view_playlists);
        RecyclerViewAdapterPlaylists recyclerViewAdapterPlaylists = new RecyclerViewAdapterPlaylists(mPlaylistName, view.getContext(),this);
        recyclerView.setAdapter(recyclerViewAdapterPlaylists);
        recyclerView.setLayoutManager(new LinearLayoutManager(getView().getContext()));
    }
}

这是播放列表片段布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/recycler_view_playlists"
        android:layout_margin="10dp" />

</LinearLayout>

奇怪的是它已经起作用了。我尝试了onBackPressed,并在实现此功能时按了BottomNavigation上的另一个视图,而不是手机上的“后退”按钮和意图显示的“主页”按钮:

fm.beginTransaction().hide(active).show(fragmentsHistory.get(fragmentsHistory.size()-1));
active = fragmentsHistory.get(fragmentsHistory.size()-1)
fragmentsHistory.remove(fragmentsHistory.size()-1);

我在做什么错? 感谢帮助!

1 个答案:

答案 0 :(得分:1)

我认为问题出在您的布局上

<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".ui.plans.PlansFragment">

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimaryDark"
        app:tabSelectedTextColor="@color/white"
        app:tabTextColor="#DBD8D8" />

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

您的HomeFragmentLayout应该与此类似