在PagerAdapter上选择viewpager时的android -make视图

时间:2016-09-07 05:15:12

标签: android android-viewpager android-recyclerview android-tabs

我的应用程序中有一个pagerAdapter,我想在选择每一行时调用并获取数据并填充recycleview中的数据。

到目前为止,我已经制作了我的viewpager并为每个孩子制作了recycleview,现在,我想在选择每个视图时调用并获取json。这是我的代码:

private static class CheesePagerAdapter extends PagerAdapter {
    private final ArrayList<CharSequence> mCheeses = new ArrayList<>();

    public void addTab(String title) {
        mCheeses.add(title);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mCheeses.size();
    }

    @Override
    public int getItemPosition(Object object) {
        final Item item = (Item) object;
        final int index = mCheeses.indexOf(item.cheese);
        return index >= 0 ? index : POSITION_NONE;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        final RecyclerView rc=new RecyclerView(container.getContext());
        RtlGridLayoutManager mLayoutManager = new RtlGridLayoutManager(container.getContext()
                , numColumns);
        rc.setLayoutManager(mLayoutManager);

        container.addView(rc, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);

        Item item = new Item();
        item.cheese = mCheeses.get(position);
        item.view = rc;
        return item;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        final Item item = (Item) object;
        return item.view == view;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mCheeses.get(position);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        final Item item = (Item) object;
        container.removeView(item.view);
    }

    public void getData(String id) {

    }

    private static class Item {
        RecyclerView view;
        CharSequence cheese;
    }
}

我有一个方法,getData确定选择了哪个位置,现在我想访问它的容器并填充数据。

我该怎么办?

1 个答案:

答案 0 :(得分:0)

在getData尝试在recyclerView的适配器中设置数据并为recyclerview和pageadapter的适配器调用 notifyDataSetChanged()以告知数据已更改