我有一个带有5个标签的viewpager,每个标签都是向用户显示不同数据的片段,每个片段本身都有recycleview,我需要在用户更改标签时将更新的数据重新插入到recyclerview中,我需要更新视图
每次我滑动到选项卡时,都需要加载该选项卡中片段的新实例,以便更新数据。
我试过了:
1-我使用了FragmentStatePagerAdapter(没有任何反应)
2- I覆盖getItemPosition方法以返回PagerAdapter.POSITION_NONE; (没有任何反应)
3- setOffscreenPageLimit(0)(没有任何反应)
每个片段内的4-刷新函数被调用onTabSelected (给出nullPointerException)
每次用户更改选项卡时,如何从头开始重新创建片段 我真的需要知道。
我一直在努力解决这个问题一周没有进展。我感谢任何帮助,无论是提示,评论还是建议。
让我知道我应该发布什么代码,谢谢。
编辑: 我的代码:
MainActivity:
public class Sefaresh_activity3 extends AppCompatActivity implements Serializable, Sefaresh3_interface {
Context context;
Toolbar mtoolbar;
private List<Food> food_list = new ArrayList<>();
MyPagerAdapter adapter;
Custom_TabLayout mTabLayout;
ViewPager mpager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sefaresh_activity3);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_LTR);
}
context = this;
sqlHelper = new SqlHelper(this);
setupTabs();
private void setupTabs() {
mtoolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(mtoolbar);
SpannableString s = new SpannableString(getTitle());
s.setSpan(new TypefaceSpan(this, "IRAN Sans Bold.ttf"), 0, s.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
setTitle(s);
try {
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
} catch (Exception e) {
L.m("something is wrong in toolbar section");
}
adapter = new MyPagerAdapter(getSupportFragmentManager());
mTabLayout = (Custom_TabLayout) findViewById(R.id.tab_layout);
mpager = (ViewPager) findViewById(R.id.viewpager);
mpager.setAdapter(adapter);
mTabLayout.setTabsFromPagerAdapter(adapter);
mTabLayout.setupWithViewPager(mpager);
mpager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
L.m("tab selected" + tab.getPosition());
mpager.setCurrentItem(tab.getPosition());
// Sefaresh3_fragment all_fragment = (Sefaresh3_fragment) adapter.getFragment_all();
// sefaresh3_fragment.onRefresh();
// Sefaresh3_fragment sefaresh3_fragment = (Sefaresh3_fragment) adapter.fragment_all;
// adapter.notifyDataSetChanged();
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
class MyPagerAdapter extends FragmentStatePagerAdapter {
// public ArrayList<Fragment> fragmentArrayList = new ArrayList<>();
Fragment fragment_all;
Fragment fragment_pizza;
Fragment fragment_sandwich;
Fragment fragment_salad;
Fragment fragment_coca;
public Fragment getFragment_coca() {
return fragment_coca;
}
public Fragment getFragment_all() {
return fragment_all;
}
public Fragment getFragment_pizza() {
return fragment_pizza;
}
public Fragment getFragment_sandwich() {
return fragment_sandwich;
}
public Fragment getFragment_salad() {
return fragment_salad;
}
String[] tabs = {
"products",
"new products",
"featured",
"star",
"star2"
};
@Override
public int getItemPosition(Object object) {
return PagerAdapter.POSITION_NONE;
}
public MyPagerAdapter(FragmentManager fm) {
super(fm);
tabs = getResources().getStringArray(R.array.sefaresh3_tabs);
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
@Override
public Fragment getItem(int num) {
Fragment fragment = null;
orders = new Orders();
orders.setOrders(all_orders);
switch (num) {
case 0:
// fragment_all = Sefaresh3_fragment.newInstance("all", "");
// return new Sefaresh3_fragment().newInstance("all","");
return Sefaresh3_fragment.newInstance("all","");
case 1:
fragment_pizza = Sefaresh3_fragment.newInstance("pizza", "");
return fragment_pizza;
case 2:
fragment_sandwich = Sefaresh3_fragment.newInstance("sandwich", "");
return fragment_sandwich;
case 3:
fragment_salad = Sefaresh3_fragment.newInstance("salad", "");
return fragment_salad;
case 4:
fragment_coca = Sefaresh3_fragment.newInstance("coca", "");
return fragment_coca;
default:
return fragment;
}
}
@Override
public CharSequence getPageTitle(int position) {
return tabs[position];
}
@Override
public int getCount() {
return 5;
}
}
答案 0 :(得分:0)
感谢大家的建议,经过一周的努力解决这个问题,我找到了解决方案。
我尝试了几种方法,但最后我找到了解决方案。
我使用Broadcast接收器告诉片段更新列表,每次我需要更新片段来更新数据列表时,我都会给广播公司打电话。它很棒,效果很好
如果有人遇到同样的问题我会很乐意帮助他:)谢谢。