如何从 Activity 通知片段中的适配器?

How to notify an Adapter in a Fragment from Activity?

我在 mainActivity 中有一个带有一些选项卡的 TabLayout,选项卡是片段。
在每个 Fragment 上我都有一个 ReclerView。
从主要 Activity 我想在我的片段中调用我的适配器上的 notifyDatasetChange() 以更新 UI.
但是怎么通知呢?

我的主要Activity:

     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) {

        }
    });

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);
}

卡适配器:

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);
    }
}
}

我建议在您的 Fragment 中放置一个 public 方法,然后从 Activity 调用。请在此处查看 "Deliver a Message to a Fragment":

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

在您的 Activity 中,只需执行以下操作以确保可以调用 Fragment 方法:

    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();
    }

然后在Fragment,有如下方法:

    public void yourMethod() { 
        recyclerViewAdapter.notifyDataSetChanged(); 
    }

你在片段中创建 updateListview() 就像

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

和 updateListview() 在你的 Activity 喜欢

updateListView();