I have a Fragment
named HomeFragment
, consisting of a TabLayout
and ViewPager
in it. TabLayout
is populated with data from backend. So each tab item has a name and on changing the tab position, I fetch data from backend and add another fragment, named SecondFragment
to the ViewPager
. The SecondFragment
consists of RecyclerView
and I am populating it with data from backend. What I need is when tabs changes then an url
corresponding to it is called and the response data should updated to RecyclerView
in SecondFragment
within the ViewPager
.
What I have did so far is as following:
Populating TabLayout
:
mViewPager= (ViewPager)rootview.findViewById(R.id.viewpager);
mTabLayout= (TabLayout)rootview.findViewById(R.id.tabs);
HomeModel model;
planList = new ArrayList<>();
adapter = new ViewPagerAdapter(getChildFragmentManager());
try {
jsonArray = resultData.getJSONArray("details");
editor.putString("tab",jsonArray.getJSONObject(0).getString("name"));
editor.commit();
for(int i=0; i<jsonArray.length();i++){
jsonObject2 = jsonArray.getJSONObject(i);
model = new HomeModel();
model.setCatid(jsonObject2.getString("id"));
model.setImage(jsonObject2.getString("image"));
model.setName(jsonObject2.getString("name"));
planList.add(model);
setupViewPager(mViewPager,jsonObject2.getString("name"));
}
setAdapter();
} catch (JSONException e) {
e.printStackTrace();
}
private void setAdapter() {
mViewPager.setAdapter(adapter);
mTabLayout.setupWithViewPager(mViewPager);
}
private void setupViewPager(ViewPager mViewPager,String name) {
adapter.addFragment(new SecondFragment(), name);
}
TabsSelectListener
:
mTabLayout.setOnTabSelectedListener(
new TabLayout.ViewPagerOnTabSelectedListener(mViewPager) {
@Override
public void onTabSelected(TabLayout.Tab tab) {
super.onTabSelected(tab);
numTab = tab.getPosition();
HomeModel browsePlan = planList.get(numTab);
editor.putString("tab", browsePlan.getName()).apply();
}
});
In my SecondFragment
I am just
populating data from to Recyclerview
using a custom adapter class:
SecondFragment
code snippet:
planList = new ArrayList<>();
try {
jsonArray = resultData.getJSONArray("products");
for(int i=0; i<jsonArray.length();i++){
jsonObject2 = jsonArray.getJSONObject(i);
model = new Spaceship();
model.setName(jsonObject2.getString("package_name"));
model.setImage(jsonObject2.getString("images"));
planList.add(model);
}
setAdapter();
} catch (JSONException e) {
e.printStackTrace();
}
adding to adapter:
private void setAdapter() {
adapter = new RecyclerHomeAdapter(getContext(), planList);
rv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
Custom Adapter class
public class RecyclerHomeAdapter extends RecyclerView.Adapter<RecyclerHomeAdapter.MyHolder> {
private ArrayList<Spaceship> spaceships;
private Context c;
public RecyclerHomeAdapter(Context c, ArrayList<Spaceship> spaceships) {
this.spaceships = spaceships;
this.c = c;
}
@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v= LayoutInflater.from(c).inflate(R.layout.home_package,parent,false);
return new MyHolder(v);
}
@Override
public void onBindViewHolder(MyHolder holder, int position) {
Spaceship s=spaceships.get(position);
holder.nameTxt.setText(s.getName());
Glide.with(c).load(s.getImage())
.thumbnail(0.5f)
.crossFade()
.fitCenter()
.placeholder(R.drawable.loading_image)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(holder.img);
}
@Override
public int getItemCount() {
return spaceships.size();
}
class MyHolder extends RecyclerView.ViewHolder
{
TextView nameTxt;
ImageView img;
public MyHolder(View itemView) {
super(itemView);
nameTxt= (TextView) itemView.findViewById(R.id.nameTxt);
img= (ImageView) itemView.findViewById(R.id.spacecraftImage);
}
}
}
Adding tabs, showing SecondFragment
inside ViewPager
with populated RecyclerView
are working.
My problem is when changing tabs. I have logged and found that I am getting correct data inside SecondFragment
on changing each tabs, but that data not updating to RecyclerView
. When tabs changes RecyclerView
shows previous data.
How can I make it works fine on tabs change, i.e update RecyclerVierw
with data I am getting on adding each instance of SecondFragment
to ViewPager
?
All responses are appreciated.
Adding some screenshots for identifying my issue.
I have multiple tabs,lotto,cricket,football etc.. as shown in the image.
When each tab is selected I am fetching data from backend and populate it to a RecyclerView in anotherFragment "SecondFragment", which is added to the ViewPager below the TabLayout, as shown in the image.
But when i swipe to next fragment tab changes to cricket, and data from backend corresponding to "cricket" is fetched in the "SecondFragment", but it not updating the RecyclerView
, as shown in the second image.
And again, on swipe to the next tab "football", data corresponding to "cricket" is showing and so on as shown in image 3, and then swiping back to first tab, it shows data corresponding to any other tab.
I don't know why its not get updated .