Android - 多个可查看项目的水平滚动

Android - horizontal scrolling of multiple viewable items(Android - 多个可查看项目的水平滚动)
本文介绍了Android - 多个可查看项目的水平滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看我自己的答案以获得简单的解决方案

See my own answer for easy solution

重要提示:提供赏金是为了清楚地修改 ViewPager 以满足下面概述的场景.请不要提供 Horizo​​ntalScrollView - 我需要涵盖完整的 Fragment 生命周期场景

我需要实现基于 Fragments 的视图的水平滚动,其中一个项目位于中心,右侧/左侧的项目部分或完全可见.ViewPager 不适合这项任务,因为它每次都专注于显示一个项目.

I need to implement horizontal scrolling of Fragments-based views in which one item is in the center and items to the right/left are partially or fully visible. ViewPager is ill suitable for the task since it's focused on displaying one item at each time.

为了便于理解,下面是一个快速草图,其中项目 1、5 和 6 位于可视区域之外.并且想要使这个可查看的数字可配置,例如在纵向视图中,我将只显示 2 个(或可能只显示一个)项目.

To make it easier to understand below is a quick sketch in which items 1, 5 and 6 are outside of viewable area. And and want to make this viewable number configurable so for example in portrait view I will only show 2 (or possibly just one) items.

我不想在屏幕上显示 3 个项目,只要显示中心项目就可以裁剪其他项目.在小屏幕上可以有 1 个中心项目,随着屏幕尺寸的增大,应该显示多个(裁剪是可以的)项目

I'm not trying to fit say 3 items on the screen, as long as central item is shown others can be cropped. On the small screen is OK to have 1 central item and as screen grows in size multiple (cropped is OK) items should be shown

我知道这看起来像一个画廊,但这些项目又不是简单的图像,而是 Fragments,每个片段中都有一个可垂直滚动的列表

I understand that this looks like a gallery but again the items are not simple images but Fragments with a vertically scrollable list in each fragment

附:发现@Commonsware 的这篇博文 列出了 3 种不同的方法.为了我的需要,我喜欢#3

P.S. Found this blogpost by @Commonsware that list 3 different approaches. For my need I like #3

推荐答案

这个答案出奇的简单,我什至不知道为什么没有马上发布.为了获得确切的效果,我需要做的就是重写 PagerAdapter#getPageWidth 方法.默认情况下,它返回 1,但如果您将其设置为 0.5,您将获得 2 页,0.33 将为您提供 3 页,依此类推.根据分页器项目之间分隔符的宽度,您可能需要稍微减小该值.

This one has surprisingly easy answer, I'm not even sure why it wasn't posted right away. All that I needed to do to get the exact effect was to override PagerAdapter#getPageWidth method. By default it returns 1 but if you set it to 0.5 you will get 2 pages, 0.33 will give you 3, etc. Depending on width of the separator between pager items you may have to slightly decrease the value.

请参阅以下代码段:

    @Override
    public float getPageWidth(final int position) {
        // this will have 3 pages in a single view
        return 0.32f;
    }

这篇关于Android - 多个可查看项目的水平滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)