简体   繁体   English

Viewpager android中使用的分页Arraylist

[英]Paginate Arraylist used in Viewpager android

I want to use Viewpager to show images like Whatsapp shows, when we click on any photo in any person chat then it would open the full screen imageview and we can scroll left or right to see more media items from that chat.我想使用Viewpager来显示像 Whatsapp 显示的图像,当我们单击任何人聊天中的任何照片时,它将打开全屏图像视图,我们可以向左或向右滚动以查看该聊天中的更多媒体项目。

I know they might be using pagination to load more images on demand on both ends of the Viewpager , but thinking of it if we update the list dynamically when user is watching a photo in the Viewpager then won't he notice the updation of the Image Arraylist .我知道他们可能会使用分页Viewpager 的两端按需加载更多图像,但是考虑一下,如果我们在用户在Viewpager 中观看照片时动态更新列表,那么他不会注意到图像的更新数组列表

Whatsapp handles it very clearly. Whatsapp 处理得很清楚。

What could be done to add more data to the arraylist dynamically without letting the user know, I am asking for pagination on both left and right ends not only one side.可以做些什么来动态地将更多数据添加到数组列表而不让用户知道,我要求在左右两端进行分页,而不仅仅是一侧。

I don't know how WhatsApp works, but if you want to have an "infinite" sliding ViewPager you can return a large value in getCount() and use modulo to get the correct image.我不知道 WhatsApp 是如何工作的,但如果你想要一个“无限”滑动的 ViewPager,你可以在getCount()返回一个大值并使用 modulo 来获得正确的图像。

Something like:就像是:

public class ImageAdapter extends PagerAdapter {
    List<Image> images;
    
    public ImageAdapter(List<Image> images) {
        this.images = images;
    }

    @Override
    public int getCount() {
        return images.size() * 100;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        Image image = images.get(position % images.size());

        // Instantiate your view
    }
}

And when you set your ViewPager set the current item to a large value as well so you can actually scroll both ways当您设置 ViewPager 时,也将当前项目设置为一个较大的值,以便您实际上可以双向滚动

viewPager.setCurrentItem(images.size() * 50)

This will probably have some impact on performance as more views will have to be created.这可能会对性能产生一些影响,因为必须创建更多视图。

Option 2选项 2

Another option is to add a ViewPager.OnPageChangeListener that changes which item is the active one.另一种选择是添加一个ViewPager.OnPageChangeListener来更改哪个项目是活动项目。 You'll still have to return a larger value in getCount() , but it's only three times the size of the list, so it won't be as drastic.你仍然需要在getCount()返回一个更大的值,但它只是列表大小的三倍,所以它不会那么剧烈。

The idea is to scroll to the "same" item which takes you to the middle of the list so it feels like you're infinitely scrolling这个想法是滚动到“相同”的项目,它会把你带到列表的中间,所以感觉就像你在无限滚动

In the adapter:在适配器中:

// For a list with 2 items, the list will look like:
// 0 1    0 1    0 1

@Override
public int getCount() {
    return images.size() * 3;
}
// Set the first active item to be in the middle
//      Here
//        |
// 0 1    0 1    0 1
viewPager.setCurrentItem(images.size());

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    int currentPos;

    @Override
    public void onPageSelected(int position) {
        currentPos = position;
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        if (state == ViewPager.SCROLL_STATE_IDLE) {
            // Here       Go here    Or here
            //   |          |          |
            // 0 1        0 1        0 1
            if (currentPos == images.size() - 1 || currentPos == adapter.getCount() - 1) {
                // Scrolling with "false" removes the animation and instantly switches 
                // item, so you won't notice the scroll
                viewPager.setCurrentItem(images.size() * 2 - 1, false);
            } else if (currentPos == images.size() * 2 || currentPos == 0) {
                // Or here  Go here    Here
                // |          |          |
                // 0 1        0 1        0 1
                viewPager.setCurrentItem(images.size(), false);
            }
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        // Not implemented
    }
});

The problem with this solution is that it's not guaranteed that onPageScrollStateChanged will be called with SCROLL_STATE_IDLE since you might scroll to a new page before it reaches the idle state, which causes you to scroll past the item where it should switch.此解决方案的问题在于不能保证onPageScrollStateChanged将使用SCROLL_STATE_IDLE调用,因为您可能会在到达空闲状态之前滚动到新页面,这会导致您滚动到应该切换的项目。 If you reach the end/start it will change items, but you will notice you can't scroll for a short time if you're scrolling fast enough.如果您到达结尾/开始,它会更改项目,但您会注意到如果您滚动得足够快,您将无法在短时间内滚动。

Neither of these options are perfect, but hopefully it will give you an idea of what is possible.这些选项都不是完美的,但希望它能让您了解什么是可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在android中使用viewPager在ArrayList中循环 - Loop in ArrayList using viewPager in android 当与android中的viewpager一起使用时,单选按钮变得不可点击 - Radiobuttons becomes unclickable when used with viewpager in android 如何按比例缩小Android ViewPager中使用的图像? - How to scaled down images used in ViewPager Android? 在ViewPager中使用Android ArrayAdapter会导致NullPointerException - Android ArrayAdapter causes NullPointerException when used in ViewPager Android Listview Inside viewpager始终显示所有页面中的最后一个viewpager页面arraylist数据 - Android Listview Inside viewpager always showing last viewpager page arraylist data in all pages 如何在ViewPager中使用ArrayList? - How to use ArrayList in ViewPager? Viewpager从Arraylist动态地 - Viewpager dynamically from Arraylist Android 广播接收器在 ViewPager 中使用时注册多个侦听器 - Android Broadcast Reciver Registering Multiple Listeners When used in a ViewPager Android:我应该为ViewPager中使用的片段添加一个空构造函数 - Android: Should I add an empty constructor to a fragment used in ViewPager Android ImageSwitcher 与 ViewPager2.registerOnPageChangeCallback 一起使用时未设置初始图像 - Android ImageSwitcher not setting initial image when used with ViewPager2.registerOnPageChangeCallback
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM