У меня есть некоторые проблемы, связанные с динамическими c вкладками и динамическими c данными в его фрагменте. Я загружаю вкладки фрагментов из JSON и получаю идентификатор для фрагмента каждой вкладки и вызываю другой API для содержимого.
Проблема заключается в том, что при нажатии на вкладку или пролистывание новые данные загружаются во фрагмент, но последние данные последней идентификатор все еще там в фоновом режиме. Я прилагаю скриншот.
TabActivity
public class NewsFeed extends Fragment {
private static RecyclerView listView;
private static ArrayList<String> CatIdList = new ArrayList<>();
private static ArrayList<Fragment> FragmentList = new ArrayList<>();
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
private TabLayout tabLayout;
private ViewPager viewPager;
private RecyclerView recyclerView;
private ApiInterface apiInterface;
private List<BlogCatData> list = new ArrayList<BlogCatData>();
private BlogCatListAdaptor adapter;
private String item;
private ArrayList<BlogCatData> dataModelList = new ArrayList<BlogCatData>();
private ArrayList<String> CatTitleList = new ArrayList<>();
private View view1;
private View v;
private SectionsPagerAdapter sectionsPagerAdapter;
public NewsFeed() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
v = inflater.inflate(R.layout.fragment_news_feed, container, false);
tabLayout = v.findViewById(R.id.tabs);
viewPager = v.findViewById(R.id.container);
getAllBlogCat();
return v;
}
private void getAllBlogCat() {
// avi.show();
ProgressDialog progress = new ProgressDialog(getActivity());
progress.setMessage("Loading... Please wait");
progress.setCancelable(false); // disable dismiss by tapping outside of the dialog
progress.show();
apiInterface = APIClient.getClient().create(ApiInterface.class);
Call<BlogCatModel> call = apiInterface.GETBlogCategory();
call.enqueue(new Callback<BlogCatModel>() {
@Override
public void onResponse(Call<BlogCatModel> call, retrofit2.Response<BlogCatModel> response) {
if (response.isSuccessful()) {
progress.cancel();
CatIdList.clear();
list = response.body().getDATA();
for (int i = 0; i < list.size(); i++) {
CatTitleList.add(list.get(i).getTITLE());
FragmentList.add(new NewsFeedListFragment(getActivity(), list.get(i).getID()));
}
setUpViewPager(viewPager);
Log.v("CatTitle", CatTitleList.toString());
Log.v("CatId", CatIdList.toString());
} else {
progress.cancel();
try {
JSONObject jObjError = new JSONObject(response.errorBody().string());
Toast.makeText(getApplicationContext(), jObjError.getString("message"), Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
@Override
public void onFailure(Call<BlogCatModel> call, Throwable t) {
progress.cancel();
Toast.makeText(getApplicationContext(), t.toString(), Toast.LENGTH_SHORT).show();
call.cancel();
}
});
}
private void setUpViewPager(ViewPager viewPager) {
tabLayout.setupWithViewPager(viewPager);
sectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager(), FragmentList, CatTitleList);
sectionsPagerAdapter.notifyDataSetChanged();
viewPager.setOffscreenPageLimit(CatIdList.size());
viewPager.setAdapter(sectionsPagerAdapter);
}
public static class PlaceholderFragment extends Fragment {
private final static String TAG = PlaceholderFragment.class.getSimpleName();
private static String item;
private static ArrayList<String> dataModelList = new ArrayList<String>();
private static String id;
private RecyclerView listView;
private List<BlogData> dataList = new ArrayList<>();
private BlogListAdaptor blogListAdaptor;
public PlaceholderFragment() {
}
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment__newsfeed_placeholder, container, false);
listView = view.findViewById(R.id.recycler_view);
String Id = getArguments().getString("id");
getBlog(Id);
setBlog();
return view;
}
private void getBlog(String id) {
ApiInterface apiInterface = APIClient.getClient().create(ApiInterface.class);
Call<BlogModel> call = apiInterface.getNewsByCategory(id);
call.enqueue(new Callback<BlogModel>() {
@Override
public void onResponse(Call<BlogModel> call, retrofit2.Response<BlogModel> response) {
if (response.isSuccessful()) {
dataList = response.body().getDATA();
blogListAdaptor.setPostList(dataList);
} else {
try {
JSONObject jObjError = new JSONObject(response.errorBody().string());
Toast.makeText(getApplicationContext(), jObjError.getString("message"), Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
@Override
public void onFailure(Call<BlogModel> call, Throwable t) {
// avi.hide();
Toast.makeText(getApplicationContext(), t.toString(), Toast.LENGTH_SHORT).show();
call.cancel();
}
});
}
private void setBlog() {
blogListAdaptor = new BlogListAdaptor(getActivity(), dataList);
listView.setAdapter(blogListAdaptor);
listView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
listView.setLayoutManager(linearLayoutManager);
}
}
}
Адаптер Sectionpager
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private ArrayList<Fragment> mFragmentList;
private ArrayList<String> mFragmentTitleList;
public SectionsPagerAdapter(FragmentManager fm, ArrayList<Fragment> mFragmentList, ArrayList<String> mFragmentTitleList) {
super(fm);
this.mFragmentList = mFragmentList;
this.mFragmentTitleList = mFragmentTitleList;
}
@NotNull
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return (mFragmentList == null) ? 0 : mFragmentList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
@Override
public int getItemPosition(Object object) {
return super.getItemPosition(object);
}
}
Фрагмент
public class NewsFeedListFragment extends Fragment {
private List<BlogData> dataList = new ArrayList<>();
private BlogListAdaptor blogListAdaptor;
private RecyclerView listView;
private String id;
private Activity activity;
private String Id;
public NewsFeedListFragment(Activity activity, String Id) {
// Required empty public constructor
this.activity = activity;
this.id = Id;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_news_feed_list, container, false);
listView = v.findViewById(R.id.recycler_view);
getBlog();
setBlog();
return v;
}
private void getBlog() {
ProgressDialog progress = new ProgressDialog(getActivity());
progress.setMessage("Loading... Please wait");
progress.setCancelable(false); // disable dismiss by tapping outside of the dialog
progress.show();
ApiInterface apiInterface = APIClient.getClient().create(ApiInterface.class);
Call<BlogModel> call = apiInterface.getNewsByCategory(id);
call.enqueue(new Callback<BlogModel>() {
@Override
public void onResponse(Call<BlogModel> call, retrofit2.Response<BlogModel> response) {
if (response.isSuccessful()) {
progress.cancel();
dataList = response.body().getDATA();
blogListAdaptor.setPostList(dataList);
} else {
progress.cancel();
try {
JSONObject jObjError = new JSONObject(response.errorBody().string());
Toast.makeText(getApplicationContext(), jObjError.getString("message"), Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
@Override
public void onFailure(Call<BlogModel> call, Throwable t) {
progress.cancel();
Toast.makeText(getApplicationContext(), t.toString(), Toast.LENGTH_SHORT).show();
call.cancel();
}
});
}
private void setBlog() {
blogListAdaptor = new BlogListAdaptor(getActivity(), dataList);
listView.setAdapter(blogListAdaptor);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
listView.setLayoutManager(linearLayoutManager);
}
}