OnBackpressed在tablayout上获取空视图

时间:2016-09-01 09:57:19

标签: android android-fragments android-viewpager android-adapter android-tablayout

嘿,伙计们请调查此事。我坚持解决这个问题。

我已使用TabLayoutViewPager在片段中设置了标签。问题是当我移动到下一个片段然后我按下后退按钮我得到空标签视图。

这里我附上了我的代码。

this is my home fragment when i click on shirt it moves me to next fragment

As i said previous fragment moved me to this product detail fragment. and now when i press back button it gives me empty view

As i said it gives me this empty view

home.xml

<?xml version="1.0" encoding="utf-8"?>

    <LinearLayout 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"
    android:gravity="center"
    android:orientation="vertical">


    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:tabIndicatorColor="@android:color/white"
        app:tabIndicatorHeight="4dp" />

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="fill_parent" />

    </LinearLayout>`

home.java(片段)

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.home, container, false);

    ((Drawer) getActivity()).setActionBarTitle("Home");

    Toast.makeText(getActivity(), "entry count" +    getActivity().getSupportFragmentManager().getBackStackEntryCount(), Toast.LENGTH_SHORT).show();


    tabLayout = (TabLayout) view.findViewById(R.id.tabLayout);

    tabLayout.addTab(tabLayout.newTab().setText("Products"));

    tabLayout.addTab(tabLayout.newTab().setText("Category"));

    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);


    viewPager = (ViewPager) view.findViewById(R.id.pager);
    Pager adapter = new Pager(getActivity().getSupportFragmentManager(), tabLayout.getTabCount());
   tabLayout.getTabCount());

    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

    viewPager.setAdapter(adapter);

    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(this);
    return view;

}

@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) {

}

pager.java(适配器)

public class Pager extends FragmentStatePagerAdapter {

int tabCount;

public Pager(FragmentManager fm, int tabCount) {
    super(fm);
    this.tabCount = tabCount;
}

@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            Product tab1 = new Product();
            return tab1;
        case 1:
            Category tab2 = new Category();
            return tab2;
        default:
            return null;
    }
}

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

MainActivity(我设置此标签片段的地方)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_drawer);


    getSupportActionBar().setTitle("Home");
    fragment = new home();
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.main, fragment);
    ft.commit();

4 个答案:

答案 0 :(得分:8)

我在替换fragment时遇到了同样的问题,而fragment取代了TabLayout(即 Inner Fragments

在研究了这个问题之后:我找到了解决方案

我的所作所为:

替换

 ViewPagerAdapter adapter = new ViewPagerAdapter(getActivity().getSupportFragmentManager());

如果您的父Activity正在展开AppCompatActivity,请使用

ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());

如果您的父活动正在延长Activity而不是

ViewPagerAdapter adapter = new ViewPagerAdapter(getActivity().getChildFragmentManager());

答案 1 :(得分:0)

在活动中写下代码

 @Override
    public void onBackPressed() {

            try {
                if (getFragmentManager().getBackStackEntryCount() > 1) {
                    FragmentManager.BackStackEntry first = getFragmentManager().getBackStackEntryAt(0);
                    getFragmentManager().popBackStack(first.getId(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
                    onNavigationItemSelected(navigationView.getMenu().getItem(0));
                } else
                    finish();

            } catch (Exception e) {
                e.printStackTrace();
            }

    }

答案 2 :(得分:0)

您无需将PageListeners附加到tabLayouttabListeners附加到ViewPager

这将解决这个问题。

tabLayout.setupWithViewPager(viewPager);

您的更新代码可能是这样的。如果你还有任何问题,请告诉我。

        @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.home, container, false);

        ((Drawer) getActivity()).setActionBarTitle("Home");

        Toast.makeText(getActivity(), "entry count" +    getActivity().getSupportFragmentManager().getBackStackEntryCount(), Toast.LENGTH_SHORT).show();


        tabLayout = (TabLayout) view.findViewById(R.id.tabLayout);

        tabLayout.addTab(tabLayout.newTab().setText("Products"));

        tabLayout.addTab(tabLayout.newTab().setText("Category"));

        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);



        viewPager = (ViewPager) view.findViewById(R.id.pager);
        Pager adapter = new Pager(getActivity().getSupportFragmentManager(), tabLayout.getTabCount());
       tabLayout.getTabCount());

 //Link tabLayout with ViewPager.
        tabLayout.setupWithViewPager(viewPager);

     return view;
    }

答案 3 :(得分:0)

您不应在片段中使用其活动Adapter创建FragmentManager。尝试替换

Pager adapter = new Pager(getActivity().getSupportFragmentManager(), tabLayout.getTabCount());

Pager adapter = new Pager(getChildFragmentManager(), tabLayout.getTabCount());