我有一个TabLayout
,其中的数据是从包含{"TOPS","LAUNDRY","BEDDING"...}
的网络服务中填充的。
此数据存储在传递到适配器的列表中。
myTabAdapter = new MyTabAdapter(getSupportFragmentManager(),tabListings);
viewpager.setAdapter(myTabAdapter);
tabList.setupWithViewPager(viewpager);
我的TabAdapter
。
public class MyTabAdapter extends FragmentStatePagerAdapter {
private ArrayList<TabListing> tabListings;
String item;
private Bundle bundle;
public MyTabAdapter(FragmentManager supportFragmentManager, ArrayList<TabListing> tabListings) {
super(supportFragmentManager);
this.tabListings = tabListings;
}
@Override
public Fragment getItem(int position) {
TabListing l = tabListings.get(position);
String a = l.getName();
bundle = new Bundle();
bundle.putString("item", a);
return OneFragment.newInstance(0, bundle);
}
@Override
public int getCount() {
return tabListings.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
TabListing l = tabListings.get(position);
String a = l.getName();
return a;
}
}
在getItem()
方法中,我使用bundle将片段传递给使用新实例方法的片段。
当数据进入像“TOPS”这样的片段时,它将在Firebase
的数据库参考字段中传递,以填充我的recyclerview
。
我的问题是当我运行应用程序时,在第一次运行中获取“LAUNDRY”而不是“TOPS”的数据。当我将2索引向前滑动到“BEDDING”时,数据将根据选项卡同步。我无法理解这是什么问题。
我的片段课。
public class OneFragment extends Fragment {
private static String a;
RecyclerView recyclerview;
AdapterToActivity act;
Bundle args;
ListingAdapter adapter;
FirebaseDatabase mDatabase;
DatabaseReference mReference;
ContentLoadingProgressBar pBar;
int position;
ArrayList<ItemList> itm = new ArrayList<>();
public OneFragment() {
}
public static OneFragment newInstance(int position, Bundle item) {
Bundle args = item;
a = args.getString("item");
OneFragment fragment = new OneFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_one, container, false);
recyclerview = v.findViewById(R.id.all_product_list);
pBar = v.findViewById(R.id.progress);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getActivity(), 1);
recyclerview.setLayoutManager(layoutManager);
adapter = new ListingAdapter(getActivity(),itm,a);
args = getArguments();
RecyclerViewAdapterMethod();
return v;
}
@Override
public void onStart() {
super.onStart();
pBar.show();
adapter.notifyDataSetChanged();
pBar.hide();
}
private void RecyclerViewAdapterMethod() {
pBar.show();
mDatabase = FirebaseDatabase.getInstance();
mReference = mDatabase.getReference(a);
mReference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
ItemList l;
l = dataSnapshot.getValue(ItemList.class);
itm.add(l);
recyclerview.setAdapter(adapter);
adapter.notifyDataSetChanged();
pBar.hide();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}