2

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. enter image description here

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.
enter image description here

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 .

enter image description here

Shivam Kumar
  • 1,892
  • 2
  • 21
  • 33
visakh r
  • 175
  • 1
  • 11
  • I'm not sure I understand, which part of code is for the new data that you aren't seeing in your view? Is that the planList? Can you give example of what you see and what you expect to see maybe? – Macmist Sep 25 '18 at 11:00
  • Something is missing here. You have `HomeFragment` and inside it `ViewPager` which contains `SecondFragment` and what is used as `FirstFragment` or whatever you call it? In `TabPagerAdapter` count is basically `3` (starting from 0) but you have just one `fragment`? – Yupi Sep 25 '18 at 11:08
  • In my SecondFragment, I am adding data models to ArrayList named planList, and then the planList is send to customAdapter class. I check the values adding to planList on each tab change and found its coreect.But the newly added values to planList is not updating to RecyclerView instead it shows previous tabs values. I will edit my question to add the custom adapter class too – visakh r Sep 25 '18 at 11:10
  • @Yupi, sorry, actually that part doesn't have any role, i removed it. – visakh r Sep 25 '18 at 11:24
  • Your question is still confusing and approach as well. Why you just didn't create two `fragments` and assign them to `ViewPager` using `adapter`? In each you can than have `recyclerview` and implement logic to download desired data from internet. – Yupi Sep 25 '18 at 22:37
  • @yupi, fragment named SecondFragment is inside HomeFragment. What i need is, when tabs in homefragment changes an API corresponding to that tab is called and that response data should populate the RecyclerView in the SecondFragment. The number of tabs in HomeFragment changes always, so i can't use multiple fragment, i want to use singe fragment, SecondFragment for all tabs to show in ViewPager. – visakh r Sep 26 '18 at 06:01
  • @Yupi, I have added screenshots for better analysis of the issue. – visakh r Sep 26 '18 at 12:06
  • @visakhr do you have somewhere code stored like Bitbucket or Github, where is possible to take a look on whole code? – Yupi Sep 26 '18 at 13:12
  • No, actually i have added all codes in the question itself – visakh r Sep 27 '18 at 03:46

0 Answers0