如何防止Fragment每次从服务器加载数据时在Android中的TabLayout中滑动Tabs?

时间:2016-04-08 11:30:43

标签: android android-fragments android-tablayout

我是Fragments的新手。我的Fragments中有5个不同的TabLayout。我在每个片段中从服务器加载数据。

我的问题如下: 当第一个片段加载时,它会调用该函数并从服务器加载数据并显示在RecyclerView上。

然后当我滑动到第二个选项卡时,服务器数据会加载,依此类推。但是,当我再次滑动说出片段1时,数据再次从服务器加载,并且需要再次设置RecyclerView

无论如何我只能加载服务器数据一次(不保存到本地数据库)并只设置recyclerview一次,这样无论我刷了多少次数据都没有加载数据再次?我已经提到了here,但我真的不明白如何解决我的问题。我正在发布我的代码片段。

TabActivity

public class TabActivity extends AppCompatActivity {
ViewPager viewpager;
Toolbar toolbar;
TabLayout tablayout;
ViewPagerAdapter viewPagerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab_activity);
    initialize();




}

private void initialize() {
    viewpager = (ViewPager) findViewById(R.id.viewpager);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    tablayout = (TabLayout) findViewById(R.id.tablayout);
    setSupportActionBar(toolbar);
    viewPagerAdapter=new ViewPagerAdapter(getSupportFragmentManager());
    viewpager.setAdapter(viewPagerAdapter);
    tablayout.setTabsFromPagerAdapter(viewPagerAdapter);
    tablayout.setupWithViewPager(viewpager);

}


}

 class ViewPagerAdapter extends FragmentStatePagerAdapter implements ViewPager.OnPageChangeListener {

public ViewPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:return LocatedEvents.newInstance();

        case 1:return QrCodeScanner.newInstance();
        case 2:return NewsFeed.newInstance();

        case 3:return CreatePoll.newInstance();
        case 4:return MyProfile.newInstance();
    }
    return NewsFeed.newInstance();
}

@Override
public int getCount() {
    return 5;
}
@Override
public CharSequence getPageTitle(int position) {
    return "Tab " + (position + 1);
}

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override
public void onPageSelected(int position) {

}

@Override
public void onPageScrollStateChanged(int state) {

}
}

NewsFeed Fragment

public class NewsFeed extends Fragment {
@Nullable

public static NewsFeed newInstance() {
    return new NewsFeed();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.newsfeed_fragment, container, false);
    load_data();
    return view;
}
}

1 个答案:

答案 0 :(得分:0)

每次在

中避免创建新的碎片
@Override
public Fragment getItem(int position) {
 switch (position) {
  case 0:
   return LocatedEvents.newInstance();

  case 1:
   return QrCodeScanner.newInstance();
  case 2:
   return NewsFeed.newInstance();

  case 3:
   return CreatePoll.newInstance();
  case 4:
   return MyProfile.newInstance();
 }
 return NewsFeed.newInstance();
}

创建一个片段数组并将其传递给您的Adapter.Something like this

@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_dashboard);

 initFragments();

}

private void initFragments() {

  ArrayList < Fragment > fragmentArrayList = new ArrayList < > ();

  fragmentArrayList.add(LocatedEvents.newInstance());
  fragmentArrayList.add(QrCodeScanner.newInstance());
  fragmentArrayList.add(NewsFeed.newInstance());
  fragmentArrayList.add(MyProfile.newInstance();

   viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), fragmentArrayList); viewpager.setAdapter(viewPagerAdapter); tablayout.setTabsFromPagerAdapter(viewPagerAdapter); tablayout.setupWithViewPager(viewpager);
  }

在适配器中将其更改为

private ArrayList < Fragment > fragments;

  public ViewPagerAdapter(FragmentManager fm, ArrayList < Fragment > fragments) {
   super(fm);
   this.fragments = fragments;
  }

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

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