使用recyclerView在底页片段中滚动不起作用

时间:2019-10-29 09:13:22

标签: android android-recyclerview fragment

我在Bottomsheet片段中有一个RecyclerView,该页面的滚动不起作用,我先用约束布局尝试过,后来我移到了相对布局,在这种情况下,它也不起作用。

fragment_layout.xml的主要布局

<?xml version="1.0" encoding="utf-8"?>

    <RelativeLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/location_constraint"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:background="@drawable/rounded_rl_new">

        <ImageView
            android:id="@+id/header_iv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:src="@drawable/black_icon" />

        <LinearLayout
            android:id="@+id/linearGpsLayout"
            android:layout_width="match_parent"
            android:layout_height="@dimen/margin_30dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="@dimen/height_50dp"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/gpsImage"
                android:layout_width="@dimen/pad_16dp"
                android:layout_height="@dimen/pad_16dp"
                android:layout_gravity="center_vertical"
                android:layout_marginStart="@dimen/margin_large"
                android:src="@drawable/icon_current" />

            <TextView
                android:id="@+id/textView3"
                style="@style/Nunito_Sans_Light"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginStart="@dimen/margin_10dp"
                android:text="@string/current_location"
                android:textSize="@dimen/city_text_size" />

        </LinearLayout>

        <View
            android:id="@+id/locationLineView"
            android:layout_width="match_parent"
            android:layout_height="@dimen/pad_5"
            android:layout_below="@+id/linearGpsLayout"
            android:layout_alignParentEnd="true"
            android:layout_marginTop="@dimen/pad_10dp"
            android:background="@color/text_color" />
        <LinearLayout
            android:id="@+id/layoutContainer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/locationLineView"
            android:layout_alignParentStart="true"
            android:layout_alignParentEnd="true"
            android:orientation="vertical">

            <include
                android:id="@+id/locationLayout"
                layout="@layout/location_fragment" />

            <include
                android:id="@+id/outletLayout"
                layout="@layout/outlet_fragment"
                android:visibility="gone" />
        </LinearLayout>

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/location_recyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/font_26"
                android:visibility="gone"
                android:nestedScrollingEnabled="false"
                android:layout_marginTop="@dimen/crownImage_marginEnd"
                android:layout_marginEnd="@dimen/recyclerViewTopMargin"
                android:layout_below="@+id/layoutContainer"
                />

        <TextView
            android:id="@+id/notFoundText"
            style="@style/Nunito_Sans"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:color="@color/text_color"
            android:gravity="center"
            android:visibility="gone"
            android:text="@string/not_found"
            android:textSize="@dimen/font_16"
            android:layout_marginTop="@dimen/margin_20"
            android:layout_marginBottom="@dimen/margin_20"
            android:layout_centerInParent="true"
            android:layout_below="@+id/layoutContainer"/>

        <ProgressBar
            android:id="@+id/progress_bar_cyclic"
            style="?android:attr/progressBarStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone"
            android:layout_centerInParent="true"
             />        
    </RelativeLayout>

Below are the two includes location_fragment.xml which has layout for edit text and popularity list

<?xml version="1.0" encoding="utf-8"?>
            <androidx.constraintlayout.widget.ConstraintLayout 
            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">

        <TextView
                android:id="@+id/select_city"
                style="@style/Nunito_Sans_Light"
                android:layout_width="wrap_content"
                android:layout_height="@dimen/special_text_top_margin"
                android:layout_marginStart="@dimen/margin_large"
                android:layout_marginTop="@dimen/margin_20dp"
                android:gravity="start"
                android:text="@string/select_city"
                android:textColor="@color/text_color"
                android:textSize="@dimen/city_text_size"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_goneMarginTop="@dimen/margin_20dp">
        </TextView>

        <EditText
                android:id="@+id/city_editText"
                style="@style/Nunito_Sans_Extrabold"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/pad_24"
                android:layout_marginTop="@dimen/small_icon_size"
                android:layout_marginEnd="@dimen/menu_txt_margin_top"
                android:cursorVisible="true"
                android:gravity="start"
                android:hint="@string/selection_city"
                android:maxLines="@string/max_line"
                android:singleLine="true"
                android:textColorHint="@color/select_city_color"
                android:textSize="@dimen/city_text_size"
                app:layout_constraintEnd_toStartOf="@+id/search_image"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/select_city" />

        <ImageView
                android:id="@+id/search_image"
                android:layout_width="@dimen/margin_medium"
                android:layout_height="@dimen/margin_medium"
                android:layout_marginEnd="@dimen/barbquePoints_height"
                app:layout_constraintBottom_toBottomOf="@+id/city_editText"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="@+id/city_editText"
                app:srcCompat="@drawable/icon_search" />

        <TextView
                android:id="@+id/hint_select_city"
                style="@style/Nunito_Sans_Roman"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/margin_large"
                android:layout_marginTop="@dimen/crownImage_marginEnd"
                android:text="@string/hint"
                android:textColor="@color/select_city_color"
                android:textSize="@dimen/textSizePoints"
                android:visibility="gone"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/select_city" />

        <TextView
                android:id="@+id/choices_textView"
                style="@style/Nunito_Sans_Light"
                android:layout_width="wrap_content"
                android:layout_height="@dimen/text_imagePoint_width"
                android:layout_marginStart="@dimen/margin_large"
                android:layout_marginTop="@dimen/pad_8dp"
                android:text="@string/popular_choices"
                android:textColor="@color/popular_city_text_color"
                android:textSize="@dimen/special_description_text_size"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/city_editText" />

        <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/popularListRecyclerView"
                android:layout_width="match_parent"
                android:layout_height="@dimen/height_30dp"
                android:layout_marginStart="@dimen/margin_large"
                android:layout_marginTop="@dimen/pad_15dp"
                android:layout_marginEnd="@dimen/recyclerViewTopMargin"
                android:scrollbars="vertical"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/choices_textView" />

    </androidx.constraintlayout.widget.ConstraintLayout>

这是outlet_fragment.xml,当用户开始在EditText中输入位置时显示出口列表

<?xml version="1.0" encoding="utf-8"?>
            <androidx.constraintlayout.widget.ConstraintLayout
            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">
        <TextView
                android:text="@string/selected_city_name"
                android:layout_width="wrap_content"
                android:layout_height="@dimen/special_text_top_margin"
                android:id="@+id/selectedCity"
                android:textSize="@dimen/city_text_size"
                style="@style/Nunito_Sans_Light"
                android:gravity="start"
                android:layout_marginTop="@dimen/margin_20dp"
                android:textColor="@color/text_color"
                app:layout_constraintStart_toStartOf="parent"
                android:layout_marginStart="@dimen/margin_large"
                app:layout_constraintTop_toTopOf="parent"/>
        <TextView
                android:text="TextView"
                android:id="@+id/city_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                style="@style/Nunito_Sans_Extrabold"
                android:textSize="@dimen/city_text_size"
                android:textColor="@color/main_app_color"
                app:layout_constraintStart_toStartOf="parent"
                android:layout_marginStart="@dimen/margin_large" android:layout_marginTop="5dp"
                app:layout_constraintTop_toBottomOf="@+id/selectedCity"/>
        <TextView
                android:id="@+id/image_textView"
                android:text="@string/change_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="start"
                style="@style/Nunito_Sans_Light"
                android:textColor="@color/main_app_color"
                android:textSize="@dimen/font_14sp"
                android:clickable="true"
                android:focusable="true"
                android:layout_marginEnd="@dimen/margin_25dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="@+id/city_name"
                app:layout_constraintBottom_toBottomOf="@+id/city_name"/>
        <ImageView
                android:id="@+id/change_image"
                android:layout_width="@dimen/pad_16dp"
                android:layout_height="@dimen/pad_16dp"
                app:srcCompat="@drawable/ic_icon_edit"
                android:layout_marginEnd="@dimen/pad_1dp"
                app:layout_constraintTop_toTopOf="@+id/image_textView"
                app:layout_constraintBottom_toBottomOf="@+id/image_textView"
                app:layout_constraintEnd_toStartOf="@+id/image_textView"/>
        <TextView
                android:text="@string/selected_outlet"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="@dimen/city_text_size"
                style="@style/Nunito_Sans_Light"
                android:id="@+id/location_outlet_textView"
                app:layout_constraintStart_toStartOf="parent"
                android:layout_marginStart="@dimen/margin_large"
                android:layout_marginTop="@dimen/recyclerViewTopMargin"
                app:layout_constraintTop_toBottomOf="@+id/city_name"/>
       </androidx.constraintlayout.widget.ConstraintLayout>



Please help me with this

1 个答案:

答案 0 :(得分:0)

我通过使BottomSheet片段不可拖动来解决此问题,以下是此代码:

对于Kotlin-:

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
            val bottomsheet = super.onCreateDialog(savedInstanceState)
            bottomsheet.setOnShowListener {
                //this disables outside touch
              //  bottomsheet .window.findViewById<View>(R.id.touch_outside)?.setOnClickListener(null)
                //this prevents dragging behavior
               (bottomsheet.window.findViewById<View>(R.id.design_bottom_sheet)?.layoutParams as CoordinatorLayout.LayoutParams).behavior = null
                (bottomsheet.window.findViewById<View>(R.id.design_bottom_sheet)?.layoutParams as CoordinatorLayout.LayoutParams).gravity = Gravity.BOTTOM
            }
            return bottomsheet
        }

这将从下面打开您的片段,要取消片段,您需要在片段外面单击。