ViewPager with tabLayout - Child Fragment需要时间来改变

时间:2018-02-23 10:04:16

标签: android xml android-fragments android-viewpager

**使用View Viewr适配器底部的tabLayout查看寻呼机,用于将片段切换到每个标签。问题是功能正常。但是tab切换中的片段在更改为另一个片段时显示为jerk。**

片段用于通过分离的线程调用api。

private void createViewPager(ViewPager viewPager) {
    TabLayoutViewPagerAdapter adapter = new TabLayoutViewPagerAdapter(getSupportFragmentManager());
    adapter.addFrag(homePageFragment);
    adapter.addFrag(orderHistoryFragment);
    adapter.addFrag(supportFragment);
    adapter.addFrag(new AccountDetailsFragment());
    viewPager.setAdapter(adapter);
    viewPager.setOffscreenPageLimit(viewPager.getAdapter().getCount());
}

ViewPager适配器

public class TabLayoutViewPagerAdapter extends FragmentStatePagerAdapter {

    private final List<Fragment> mFragmentList = new ArrayList<>();

    public List<String> getmFragmentTitleList() {
        return mFragmentTitleList;
    }

    private final List<String> mFragmentTitleList = new ArrayList<>();

    public TabLayoutViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFrag(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    public void addFrag(Fragment fragment) {
        mFragmentList.add(fragment);
    }
}

活动xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/tabs"
            android:background="@color/white"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="55dp"
            android:layout_alignParentBottom="true"
            android:layout_gravity="bottom"
            android:background="@drawable/tablayout_bg"
            app:tabIndicatorColor="@color/zotocolor"
            app:tabMode="fixed"
            app:tabSelectedTextColor="@color/zotocolor"
            app:tabTextColor="@color/black" />

    </RelativeLayout>

</android.support.design.widget.CoordinatorLayout>

1 个答案:

答案 0 :(得分:0)

更改viewpager.setOffscreenPageLimit(2)或默认情况下将其设置为1并检查加载时间,因为您不需要将每个片段保留在内存中,这会导致视图寻呼机中的加载响应变慢。

查看此内容,了解有关viewpager https://developer.android.com/reference/android/support/v4/view/ViewPager.html#setOffscreenPageLimit%28int%29中的setOffscreenPagLimit的更多信息