Viewpager 没有得到最后一项 [英] Viewpager not getting last item

查看:26
本文介绍了Viewpager 没有得到最后一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用中使用了两个 Viewpager

I am using two Viewpager in my app,

1) 第一个 Viewpager 只显示图片

1) First Viewpager displays images only

2) 我正在显示价格

现在的问题是我的 viewpager1 中显示了 4 张图像,而在第二个寻呼机中,我的价格与所选产品相同.第一次它不显示任何内容,但是当我滚动图像并转到下一个时,它显示价格..

now the issue is i have 4 images displaying in my viewpager1, and in second pager i have price as per selected product. first time it does not show anything, but when i scroll image and goes to next, it shows price..

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

                Picasso.with(ProductLandingActivity.this)                            .load(categorylist.get(position).getProductLanding_packLink())
                        .error(R.drawable.nopreview )
                        .placeholder(R.drawable.progress_animation)
                        .into(selectedImage);

                System.out.println("Selected is"+position);
              selectedname.setText(categorylist.get(position).getProductLanding_packDesc());

                for (int i = 0; i < categorylist.get(position).getItems().size(); i++) {
                    System.out.println("ProductPack_ID : " + categorylist.get(position).getItems().get(i).getPackSize_sellingPrice());


                }

                temp = categorylist.get(position).getItems();

              packadapter = new MyPacksPagerAdapter(ProductLandingActivity.this,categorylist);
                pagerpacks.setAdapter(packadapter);

            }

            @Override
            public void onPageSelected(int position) {


            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }


        });

适配器

private class MyPacksPagerAdapter extends PagerAdapter {

        Context context;
        ArrayList<PackListModel> packsizedata ;

        public MyPacksPagerAdapter(Context context,ArrayList<PackListModel> packsizedata) {
            this.context = context;
            this.packsizedata = packsizedata;

        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            final OneActor oneActor;

            View view;
            LayoutInflater infl = ((Activity)context).getLayoutInflater();
            view = infl.inflate(R.layout.list_item_pagerpacktitles, container,false);
            oneActor = new OneActor();
           // oneActor.avatar = (ImageView) view.findViewById(R.id.image);
            oneActor.name = (TextView) view.findViewById(R.id.product_landing_packsname);
            oneActor.cmtCount = (TextView) view.findViewById(R.id.product_landing_packsprice);
            view.setTag(oneActor);


          oneActor.name.setText(temp.get(position).getPackSize_packSize());
            oneActor.cmtCount.setText(temp.get(position).getPackSize_sellingPrice());

            ((ViewGroup) container).addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View)object);
        }

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

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return (view == object);
        }
        class OneActor{
           // ImageView avatar;
            TextView name,cmtCount;
        }
    }

默认情况下,当我运行应用程序时,它显示如下,在第二个寻呼机中,它不显示产品价格,

By defaulat when i run the app it shows like this,in second pager it is not showing product price,

但是当我滚动图像时它显示价格

But when i scroll image it shows price

我的预期输出是

这是我的 json 响应

This is my json response

http://pastebin.com/fbJang2B

推荐答案

首先,正如@NotobharatKumar 所提到的,您在父适配器的错误事件上加载第二个适配器,即在 onPageScrolled 方法.其次,您每次都在该特定事件上设置一个新适配器,这似乎没用.最后,您要在事件中设置数据,(我不确定您为什么要这样做,但是)我更愿意将其设置在单独的适配器中,并让事件监听特定行为.

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.

对我来说,您似乎有两个单独的适配器,但一个数据列表由两者共享.假设,您必须在开始时分别设置它们的数据,并且在顶部适配器的事件 onPageSelected 上,您只需要自动滚动第二个.并且如果它们在列表中的位置相同,onPageSelected 应该可以正常工作.

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:

您在 onScrollChanged 中的代码,当您设置图像和文本时,对我来说似乎很奇怪.我会使用第一个适配器,我将为第一个 ViewPager 设置像这两个这样的所有数据:

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

然后,像往常一样,在 FirstPagerAdapter 中设置您的数据:

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.

您只在第二个适配器中使用 getPackSize_packSize()getPackSize_ sellPrice(),因此您应该创建一个单独的列表来仅填充这些数据,但在滑动事件之外.首先初始化第二个列表和适配器:

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

您必须在创建和填充 categorylist 时执行此操作.因此,当您从服务器检索数据时,将上面的代码放在例如您的回调中.

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.

由于第二个列表 packItems 的填充顺序与 categorylist 相同,因此更改两个位置不会有任何奇怪的行为.现在,在第二个适配器中,最好使用本地列表 packsizedata,如下所示:

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());
    ...
}

最后,使用第一个的onPageSelected事件控制底部的ViewPager:

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

希望这会有所帮助.

这篇关于Viewpager 没有得到最后一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆