How to notify an Adapter in a Fragment from Activity?
Asked Answered
M

2

7

I have a TabLayout in mainActivity with some tabs, tabs are Fragments.
And on each Fragment I have a ReclerView.
From the main Activity I want to call notifyDatasetChange() on my Adapter in my Fragment for updating the UI.
But how to notify it?

myMainActivity:

     final ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);


    TabLayout     tabLayout = (TabLayout) findViewById(R.id.tabLayout);
    tabLayout.addTab(tabLayout.newTab().setText("Tab1"));
    tabLayout.addTab(tabLayout.newTab().setText("Tab2"));


    final PagerAdapter adapter = new PagerAdapter
            (getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });

One of myTabFragment:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    RecyclerView v = (RecyclerView) inflater.inflate(R.layout.tab_fragment_1, container, false);
    SetupRecycleView(v);
    return v;
}


void SetupRecycleView(RecyclerView recList) {
    recList.setHasFixedSize(true);
    LinearLayoutManager llm = new LinearLayoutManager(G.context);
    llm.setOrientation(LinearLayoutManager.VERTICAL);
    //recList.setLayoutManager(new GridLayoutManager(G.context, 2));
    recList.setLayoutManager(llm);
    List<StructGhaza> ghazas = StructGhaza.getAllGhaza("2");
    CardAdapter adapter = new CardAdapter(ghazas);
    recList.setAdapter(adapter);
}

The CardAdapter:

public class CardAdapter extends     RecyclerView.Adapter<CardAdapter.GhazaViewHolder> {

private List<StructGhaza> ghazas;

public CardAdapter(List<StructGhaza> ghazaList) {
    this.ghazas = ghazaList;
}

@Override
public int getItemCount() {
    return ghazas.size();
}

@Override
public void onBindViewHolder(GhazaViewHolder ghazaViewHolder, int position) {

    StructGhaza ghaza = ghazas.get(position);
    ghazaViewHolder.vTitle.setText(ghaza.Name);
    //----Load Image
    Glide.with(G.context)
            .load(ghaza.Aks)
            .placeholder(R.drawable.loading_spinner)
            .crossFade()
            .into(ghazaViewHolder.itemImg);
    //---------------
    ghazaViewHolder.Rate.setRating(ghaza.Star);
    ghazaViewHolder.sampleLayout.setHoverView(ghazaViewHolder.hover);
    ghazaViewHolder.sampleLayout.addChildAppearAnimator(ghazaViewHolder.hover, R.id.tvHover, Techniques.FlipInX);
    ghazaViewHolder.sampleLayout.addChildDisappearAnimator(ghazaViewHolder.hover, R.id.tvHover, Techniques.FlipOutX);
}

@Override
public GhazaViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View itemView = LayoutInflater.
            from(viewGroup.getContext()).
            inflate(R.layout.fragment_carditem, viewGroup, false);

    return new GhazaViewHolder(itemView);
}

public static class GhazaViewHolder extends RecyclerView.ViewHolder {
    protected TextView vTitle;
    protected BlurLayout sampleLayout;
    protected View hover;
    protected ImageView itemImg;
    protected RatingBar Rate;

    public GhazaViewHolder(View v) {
        super(v);
        sampleLayout = (BlurLayout) v.findViewById(R.id.sample);
        itemImg = (ImageView) v.findViewById(R.id.item_img);
        hover = LayoutInflater.from(G.context).inflate(R.layout.fragment_hover, null);
        vTitle = (TextView) v.findViewById(R.id.titleTv);
        Rate = (RatingBar) v.findViewById(R.id.rating);
    }
}
}
Musky answered 5/8, 2015 at 12:27 Comment(2)
An Activity should know everything about any Fragments attached to it (the reverse is not true, however). Simply create a public method in your Fragment and call it from the Activity.Authority
something like method public void refreshData() { adapter.notifydatasetchanged(); } in your fragment. First find your fragment by tag name and then call this functionaFictive
B
15

I would recommend putting a public method in your Fragment and then calling from the Activity. See "Deliver a Message to a Fragment" here:

http://developer.android.com/training/basics/fragments/communicating.html

In your Activity, just do the following to make sure the Fragment method can be called:

    TabFragment tabFragment = (TabFragment) getSupportFragmentManager().findFragmentByTag(TAB_FRAGMENT_TAG);

        // Check if the tab fragment is available
        if (tabFragment != null) {

            // Call your method in the TabFragment
            articleFrag.yourMethod();
    }

And then in the Fragment, have the following method:

    public void yourMethod() { 
        recyclerViewAdapter.notifyDataSetChanged(); 
    }
Busybody answered 5/8, 2015 at 12:35 Comment(3)
where i can get TAB_FRAGMENT_TAG ?Consistency
@sahdevrajput74: that can just be any constant you define in your class. For example: private static final String TAB_FRAGMENT_TAG = "some tag name";Busybody
this sounds right but where do I put this function? The adapter is in onViewCreated and is a val not a var so I can't have it as a member of the fragment.Abadan
T
0

you create updateListview() in your fragment like

public void updateListView() {
    yourAdapter.notifyDataSetChanged();
}

and updateListview() in your Activity like

updateListView();
Tautog answered 7/9, 2016 at 11:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.