我有一个片段,我在其中插入带有视图寻呼机的标签布局。问题是当我从服务器获取onSuccess方法中的数据时,我的适配器不会更新数据。它在我的onSuccess方法被调用之前被初始化。如何告诉我的适配器重新考虑我的数据?我尝试了notifyDataSetChanged()
,但它不起作用。
我的片段
public class CinemaFragmentLandingPage extends BaseHomeFragment implements View.OnClickListener, TabLayout.OnTabSelectedListener, CinemasContract.View {
private CinemasContract.Presenter mPresenterCinemas;
private ViewPager mViewPager;
private TabLayout mTabLayout;
private CinemaFragmentTabAdapter mCinemaTabAdapter;
private List<CinemasModel> mDataCinemas;
public static CinemaFragmentLandingPage newInstance() {
return new CinemaFragmentLandingPage();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_cinema_landing, container, false);
mDataCinemas = new ArrayList<>();
mPresenterCinemas.getCinemas();
initViews(rootView);
return rootView;
}
private void initViews(View rootView) {
mTabLayout = rootView.findViewById(R.id.tab_layout);
mTabLayout.addTab(mTabLayout.newTab().setText(getResources().getString(R.string.cinemas)));
mTabLayout.addTab(mTabLayout.newTab().setText(getResources().getString(R.string.films)));
mViewPager = rootView.findViewById(R.id.pager);
mCinemaTabAdapter = new CinemaFragmentTabAdapter(this.getChildFragmentManager(), 2, mDataCinemas);
mViewPager.setAdapter(mCinemaTabAdapter);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
mTabLayout.setOnTabSelectedListener(this);
}
@Override
public void onClick(View view) {
}
@Override
public void onTabSelected(TabLayout.Tab tab) {
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
@Override
public void setLoading(boolean isActive) {
mHomeActivity.setLoadingDialog(isActive);
}
@Override
public void showGetCinemasSuccess(List<CinemasModel> cinemasModels) {
setLoading(false);
mDataCinemas = cinemasModels;
mViewPager.getAdapter().notifyDataSetChanged();
mViewPager.getAdapter().getItemPosition(0);
}
@Override
public void showGetCinemasFail(int errorCode) {
setLoading(false);
}
@Override
public void setPresenter(CinemasContract.Presenter presenter) {
mPresenterCinemas = Preconditions.checkNotNull(presenter);
}
}
我的适配器
public class CinemaFragmentTabAdapter extends FragmentStatePagerAdapter {
private int mNumOfTabs;
private ArrayList<CinemasModel> mCinemasData;
public CinemaFragmentTabAdapter(FragmentManager fm, int numOfTabs, List<CinemasModel> cinemasData) {
super(fm);
this.mNumOfTabs = numOfTabs;
this.mCinemasData = new ArrayList<CinemasModel>();
this.mCinemasData.addAll(cinemasData);
}
@Override
public Fragment getItem(int position) {
Fragment tab = null;
Bundle cinemasBundle = new Bundle();
cinemasBundle.putParcelableArrayList("cinemas", this.mCinemasData);
switch (position) {
case 0:
tab = new CinemaFragment();
tab.setArguments(cinemasBundle);
break;
case 1:
tab = new FilmsFragment();
tab.setArguments(cinemasBundle);
break;
default:
break;
}
return tab;
}
@Override
public int getCount() {
return mNumOfTabs;
}
}
任何帮助将不胜感激。
答案 0 :(得分:2)
首先在你的pojo类中定义构造函数,比如..
public CinemasModel(String title, String description, String imageUrl, String author, String url) {
this.title = title;
this.description = description;
this.imageUrl = imageUrl;
this.author = author;
this.url = url;
}
然后更新此代码。
CinemasModel cinemas= new CinemasModel(title, description, url, imageUrl, author);
mDataCinemas.add(cinemas);
System.out.println("mDataCinemas size = " + mDataCinemas.size());
mCinemaTabAdapter = new inemaFragmentTabAdapter(getActivity(), mDataCinemas);
mViewPager.setAdapter(mCinemaTabAdapter );
mCinemaTabAdapter.notifyDataSetChanged();
注意:在pojo类中更新您的参数而不是标题,描述等。我希望它对您有所帮助..
答案 1 :(得分:0)
mDataCinemas = cinemasModels;
到
mDataCinemas.clear();
mDataCinemas.addAll(cinemasModels);
并调用notifyDataSetChange。我认为这会对你有所帮助
答案 2 :(得分:0)
在viewpager适配器中使用一个参数作为ArrayList mCinemasData添加方法刷新,并在方法内添加2行以下 this.mCinemasData = mCinemasData; notifyDataSetChanged(); 从onsuccess调用方法,让我知道发生了什么?
答案 3 :(得分:0)
这样做:
mCinemaTabAdapter.notifyDataSetChanged();
mCinemaTabAdapter.getItemPosition(0);
而不是:
mViewPager.getAdapter().notifyDataSetChanged();
mViewPager.getAdapter().getItemPosition(0);
答案 4 :(得分:0)
在您的代码中执行此操作
adapter.notifyDataSetChanged();
adapter.getItemPosition(0);
它会帮助你。
答案 5 :(得分:0)
尝试在适配器和方法showGetCinemasSuccess(data){adapter.updateData(data)}中创建方法updateData(ArrayList mCinemasData) 和。之后的notifyDataSetChange。