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);
    }
}
}
                Intent intent = new Intent(this, mFragmentFavorite. class); startActivity(intent); If you want to start aFavorite instead of mFragmentFavorite then you only need to change out their names in the created Intent .
To pass data from one fragment to another Bundle will help. LifeShapeDetailsFragment fragment = new LifeShapeDetailsFragment(); // object of next fragment Bundle bundle = new Bundle(); bundle. putInt("position", id); fragment. setArguments(bundle);
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(); 
    }
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With