gpt4 book ai didi

android - 如何模仿 Lollipop 的手机应用程序,在可滚动内容之前调整上部项目的大小?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:12:57 24 4
gpt4 key购买 nike

背景

在 Lollipop 的电话应用程序中,当您到达“最近”或“联系人”选项卡并尝试滚动时,首先发生的是调整上部区域的大小,该区域由最近的事件和搜索框组成,如这样的:

enter image description here

如果 RecyclerView 上有超过 3 个项目,只要上部区域可以缩小到最小尺寸,RecyclerView 就不会向下滚动。

向上滚动也是如此:只要上部区域没有完全放大,RecyclerView 就不会向上滚动。

问题

我找不到实现此功能的最佳方法。

更具体地说,我对调整上部区域的大小有疑问,这样它就会像在 Lollipop 上一样流畅。

为简化起见,我只需要调整屏幕的一部分,而不是手机应用程序(“最近的事件”和搜索框)上显示的两部分。

我尝试过的

我尝试过很多事情:

  1. 使 RecyclerView 变得和整个屏幕一样大,同时有一个假的、大的、空的标题。当我滚动时,我还将“translationY”设置为顶部的真实内容。这存在将滚动条显示为上部区域的一部分的问题(我想在正确的位置显示滚动条)。此外,它还变得非常复杂,因为您还必须处理其他页面的 RecyclerViews 滚动值。这是类似于一些第三方库的解决方案,例如 this one (但在这里我使用的是布局而不是 ImageView,但代码几乎相同)。

  2. RecyclerView在上方区域下方,滚动改变上方区域的layoutParams,和我在this post上做的类似.

  3. 与 2 相同,但我没有更改 LayoutParams,而是更改了 View 的 scaleY。

对于#2 和#3,问题是在某些情况下,一旦我得到某个值的 +Y 滚动事件,我也会得到相同值的 -Y 的滚动事件(并且反之亦然),使调整大小效果的处理“跳跃”(向上和向下,即使用户很好地滚动)。

我猜测为什么会发生这种情况是因为当我移动 RecyclerView 时,用户进行的下一个事件位于与原始事件完全不同的位置,因为我已经更改了 RecyclerView 的定位方式或其大小.

我尝试同时使用 setOnTouchListener (甚至连同 GestureDetectorCompat )和 setOnScrollListener .所有这些都导致了同样奇怪的问题。

问题

我该如何解决这个问题?为什么会发生,为什么不总是发生?


编辑:我试图更改 #2,这样我就不会处理 RecyclerView 的触摸事件,而是处理其容器(一些 LinearLayout)的触摸事件。出于某种原因,容器没有响应触摸事件,所以我将其替换为 RelativeLayout,并在此布局内的所有内容之上放置一个 View ,它将响应触摸事件(并相应地更改上部区域)。这行得通,但随后我无法在需要时将事件进一步传递给 subview 。

我认为最好的办法是自定义 RecyclerView(扩展它)触摸处理。也许添加一个滚动监听器,当 RecyclerView 即将滚动时,它会询问是否可以,如果没有,它不会尝试滚动,直到触发触摸事件并重置其状态。

最佳答案

好吧,一个解决方案(我仍然不喜欢,但有效)是使用“TouchInterceptionFrameLayout”类(来自 the library 我发现),这将允许你要决定何时允许滚动、何时不允许滚动以及何时不应该滚动,您可以更改同时具有分页选项卡和 ViewPager 的布局的填充(或您希望的内容)。

viewPager&tabs 的容器覆盖了整个区域,并且有一个变化的填充。

这是布局:

        <LinearLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<!-- put upper content here -->

</LinearLayout>

<LinearLayout
android:id="@+id/pagerAndTabsContainer"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.astuetz.PagerSlidingTabStrip
android:id="@+id/viewPagerSlidingTabsStrip"
android:layout_width="match_parent"
android:background="@color/default_background"
android:layout_height="48dip"/>

<android.support.v4.view.ViewPager
android:background="@color/default_background"
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>


</com.github.ksoichiro.android.observablescrollview.TouchInterceptionFrameLayout>

然后,您需要一些初始化代码(布局阶段完成后,您可以获得 View 的实际大小):

// the upper area height, that can be shrunk to a size of 0 height when needed
mResizeableViewHeight = mHeaderView.getHeight();
// set initial padding if you wish to show the upper area by default
mPagerAndTabsContainer.setPadding(0, mResizeableViewHeight, 0, 0);
prepareViewPager(mViewPager); // set the pages and fragments...
// init the "TouchInterceptionFrameLayout" to handle touch events
mContentView.setScrollInterceptionListener(mInterceptionListener);

在 TouchInterceptionFrameLayout.TouchInterceptionListener 的“shouldInterceptTouchEvent”函数上,你决定什么时候应该阻止触摸,在这个接口(interface)的“onMoveMotionEvent”上你应该处理被阻止的触摸。

对于每个具有 recyclerView 的 fragment ,使用 ObservableRecyclerView 代替,并调用这些行:

    // get the fragment that shows the screen I've made (with the layout)
Fragment fragment = getTargetFragment();
// let the recyclerView handle filtered touches
mRecyclerView.setTouchInterceptionViewGroup(((IGetContainer) fragment).getContainer());
// if you wish to put animations when you do a touch-up event, this may be handy (or use onUpOrCancelMotionEvent of the "TouchInterceptionListener" too/instead ) :
if (fragment instanceof ObservableScrollViewCallbacks)
mRecyclerView.setScrollViewCallbacks((ObservableScrollViewCallbacks) fragment);

我不太喜欢这个解决方案,因为它用触摸包装覆盖了整个屏幕,因为它会导致 viewPager 的 overdraw (并且需要你为它设置背景),并且因为它与我的想法。它也比 Lollipop 的 Phone 应用程序流畅得多,无论我如何使用滚动规则。

我可以让 TouchInterceptionListener 仅在触摸事件源自 RecyclerView 时才开始处理触摸事件,但这会使它的复杂性变得更糟。

关于android - 如何模仿 Lollipop 的手机应用程序,在可滚动内容之前调整上部项目的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27997948/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com