【问题标题】:On Activity started layout jumps to RecyclerView's topOn Activity 开始布局跳转到 RecyclerView 的顶部
【发布时间】:2016-07-25 13:04:58
【问题描述】:

Activity 打开时,会显示 RecyclerView 布局的顶部,而不是 Activity 布局的顶部。

活动布局.xml文件:

    <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:paddingBottom="20dp"
                android:paddingTop="20dp">

            ...
            <RelativeLayout/>
            <View />
            <LinearLayout/>
            ...

            <android.support.v7.widget.RecyclerView
                android:id="@+id/venue_place_info_gallery_recycler_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"/>

            </LinearLayout>
    </ScrollView>

创建活动:

    RecyclerView galleryRecyclerView = (RecyclerView) findViewById(R.id.venue_place_info_gallery_recycler_view);
    galleryRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(gridLayoutColumns, StaggeredGridLayoutManager.VERTICAL));
    VenueGalleryAdapter venueGalleryAdapter = new VenueGalleryAdapter(VenuePlaceInfoActivity.this, images);
    galleryRecyclerView.setAdapter(venueGalleryAdapter);

适配器非常简单。它在构造函数中接收图像作为参数,以后不能更改数据。 我尝试将各种设置应用到 RecyclerView 布局,但无论有没有它们都一样。例如:

    galleryRecyclerView.setNestedScrollingEnabled(false);
    galleryRecyclerView.setHasFixedSize(true);
    galleryRecyclerView.setLayoutFrozen(true);
    galleryRecyclerView.setPreserveFocusAfterLayout(false);

更新:

我在answer of another question 中找到了有关该主题的更多信息。这完全是因为 ScrollView 和 RecyclerView 无法同时存在,即使您将 setNestedScrollingEnabled 设置为 true。但它仍然不能解决我的问题。我需要在图库 RecyclerView 上方放置一些东西,并且我想向下滚动浏览所有图像(而不是将它们放入容器中)。

【问题讨论】:

  • 可以附上截图吗?
  • 删除 galleryRecyclerView.setPreserveFocusAfterLayout(false);行
  • 我已经说过这些附加参数目前已被删除。我之前刚刚尝试过,但它们并没有改变任何东西。
  • @YasinKaçmaz,它应该是一个视频,因为在屏幕截图上显示没有什么有趣的东西。它只是从屏幕顶部开始的画廊。
  • @Galya 我无法想象然后我想要一些视觉效果。对不起,我在糟糕的一天

标签: android layout android-recyclerview


【解决方案1】:

如果你想让你的布局看起来不错,我会使用 CoordinatorLayout,其中 AppBarLayout 作为其中的第一个元素,RecyclerView 作为第二个元素。在 AppBarLayout 中,您应该将您想要的任何内容放在 RecyclerView 上方。类似的东西:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/white">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:elevation="0dp">

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_scrollFlags="scroll|enterAlways"
                app:titleEnabled="false">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:clipChildren="false"
                    android:clipToPadding="false"
                    android:orientation="vertical"
                    android:paddingTop="20dp"
                    app:layout_collapseMode="parallax">

                    <!-- stuff you want above your recyclerview -->

                </LinearLayout>
            </android.support.design.widget.CollapsingToolbarLayout>
        </android.support.design.widget.AppBarLayout>

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    </android.support.design.widget.CoordinatorLayout>
</LinearLayout>

这里有人做了类似的事情,只是不使用 appbarlayout 中的工具栏,您可以使用任何您喜欢的视图布局。 AppBarLayout 是一个扩展的 Vertical LinearLayout:http://www.basagee.tk/handling-scrolls-with-coordinatorlayout/

【讨论】:

  • 这可能不是我一直在寻找的精简和简单的解决方案,但它确实可以完成工作,因此它是公认的答案。我已经对其进行了编辑以添加对我有用的实际代码。
【解决方案2】:

只需在您的活动/片段根布局中添加以下标记即可。

android:descendantFocusability="blocksDescendants"

【讨论】:

    猜你喜欢
    • 2015-06-03
    • 1970-01-01
    • 2020-10-14
    • 2020-06-18
    • 2019-05-04
    • 1970-01-01
    • 1970-01-01
    • 2016-08-29
    • 1970-01-01
    相关资源
    最近更新 更多