我在mainActivity中有一个TabLayout,有一些标签,标签是片段
在每个片段上我都有一个ReclerView
从主活动我想在我的片段中的我的适配器上调用notifyDatasetChange()
来更新UI
但是如何通知呢?
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) {
}
});
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);
}
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);
}
}
}
答案 0 :(得分:11)
我建议您在Fragment
中使用公开方法,然后从Activity
拨打电话。请参阅&#34;向片段发送消息&#34;在这里:
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();
}
答案 1 :(得分:0)
在片段中创建updateListview(),如
public void updateListView() {
yourAdapter.notifyDataSetChanged();
}
您的活动中的和updateListview()
updateListView();