在fragment中使用Tablayout+ViewPager切换Fragment(出现bug的解决)

在fragment中使用Tablayout+ViewPager切换Fragment

在上一篇文章(在子fragment实现TabLayout和ViewPager的组合)中,实现了可以在fragment中通过tablayout和viewpager实现对子fragment的切换,但是在使用过中发现问题,所以这篇文章记录一下bug的解决过程。

问题描述

但是在实际的使用过程中发现,当fragment先跳转到“我的”fragment即下面的图片

在这里插入图片描述
然后在点击下面左手数第三个图标就会出现下图的错误。

在这里插入图片描述

下面的bottombar就会消失,但是左右可以正常滑动,当点击上面页面中的某一个editetxt时,bottombar就会正常出现,如下图所示:

在这里插入图片描述

“我的”界面的fragment是使用了QMUI的List的布局。出现这个bug后之后,首先分析可能是fragmentmanager中出现了多余的fragment,但是发现不对。其次,我又觉得可能是使用的xml的布局文件出现了问题,经过一段时间测试,发现也不是问题的根源所在。最后,我想起之前想尝试QMUI中的QMUITabSegment控件时,看了看QMUI的源码,发现QMUI中在使用Tab时,使用Tab的addOnTabSelectedListener属性,用来判断tab的滑动,就顺着这个思路试着看看能不能通过这个方法来切换fragment,经过尝试,就在xml文件的viewpager控件中,添加了一个fragmelayout布局,使其通过viewpager来切换fragment。源码如下

(1):fragment_temporary_task.xml 修改为

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:id="@+id/draft_box_scrollview"
    tools:context=".fragment.TemporaryTaskFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@color/qmui_config_color_white">

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/fragment_temp_tabLayout"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            app:tabMaxWidth="0dp"
            app:tabGravity="fill"
            app:tabIndicatorColor="@color/lightblue"
            app:tabTextColor="@color/gray"
            app:tabSelectedTextColor="@color/lightblue"
            app:tabTextAppearance="@style/TabLayoutTextStyle"
            app:tabMode="fixed"
            app:tabBackground="@drawable/selector_bg">

        </com.google.android.material.tabs.TabLayout>

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

            <FrameLayout
                android:id="@+id/fl_content_temp"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                />

        </androidx.viewpager.widget.ViewPager>

    </LinearLayout>


</LinearLayout>

(2):与上面xml文件对应的fragment中代码修改为:

public class TemporaryTaskFragment extends Fragment  {

    private static final String TAG = "TemporaryTaskFragment";
    private QMUITabSegment tabSegment;

    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private TemporaryTaskFragmentAdapter myFragmentPagerAdapter;
    private PromptDialog promptDialog;

    private TabLayout.Tab one;
    private TabLayout.Tab two;
    private FrameLayout frameLayout;
    private List<Fragment> mFragmentList = new ArrayList<>();

    public TemporaryTaskFragment() {
        // 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_temporary_task, container, false);
        promptDialog = new PromptDialog(getActivity());

        initview(view);

        return view;
    }


    private void initview(View view) {
//        tabSegment = view.findViewById(R.id.fragment_temp_tab);
        mFragmentList.clear();
        mFragmentList.add(new DraftTaskFragment());
        mFragmentList.add(new YinhuanFragment());
        frameLayout = view.findViewById(R.id.fl_content_temp);
        //使用适配器将ViewPager与Fragment绑定在一起
        mViewPager = view.findViewById(R.id.fragment_temp_viewpager);

        //将TabLayout与ViewPager绑定在一起
        mTabLayout = view.findViewById(R.id.fragment_temp_tabLayout);
        mTabLayout.setupWithViewPager(mViewPager);

        //指定Tab的位置
        one = mTabLayout.getTabAt(0);
        two = mTabLayout.getTabAt(1);
        changeFragment(0);

        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                Log.e(TAG,"postion:"+tab.getPosition());
                changeFragment(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }

    private void changeFragment(int position) {
        FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fl_content_temp, mFragmentList.get(position));
        transaction.commit();
    }


  @Override
  public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    myFragmentPagerAdapter = new TemporaryTaskFragmentAdapter(getChildFragmentManager());
    mViewPager.setAdapter(myFragmentPagerAdapter);
  }

这样上面出现的bug就解决了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值