[英]how load fragment in ViewPager only when its selected
I'm using 3 Fragments
inside a Viewpager
, the problem is that I am loading big data in an Asynctask
and loaders. 我在Viewpager
使用了3个Fragments
,问题是我在Asynctask
和加载器中加载大数据。 On devices like HTC one
, it works fine, however, on low-end devices, it takes a lot of time. 在像HTC one
这样的设备上,它工作正常,但是,在低端设备上,它需要花费很多时间。 This is mainly because when I implement the pagerAdapter
, I put the Fragments inside an ArrayList
, this force the fragments instantiate when the main activity is loaded. 这主要是因为当我实现pagerAdapter
,我将Fragments放在ArrayList
,这会强制片段在加载主活动时实例化。 What I need is that it just "load" the first fragment (main) and when the user Swype
, load the other fragment. 我需要的是它只是“加载”第一个片段(main),当用户Swype
,加载另一个片段。 its any way to achieve this? 它的任何方式实现这一目标? this is my pageAdapater 这是我的pageAdapater
public class PagerAdapter extends FragmentPagerAdapter {
private final ArrayList<Fragment> mFragments = new ArrayList<Fragment>();
// private final ArrayList<String> titulos = new ArrayList<String>();
// private int NUM_PAGES =0;
public PagerAdapter(FragmentManager manager,int num_pages) {
super(manager);
// this.NUM_PAGES = num_pages;
}
public void addFragment(Fragment fragment,String title) {
mFragments.add(fragment);
notifyDataSetChanged();
}
@Override
public int getCount() {
//return NUM_PAGES;
return mFragments.size();
}
@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
}
The above method from Sun did not work for me (maybe it does for you), but I thought I would share my edit of his method also. 来自Sun的上述方法对我不起作用(也许它对你有用),但我想我也会分享我对他的方法的编辑。 Very good method by the way Sun! 太阳的方式非常好的方法!
private boolean _hasLoadedOnce= false; // your boolean field
@Override
public void setUserVisibleHint(boolean isFragmentVisible_) {
super.setUserVisibleHint(true);
if (this.isVisible()) {
// we check that the fragment is becoming visible
if (isFragmentVisible_ && !_hasLoadedOnce) {
new NetCheck().execute();
_hasLoadedOnce = true;
}
}
}
I'm gonna add my solution here since I faced a similar issue. 因为我遇到了类似的问题,所以我会在这里添加我的解决方案。 My asynchronous task wasn't loading huge amounts of data, but it prevents unnecessary network calls. 我的异步任务没有加载大量数据,但它可以防止不必要的网络调用。 Here's what I added in my Fragment: 这是我在片段中添加的内容:
private boolean _hasLoadedOnce= false; // your boolean field
@Override
public void setUserVisibleHint(boolean isFragmentVisible_) {
super.setUserVisibleHint(isVisibleToUser);
if (this.isVisible()) {
// we check that the fragment is becoming visible
if (!isFragmentVisible_ && !_hasLoadedOnce) {
//run your async task here since the user has just focused on your fragment
_hasLoadedOnce = true;
}
}
}
With the above code, your Fragment will be loaded, but your async task will not run until the user actually scrolls to the Fragment for the first time. 使用上面的代码,您的Fragment将被加载,但是您的异步任务将不会运行,直到用户第一次实际滚动到Fragment。 Once displayed, your async task will run for the first time automatically. 显示后,您的异步任务将自动第一次运行。 Then you can provide a way to load more data via a button or pull to refresh. 然后,您可以提供一种通过按钮加载更多数据或拉动刷新的方法。 The above Fragment was in my ViewPager and seemed to work fine. 上面的片段在我的ViewPager中,似乎工作正常。
Use fragmentStatePageAdapter if you have a lot of pages and you want to destroy them when not visible. 如果您有很多页面并且想要在不可见时销毁它们,请使用fragmentStatePageAdapter。 It has implemented a setMenuVisibility(boolean menuVisible) when fragment becomes visible, so use that. 当片段变得可见时,它已经实现了setMenuVisibility(布尔menuVisible),所以使用它。
I might be late for the party but here's my solution and it works as expected. 我可能会迟到了,但这是我的解决方案,它按预期工作。 In all of your child fragments create a boolean
variable: 在所有子片段中创建一个boolean
变量:
private boolean loadFragmentExecuted = false;
in the child fragments create a generic method called loadFragment
and move all of the logic you added in onCreateView
to that method: 在子片段中创建一个名为loadFragment
的泛型方法,并将您在onCreateView
添加的所有逻辑移动到该方法:
public void loadFragment()
{
if(!loadFragmentExecuted)
{
//Add your logic to manipulate the UI or load data etc...
loadFragmentExecuted = true;
}
}
in your pageview logic create the fragments dynamically like: 在您的网页浏览逻辑中动态创建片段,如:
//add the fragment
String fragmentName = "com.something." + fragmentId;
//check if the class exists
try
{
Class myFragmentClass = Class.forName(fragmentName);
Fragment myFragment = (Fragment) myFragmentClass.newInstance();
mFragments.add(myFragment);
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
catch (InstantiationException e)
{
e.printStackTrace();
}
then set your pager adapter and attach a tablayout with it: 然后设置你的寻呼机适配器并附上一个tablayout:
//set our pager adapter that contains different fragments
mPagerAdapter = new BasePagerAdapter(mFragmentManager, mFragments);
//link the adapter to the viewpager
mViewPager.setAdapter(mPagerAdapter);
//cache fragments
int limit = (mPagerAdapter.getCount() > 0 ? mPagerAdapter.getCount() : 1);
mViewPager.setOffscreenPageLimit(limit);
//add the page listner to the viewPager and link it to the tabLayout
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
//on tab selected select current viewpager item
mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener()
{
@Override
public void onTabSelected(TabLayout.Tab tab)
{
mViewPager.setCurrentItem(tab.getPosition());
//get fragment for the selected tab
Fragment f = mPagerAdapter.getItem(tab.getPosition());
//load the content of the fragment
try
{
Class c = f.getClass();
Method loadFragment = c.getMethod("loadFragment");
loadFragment.invoke(f);
}
catch (IllegalAccessException e){}
catch (InvocationTargetException e){}
catch (NoSuchMethodException e){}
}
@Override
public void onTabUnselected(TabLayout.Tab tab)
{
}
@Override
public void onTabReselected(TabLayout.Tab tab)
{
}
});
Slightly modified version to fix potential NPE caused by some views not fully initialised 一些修改后的版本修复了由于某些视图未完全初始化而导致的潜在NPE
private boolean _hasLoadedOnce= false; // your boolean field
@Override
public void setUserVisibleHint(boolean isFragmentVisible_) {
super.setUserVisibleHint(isVisibleToUser);
if (this.isVisible()) {
// we check that the fragment is becoming visible
if (!isFragmentVisible_ && !_hasLoadedOnce) {
new Handler().post(() -> {
makeAsyncRequest();//do your asyn stuffs
_hasLoadedOnce = true;
});
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.