从tablayout,android获取标签或id的片段

时间:2015-08-08 07:35:33

标签: android android-fragments android-adapter

我使用this tutorial创建标签。 现在我在每个片段中都有一个recyclerview,我想通过mainactivity通知他们。如何在片段中访问适配器?

我用它来调用片段中的refreshData()方法:

  TabFragment1 tabFragment = (TabFragment1) getSupportFragmentManager().findFragmentByTag(TabFragment1.class.getSimpleName());
 if (tabFragment != null) {
                    tabFragment.refreshData();
                } 

但返回null。

mainActivity:

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

    }
});

PagerAdapter:

public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;

public PagerAdapter(FragmentManager fm, int NumOfTabs) {
    super(fm);
    this.mNumOfTabs = NumOfTabs;
}

@Override
public Fragment getItem(int position) {

    switch (position) {
        case 0:
            TabFragment1 tab1 = new TabFragment1();
            return tab1;
        case 1:
            TabFragment2 tab2 = new TabFragment2();
            return tab2;
        case 2:
            TabFragment3 tab3 = new TabFragment3();
            return tab3;
        case 3:
            TabFragment4 tab4 = new TabFragment4();
            return tab4;
        default:
            return null;
    }
}

@Override
public int getCount() {
    return mNumOfTabs;
}
}

myTabFragment之一:

 public CardAdapter adapter;
       @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;
    }
  public void refreshData() {
        adapter.notifyDataSetChanged();

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

1 个答案:

答案 0 :(得分:5)

修改 经过多次测试,之前的答案适用于Android&lt; 21,但对于Android&gt; 21它有点奇怪,它似乎没有以同样的方式工作。

最后,我解决了这个问题,它无处不在:

在适配器中:

    HashMap<Integer, Fragment> mPageReferenceMap = new HashMap<>();
@Override
public Object instantiateItem(ViewGroup container, int position) {
    Fragment fragment = (Fragment) super.instantiateItem(container, position);
    mPageReferenceMap.put(position, fragment);
    return fragment;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    super.destroyItem(container, position, object);
    mPageReferenceMap.remove(position);
}
public Fragment getFragment(int key) {
    return mPageReferenceMap.get(key);
}

你可以在活动中调用getFragment:

int index = viewPager.getCurrentItem();
ViewPagerAdapter adapter = ((ViewPagerAdapter)viewPager.getAdapter());
final YourFragment tabFragment = (YourFragment) adapter.getFragment(index);

以前的答案:

警告:它并不适用于Android&gt; 21

我遇到了同样的问题。最后,我没有使用id或tag。

而不是使用:

CustomFragment customFragment = (CustomFragment) getSupportFragmentManager().findFragmentByTag(getString(R.string.fragment_custo_tag));

使用它:

CustomFragment customFragment = (CustomFragment) getSupportFragmentManager().getFragments().get(0);

获取CustomFragment的实例。

我已将get(0)设置为CustomFragment实例的位置为零。

我在这里发现了这个问题: Set and get using tag a fragment in android