ITKeyword,专注技术干货聚合推荐

注册 | 登录

Why does changing fragments hide my tablayout (android)?

itPublisher 分享于

2021腾讯云限时秒杀,爆款1核2G云服务器298元/3年!(领取2860元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1062

2021阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址https://www.aliyun.com/minisite/goods

推荐:Android TabLayout 浅显总结

  环境搭建   1.Android design支持包和Android v4 支持包。 2.在build.gradle中添加 dependencies { compile files('libs/android-support-v4.jar')

I have two fragment in my app. fA and fb. fA has a toolbar, a tab layout and a viewpager. fB has a textview and an image view. When I click on some button in fA in any pager, it open fB and displays an image with some text. It opens all fine, but when I press back to return to my first fragment, I loose my tab layout. The place for the tabs is present, but no indicator or title. Can someone help?

My fA xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    android:fitsSystemWindows="true">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/primary"
        android:clipToPadding="false"
        android:elevation="4dp" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/primary"
        android:clipToPadding="false"
        android:elevation="4dp"
        android:paddingEnd="0dp"
        android:paddingStart="48dp"
        android:layout_marginTop="56dp"
        android:scrollbars="horizontal"
        app:tabGravity="center"
        app:tabIndicatorColor="@android:color/white"
        app:tabMode="scrollable"
        app:tabTextColor="@color/primary_text" />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false" />

</FrameLayout>

and my class:

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

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

        titles = getTitles();

        MainActivity parent = (MainActivity) getActivity();
        parent.setToolbar((Toolbar) view.findViewById(R.id.toolbar));

        viewPager = (ViewPager) view.findViewById(R.id.viewPager);
        stateListener = parent.getViewPagerStateListenerInstance();
        viewPager.setAdapter(new PagerAdapter(getChildFragmentManager()));
        viewPager.addOnPageChangeListener(stateListener);

        TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tab_layout);
        tabLayout.setupWithViewPager(viewPager);
        parent.setUpTabLayout(tabLayout);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        viewPager.removeOnPageChangeListener(stateListener);
    }

My toolbar and my tab layout are in the fragment and are to be replaced.

Any help is appreciated. Thank You

推荐:android material design之Tablayout,Recyclerview,Fragment,Viewpager搭配使用(四)

我的项目源码托管地址:点击打开我的项目源码地址 相信大家都用过fragment+viepager的组合,也都比较熟悉,现在说一下美拍,暴风,爱奇艺的高仿实现,这个实用性

answer 1

Please use the following code. It worked for me

viewPager.postDelayed(new Runnable() {
        @Override
        public void run() {
            setupWithViewPager(viewPager)
        }
    }, 1);

I do this each time on onCreateView of Fragment

推荐:Android TabLayout定制CustomView与ViewPager交互双向联动

 Android TabLayout定制CustomView与ViewPager交互双向联动 我之前写了一些关于Android Material Design TabLayout的文章, (1)《Android L、M扩展支持包des


相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。