【问题标题】:RecyclerView is cutting off the last item when switching to landscape mode切换到横向模式时,RecyclerView 正在切断最后一项
【发布时间】:2018-02-18 17:37:59
【问题描述】:

我在MainActivity 中使用TabLayout,在第二个tab 里面我有一个RecyclerView,我成功地将数据绑定到RecyclerView,一切正常,但是当我将屏幕旋转到横向时模式回收器的最后一项正在被切断,

问题是这样的:

这是纵向模式:

和横向模式:

这是我的 XML 文件:

片段布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/cities_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginStart="5dp"
        android:layout_marginEnd="5dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

</android.support.constraint.ConstraintLayout>

最后是 RecyclerView 项目:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:elevation="3dp"
        card_view:cardCornerRadius="3dp"
        card_view:cardUseCompatPadding="true">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/img_city"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:maxHeight="250dp"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:clickable="true"
                android:scaleType="fitXY" />

        </RelativeLayout>

    </android.support.v7.widget.CardView>

</LinearLayout>

我尝试将 android:layout_marginBottom 添加到 de RecyclerView 但不起作用。

谢谢。

【问题讨论】:

  • 只有一个 xml 用于横向和纵向模式的根视图(视图选项卡布局)?
  • 是的,我只有一个。
  • 使用带有线性布局子视图的滚动视图...包裹recyclerview...或者在recyclerview的底部留出边距
  • 没有改变
  • 为什么需要android:scrollbars="none"?在我看来,您的项目在屏幕上没有足够的空间,并且由于您无法滚动,因此看起来视图正在“被切断”。

标签: android listview android-recyclerview


【解决方案1】:
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
xmlns:app="http://schemas.android.com/apk/res-auto"/>

 <android.support.v7.widget.RecyclerView 
android:id="@+id/cities_recycler_view" 
android:layout_width="match_parent" 
android:layout_height="0dp" 
android:scrollbars="none" 
android:layout_marginLeft="5dp" 
android:layout_marginRight="5dp" 
android:layout_marginStart="5dp" 
android:layout_marginEnd="5dp" 
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf ="parent"
 app:layout_constraintRight_toRightOf="parent" />
</android.support.constraint.ConstraintLayout>

【讨论】:

  • 添加250dp的静态高度到cardview或者recyclerview项目的相对布局
  • 尝试将约束布局改为线性布局
【解决方案2】:

我现在在为平板电脑开发应用程序时面临同样的问题,这很烦人,没有答案,我找到了 2 个解决方案。

我认为他们一点也不好。

首先,您可以提供像 150dp 这样的底部填充,或者在布局的末尾创建一个高度为 150dp 的视图。

【讨论】:

  • 也许你应该发布这两个不太好的解决方案的链接,然后让 OP 尝试一下
  • 我确实告诉他他如何复制解决方案“首先,您可以提供像 150dp 这样的底部填充,或者在布局的末尾创建一个高度为 150dp 的视图。”,相信我,这些是基本的东西,每个在 android 上工作的人都知道如何去做,这就是我没有为它编写代码的原因。
猜你喜欢
  • 2015-12-20
  • 2017-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-05
  • 1970-01-01
  • 2011-02-06
相关资源
最近更新 更多