小编Chi*_*ayu的帖子

OnBackpressed在tablayout上获取空视图

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

我使用TabLayout和设置了片段标签ViewPager.问题是当我移动到下一个片段然后我按下后退按钮我得到空标签视图.

在这里我附上我的代码.

这是我的家庭片段,当我点击衬衫时,它将我移动到下一个片段

正如我所说,之前的片段让我感动这个产品细节片段. 现在,当我按下后退按钮时,它给我空视图

正如我所说,它给了我这个空洞的看法

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>`
Run Code Online (Sandbox Code Playgroud)

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()); …
Run Code Online (Sandbox Code Playgroud)

android android-adapter android-fragments android-viewpager android-tablayout

5
推荐指数
2
解决办法
1451
查看次数