我无法将 RecyclerView 滚动到最后。它切断了我的最后一个元素,我完全不知道为什么。我的项目中有另一个 RecyclerView,它似乎也损坏了。
这是我的布局代码:
<RelativeLayout 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:id="@+id/container"
android:padding="10dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cv"
app:cardCornerRadius="2dp"
app:cardUseCompatPadding="true">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="220dp"
android:orientation="horizontal">
(...)
</RelativeLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/textView17"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="HISTORIA"
android:id="@+id/textView17"
android:layout_below="@+id/cv"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
另一个损坏的 RecyclerView:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.praca_inz.Fragments.PetrolFragment">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:scaleType="center"
android:src="@drawable/ic_editor_mode_edit"
android:layout_marginBottom="70dp"
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
有什么想法吗,因为我没有。
您的第一个布局存在问题。在
RelativeLayout
中,match_parent
大小的元素将与布局本身具有完全相同的大小。这就是你的RecyclerView
的情况。但是,它并不位于布局的顶部,因为它位于 @+id/textView17
TextView
的下方。因此,您丢失了大约 250~300dp 的 RecyclerView
(卡片 + 文本的高度)。在这种情况下,您可能应该使用垂直的 LinearLayout
,这正是它们的用途。只需将 RecyclerView
的高度设置为 0,重量设置为 1。
您应该在 RecyclerView 中设置填充底部。像这样:
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/textView17"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:paddingBottom="20dp" />
我希望对你有帮助,抱歉我的英语不好。
MainActivity 布局中 ViewPager 有问题。解决方案是将
paddingTop
参数设置为 105dp(以覆盖 Toolbar 和 TabLayout)并删除 app:layout_behavior="@string/appbar_scrolling_view_behavior"
参数。
之前:
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
之后:
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="105dp"/>
谢谢@oguzhand,指导我解决问题!
将alignParentBottom =“true”添加到recyclerView。这应该将其固定到底部边缘,顶部将通过您已经就位的layoutBelow标签固定到文本视图
您可以在recyclerview中使用android:layout_height =“0dp”
`<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rcListDevice"
android:layout_width="wrap_content"
android:layout_height="0dp"
...
/>`
它对我有用。 (我在父级中使用了 ConstraintLayout)
<androidx.recyclerview.widget.RecyclerView
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/addUser"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginTop="20dp"
android:layout_marginBottom="80dp"
android:id="@+id/recycleruserDetails"
app:layout_constraintVertical_bias="0.0"
android:layout_width="match_parent"
android:layout_height="0dp" />