简体   繁体   中英

android -make view when viewpager get selected on PagerAdapter

I've a pagerAdapter in my application ,I want to call and get data when each row is selected and fill the data inside recycleview .

so far ,I've made my viewpager and made recycleview for each child , now , I want to call and get json when each view is selected. this is my code :

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

I've a method , getData that determine which position is selected, now I want to access its container and fill the data .

What should I do ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM