如何使用ViewPager在Android的TabLayout中预加载选项卡->] [[

问题描述 投票:-1回答:1
我有一个图库图库,是在主要活动中从Uris添加到图片库的,但是直到打开标签页,页面才会加载。我试图使用ViewPager在TabLayout中预加载图库页面,所以打开标签时它不会滞后。

我在android studio中了解了Loaders,但在API 28之后已弃用。它说使用ViewModels和LiveData的组合。https://developer.android.com/guide/components/loaders

我也找到了这篇文章,并在我的画廊类中重写了setUserVisibleHint方法,但这不起作用。

TabLayout Preload next tab

这是我的代码:

主要活动:

tabLayout = findViewById(R.id.tabLayout); pageAdapter = new PageAdapter(getSupportFragmentManager(), 3, getApplicationContext()); final NonSwipeableViewPager viewPager = findViewById(R.id.viewPager); viewPager.setAdapter(pageAdapter); TabLayout tabs = findViewById(R.id.tabLayout); tabs.setupWithViewPager(viewPager); tabs.getTabAt(0).setIcon(R.drawable.location).getIcon().setColorFilter(Color.CYAN, PorterDuff.Mode.SRC_ATOP); tabs.getTabAt(1).setIcon(R.drawable.list).getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);; tabs.getTabAt(2).setIcon(R.drawable.gallery).getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP); tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { tab.getIcon().setColorFilter(Color.CYAN, PorterDuff.Mode.SRC_ATOP); } @Override public void onTabUnselected(TabLayout.Tab tab) { tab.getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP); } @Override public void onTabReselected(TabLayout.Tab tab) { } });

页面适配器:

public class PageAdapter extends FragmentPagerAdapter { private int numOfTabs; private Context context; private static final int[] TAB_TITLES = new int[]{R.string.tab_text_1, R.string.tab_text_2, R.string.tab_text_3}; public PageAdapter(@NonNull FragmentManager fm, int numOfTabs, Context context) { super(fm); this.numOfTabs = numOfTabs; this.context = context; } @NonNull @Override public Fragment getItem(int position) { switch (position){ case 0: return new MapsActivity(); case 1: return new PreviousTrips(); case 2: return new Gallery(); default: return null; } } @Override public int getCount() { return numOfTabs; } @Nullable @Override public CharSequence getPageTitle(int position) { return context.getResources().getString(TAB_TITLES[position]); } }

NonSwipeableViewPager:

public class NonSwipeableViewPager extends ViewPager { public NonSwipeableViewPager(Context context) { super(context); setMyScroller(); } public NonSwipeableViewPager(Context context, AttributeSet attrs) { super(context, attrs); setMyScroller(); } @Override public boolean onInterceptTouchEvent(MotionEvent event) { // Never allow swiping to switch between pages return false; } @Override public boolean onTouchEvent(MotionEvent event) { // Never allow swiping to switch between pages return false; } private void setMyScroller() { try { Class<?> viewpager = ViewPager.class; Field scroller = viewpager.getDeclaredField("mScroller"); scroller.setAccessible(true); scroller.set(this, new MyScroller(getContext())); } catch (Exception e) { e.printStackTrace(); } } public class MyScroller extends Scroller { public MyScroller(Context context) { super(context, new DecelerateInterpolator()); } @Override public void startScroll(int startX, int startY, int dx, int dy, int duration) { super.startScroll(startX, startY, dx, dy, 350 /*1 secs*/); } } }

我有一个图库图库,是在主要活动中从Uris添加到图片库的,但是直到打开标签页,页面才会加载。我正在尝试使用ViewPager在TabLayout中预加载图库页面,所以...
android android-viewpager android-tablayout preload android-pageradapter
1个回答
0
投票
我设法用以下代码预加载了标签:
© www.soinside.com 2019 - 2024. All rights reserved.