Viewpager not getting last item

狂风中的少年 提交于 2019-12-03 16:20:09

First, as mentioned by @NotobharatKumar, you're loading the second adapter on a wrong event of the parent adapter, ie in onPageScrolled method. Second, you're setting a new adapter each time on that specific event, it seems useless. Finally, you are setting datas in an event, (I'm not sure why you're doing this but) I'd prefer to set it in a separate adapter and let the events listener for specific behaviors.

For me, it seems that you have two separate adapters, but one datas list shared by both. Assuming that, you have to set them both at start with their datas respectly, and on the event onPageSelected of the top adapter, you just have to automatically scroll the second. And if they have the same position in the list, onPageSelected should do the work correctly.


So, these modifications should solve your issue:

Your code in onScrollChanged, when you set the image and the text, seems really weird to me. I'd use a first adapter where I'll set all the datas like these two for the first ViewPager:

@Override
public void onCreate(Bundle savedInstansteState) {
    ...
    // set a simple adapter for the first ViewPager
    FirstPagerAdapter imageadapter = 
             new FirstPagerAdapter(ProductLandingActivity.this, categorylist);
    pagerimages.setAdapter(imageadapter);
    ...
}

Then, as usual, set your datas in the FirstPagerAdapter:

@Override
public View getView(int position, View view, ViewGroup container) {
    ...
    // set the content
    Picasso.with(ProductLandingActivity.this)                            
        .load(categorylist.get(position).getProductLanding_packLink())
        .error(R.drawable.nopreview )
        .placeholder(R.drawable.progress_animation)
        .into(selectedImage);

    selectedname.setText(
            categorylist.get(position).getProductLanding_packDesc());
    ...
}

Then no need to (re)load the image or the text when an event is triggered, since they will be holding by the adapter.

You only use getPackSize_packSize() and getPackSize_sellingPrice() in the second adapter, so you should create a separate list to only fill with these datas but outside the swiping event. Start by initializing the second list and the adapters:

// get the Items to fill the pack items list (like you did for `temp`)
ArrayList<Items> packItems = new ArrayList<>();
for (int i = 0; i < categorylist.size(); i++) {
    packItems.add(categorylist.get(position).getItems());
}

// fill the first adapter with your list of products (ie categorylist)
...
pagerimages.setAdapter(imageadapter);
...
// fill the second adapter with the pack items list
packadapter = new MyPacksPagerAdapter(ProductLandingActivity.this, packItems);
pagerpacks.setAdapter(packadapter);

You have to do this when categorylist is created and populated. So place this above code for example in your callback, when you retrieve the datas from your server.

Since the second list packItems is filling in the same order than categorylist, there will be no weird behavior by changing the both positions. Now, in the second adapter, it's preferable to use the local list packsizedata, as follows:

@Override
public Object instantiateItem(ViewGroup container, int position) {
    ...
    oneActor.name.setText(
            packsizedata.get(position).getPackSize_packSize());
    oneActor.cmtCount.setText(
            packsizedata.get(position).getPackSize_sellingPrice());
    ...
}

Finally, control the bottom ViewPager by using onPageSelected event of the first:

pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    @Override
    public void onPageScrolled(int position, float positionOffset, 
                            int positionOffsetPixels) { }

    @Override
    public void onPageSelected(int position) {
        // set the second current page regarding the position of the first
        pagerpacks.setCurrentItem(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) { }
});

Hope this will be helpful.

You should implement code for action at onPageSelected method because after page changed of your ViewPager then onPageSelected will be called.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!