RecyclerView在NestedScrollview中不起作用

时间:2019-03-22 17:17:15

标签: android android-layout android-recyclerview android-xml android-nestedscrollview

关于RecyclerView的类似问题过去曾被问过几次,我也尝试过为我的代码提供解决方案,但仍然无法找到解决我问题的实际解决方案。 顺便说一句,如果这个问题很重要,我正在使用UltimateRecyclerView库。

这是我的xml代码,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activities.TestChartActivity">

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/main_scroll_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <RelativeLayout
            android:id="@+id/main_layout_transactions"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <info.hoang8f.android.segmented.SegmentedGroup xmlns:segmentedgroup="http://schemas.android.com/apk/res-auto"
                android:id="@+id/rdGrpChartType"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:orientation="horizontal"
                android:weightSum="4"
                segmentedgroup:sc_border_width="2dp"
                segmentedgroup:sc_corner_radius="5dp">

                <RadioButton
                    android:id="@+id/rdBtn1m"
                    style="@style/RadioButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_weight="1"
                    android:button="@null"
                    android:gravity="center"
                    android:padding="2dp"
                    android:text="1M" />

                <RadioButton
                    android:id="@+id/rdBtn3m"
                    style="@style/RadioButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_weight="1"
                    android:button="@null"
                    android:gravity="center"
                    android:padding="2dp"
                    android:text="3M" />

                <RadioButton
                    android:id="@+id/rdBtn6m"
                    style="@style/RadioButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_weight="1"
                    android:button="@null"
                    android:gravity="center"
                    android:padding="2dp"
                    android:text="6M" />

                <RadioButton
                    android:id="@+id/rdBtn12m"
                    style="@style/RadioButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_weight="1"
                    android:button="@null"
                    android:gravity="center"
                    android:padding="2dp"
                    android:text="12M" />

            </info.hoang8f.android.segmented.SegmentedGroup>


            <Spinner
                android:id="@+id/spinner_property"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/rdGrpChartType"
                android:layout_margin="5dp" />


            <com.github.mikephil.charting.charts.CombinedChart
                android:id="@+id/reports"
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:layout_below="@+id/layout_transaction_summary"
                android:layout_marginBottom="20dp"
                android:layout_marginEnd="10dp"
                android:layout_marginStart="10dp" />

            <RelativeLayout
                android:id="@+id/layout_transactions_list"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/reports">

                <LinearLayout
                    android:id="@+id/transactions_buttons_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="@dimen/regular_margin"
                    android:layout_marginEnd="@dimen/fab_margin_right"
                    android:layout_marginStart="@dimen/fab_margin_right"
                    android:layout_marginTop="10dp"
                    android:elevation="2dp"
                    android:orientation="horizontal">

                    <RadioGroup
                        android:id="@+id/transactions_switch_layout"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@drawable/transactions_switch_background_border"
                        android:orientation="horizontal">

                        <RadioButton
                            android:id="@+id/radio_transaction_income"
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:background="@drawable/income_switch_background_selector"
                            android:button="@null"
                            android:checked="true"
                            android:fontFamily="sans-serif-medium"
                            android:gravity="center"
                            android:paddingBottom="8dp"
                            android:paddingTop="8dp"
                            android:text="@string/title_income"
                            android:textAllCaps="true"
                            android:textColor="@color/income_switch_color_selector" />

                        <RadioButton
                            android:id="@+id/radio_transaction_expense"
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:background="@drawable/expense_switch_background_selector"
                            android:button="@null"
                            android:fontFamily="sans-serif-medium"
                            android:gravity="center"
                            android:paddingBottom="8dp"
                            android:paddingTop="8dp"
                            android:text="@string/title_expense"
                            android:textAllCaps="true"
                            android:textColor="@color/expense_switch_color_selector" />


                    </RadioGroup>
                </LinearLayout>


                <com.marshalchen.ultimaterecyclerview.UltimateRecyclerView
                    android:id="@+id/income_recycler_view"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_below="@+id/transactions_buttons_layout"
                    android:visibility="visible"
                    app:recyclerviewClipToPadding="true"
                    app:recyclerviewPadding="2dp" />

                <com.marshalchen.ultimaterecyclerview.UltimateRecyclerView
                    android:id="@+id/expense_recycler_view"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_below="@+id/transactions_buttons_layout"
                    android:visibility="gone"
                    app:recyclerviewClipToPadding="true"
                    app:recyclerviewPadding="2dp" />

            </RelativeLayout>

            <LinearLayout
                android:id="@+id/no_transaction"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical"
                android:visibility="gone">

                <ImageView
                    android:id="@+id/no_transaction_img"
                    android:layout_width="60dp"
                    android:layout_height="60dp"
                    app:srcCompat="@drawable/ic_no_transaction" />

                <TextView
                    android:id="@+id/txt_no_transaction"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:text="@string/str_no_transaction"
                    android:textColor="@color/darkestGrey"
                    android:textSize="20sp" />

            </LinearLayout>
        </RelativeLayout>
    </android.support.v4.widget.NestedScrollView>
</RelativeLayout>

我已附上我到目前为止用上述代码实现的图像,其中RecyclerView保持固定且可滚动,但是我无法滚动整个列表布局。

任何帮助将不胜感激。

enter image description here

1 个答案:

答案 0 :(得分:0)

NestedScrollView和RecyclerView都具有相同的滚动属性。

如果我们在NestedScrollView中添加RecyclerView,则有时RecyclerView不滚动,或者如果滚动它滚动,则它单独滚动而不是使用nestedscrollview滚动,

如果我们需要滚动整个布局并使用recyclerview,则在您的活动中添加以下代码:

recyclerviewName.setNestedScrollingEnabled(false);

使用上面的代码recyclerview并一起滚动。