Android中RecyclerView嵌套在ScrollView中时,Margin和Padding无法正常工作。

3

当我将RecyclerView作为ScrollView的子项时,marginBottompaddingBottom不起作用。设置这些值到父级LinearLayout也没有任何影响。似乎ScrollView不能滚动到底部,因为ScrollBar无法到达底部。如何解决这个问题?

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

<!--To avoid scroll view from scrolling to the bottom automatically-->
<View android:layout_width="match_parent" android:id="@+id/focus_view" android:layout_height="0dp" android:focusable="true" android:focusableInTouchMode="true"><requestFocus/></View>

<ScrollView
    android:id="@+id/sv_categories_statistics"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="10">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_margin="4dp">

        <LinearLayout
            android:id="@+id/ll_categories_statistics_filters_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="4dp"
            android:layout_marginTop="4dp"
            android:layout_marginEnd="4dp">

            <LinearLayout
                android:id="@+id/ll_categories_statistics_select_date_fragment_container"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:background="@drawable/border_primary_light"
                android:padding="4dp"
                android:layout_marginEnd="4dp"/>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:background="@drawable/border_accent"
                android:padding="4dp">

                <Spinner
                    android:id="@+id/spn_categories_statistics_bill_type"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>

            </LinearLayout>
        </LinearLayout>

        <com.github.mikephil.charting.charts.PieChart
            android:id="@+id/pc_categories_statistics"
            android:layout_width="match_parent"
            android:layout_height="325dp"
            android:layout_margin="4dp"/>

    <fragment
        android:id="@+id/fgm_categories_statistics_not_enough_data"
        class="com.dolinsek.elias.cashcockpit.NotEnoughDataFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        app:text="@string/label_not_enough_data_for_statistic"
        android:layout_marginStart="4dp"
        android:layout_marginEnd="4dp"
        android:layout_marginBottom="8dp"/>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv_categories_statistics"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</ScrollView>

Example

1个回答

4

你尝试过使用clipToPadding吗?

 <android.support.v7.widget.RecyclerView
            android:id="@+id/rv_categories_statistics"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="50dp"
            android:clipToPadding="false"/>

它可以工作,但是RecyclerView现在没有显示所有项目。 - Elias Dolinsek
请尝试将scrollview android:layout_height设置为match或wrap。移除scrollview的layout_weight属性。 - prashant17

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接