Tablayout的tab没有自动回弹功能,第一次快速滑动之后才有回弹..
澳门银河官网
澳门银河官网
当前位置 : 澳门银河官网 > 澳门银河官网开户

Tablayout的tab没有自动回弹功能,第一次快速滑动之后才有回弹..

  1. 描述你的问题

这是模仿今日头条练习的,但是布局出现问题如下:

下面的bottom是用四个Fragment切换的,FragmentTransaction.replace切换的。顶部呢,是call这个Fragment的布局呢是TabLayout和ViewPager的,这几个tab也是用4个Fragment适配的...。感觉就是Fragment上加载fragment...

现在的问题是当从其他三个bottom的图标切换到call这一页的时候,第一,第二个没有回弹效果,也不加载textview,如图,到第三个就正常了...

比如,当Tab1正常下面还有个textview显示,当从call点击到其他三个任意一个,然后再回到call,这时候,tab1的textview不见了,tab1到tab2没有回弹效果,你滑多少就是多少...到滑到tab3就正常了

你觉得这问题是什么?或者说那个今日头条是怎样布局的?

  1. 贴上相关代码

代码太多,弄一点上来

底部fragment的写在mainActivity中

@Override
    protected void onCreateBundle savedInstanceState {
        super.onCreatesavedInstanceState;
        setContentViewR.layout.activity_main;

        initView;
        setDefaultFragment;
    }

    private void setDefaultFragment {

        FragmentManager manager = getSupportFragmentManager;
        FragmentTransaction transaction = manager.beginTransaction;
        theOne = new First;
        transaction.replaceR.id.content_layout, theOne;
        transaction.commit;
    }


    private void initView {

        one = LinearLayout findViewByIdR.id.one;
        two = LinearLayout findViewByIdR.id.two;
        three = LinearLayout findViewByIdR.id.three;
        four = LinearLayout findViewByIdR.id.four;
        one.setOnClickListenerthis;
        two.setOnClickListenerthis;
        three.setOnClickListenerthis;
        four.setOnClickListenerthis;

    }

    @Override
    public void onClickView view {

        FragmentManager fm = getSupportFragmentManager;
        FragmentTransaction ft = fm.beginTransaction;

        switch view.getId {

            case R.id.one:
                if theOne == null {
                    theOne = new First;
                }
                ft.replaceR.id.content_layout, theOne;
                break;

            case R.id.two:

                if theTwo == null {
                    theTwo = new Second;
                }
                ft.replaceR.id.content_layout, theTwo;
                break;

            case R.id.three:

                if theThree == null {

                    theThree = new Third;
                }
                ft.replaceR.id.content_layout, theThree;
                break;

            case R.id.four:
                if theFour == null {

                    theFour = new Fourth;
                }
                ft.replaceR.id.content_layout, theFour;
                break;

            default:
                break;

        }

        ft.commit;
    }
}

然后是,call的Fragment

public class First extends Fragment {

    private TabLayout tabLayout;
    private ViewPager pager;


    public First {
        // Required empty public constructor
    }


    @Override
    public View onCreateViewLayoutInflater inflater, ViewGroup container, Bundle savedInstanceState {
        // Inflate the layout for this fragment
        return inflater.inflateR.layout.fragment_first, container, false;
    }

    @Override
    public void onViewCreatedView view, @Nullable Bundle savedInstanceState {
        super.onViewCreatedview, savedInstanceState;
        tabLayout = TabLayout view.findViewByIdR.id.tabLayout;
        pager = ViewPager view.findViewByIdR.id.viewPager;
        
        
        FragmentsAdapter adapter = new FragmentsAdaptergetFragmentManager, Datas.getInstance.getTitleDatas, Datas
            .getInstance
            .getFragments;

        pager.setAdapteradapter;
        tabLayout.setupWithViewPagerpager;
        tabLayout.setTabsFromPagerAdapteradapter;
    }

}
  1. 贴上报错信息

  2. 贴上相关截图

  3. 已经尝试过哪些方法仍然没解决(附上相关链接)

FragmentsAdapter adapter = new FragmentsAdaptergetFragmentManager, Datas.getInstance.getTitleDatas, Datas
            .getInstance
            .getFragments;

这里传入 FragmentManager 的时候不要调用 getFragmentManager 方法(调用这个方法返回的是该 fragment 所在 activity 的 FragmentManager),应该使用 getChildFragmentManager 方法获取到的 FragmentManager。

fragment还没写。简单加个view就解决了

该答案已被忽略,原因:

广告位

澳门银河官网