从适配器调用BottomSheetBehavior

时间:2020-08-27 23:57:35

标签: kotlin android-recyclerview fragment adapter

我只是在用kotlin学习android,遇到了一个我不清楚的情况,感谢您的支持。

在一个片段中,我有一个带有产品标题和按钮的RecyclerView。当我单击该按钮时,我应该得到一个BottomSheetBehavior。

由观察者试过,但失败了。现在,我尝试在片段内调用一个方法,但会引发错误。

这是回收站的视图

<androidx.constraintlayout.widget.ConstraintLayout 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="wrap_content"
    android:layout_marginTop="10dp"
    android:background="@drawable/bg_row_food_by_prov"
    android:padding="10dp"
    app:cardCornerRadius="10dp"
    app:cardElevation="4dp">


    <androidx.cardview.widget.CardView
        android:id="@+id/img_row_det_prov_card"
        android:layout_width="93dp"
        android:layout_height="100dp"
        app:cardCornerRadius="10dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/img_row_det_prov"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.cardview.widget.CardView>

    <ImageView
        android:id="@+id/img_row_det_prov_veg_green"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/icon_veg_green"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/img_row_det_prov_card" />

    <TextView
        android:id="@+id/tv_row_det_prov_title"
        style="@style/styleTitleRowPopulares"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="3dp"
        android:text="@string/str_row_title"
        app:layout_constraintStart_toEndOf="@+id/img_row_det_prov_card"
        app:layout_constraintTop_toTopOf="@+id/img_row_det_prov_card" />

    <TextView
        android:id="@+id/tv_row_det_prov_subtitle"
        style="@style/textStyleSubTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/str_row_subtitle"
        app:layout_constraintStart_toStartOf="@+id/tv_row_det_prov_title"
        app:layout_constraintTop_toBottomOf="@+id/tv_row_det_prov_title" />

    <View
        android:id="@+id/divisor_prov_detail_row_subtitle"
        android:layout_width="50dp"
        android:layout_height="1dp"
        android:layout_marginTop="8dp"
        android:background="@color/colorDivisorGray"
        app:layout_constraintStart_toStartOf="@+id/tv_row_det_prov_subtitle"
        app:layout_constraintTop_toBottomOf="@+id/tv_row_det_prov_subtitle" />

    <TextView
        android:id="@+id/tv_row_det_prov_price"
        style="@style/stylePopularesPriceNew"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dp"
        android:text="@string/str_row_price"
        app:layout_constraintBottom_toBottomOf="@+id/img_row_det_prov_card"
        app:layout_constraintStart_toStartOf="@+id/tv_row_det_prov_subtitle" />

    <LinearLayout
        android:id="@+id/btn_row_prov_det_add"
        style="@style/styleButtonGreenSmall"
        android:layout_width="88dp"
        android:layout_height="28dp"
        android:gravity="center"
        app:layout_constraintBottom_toBottomOf="@+id/img_row_det_prov_card"
        app:layout_constraintEnd_toEndOf="parent">

        <TextView
            android:id="@+id/btn_text_row_prov_det_add"
            style="@style/styleButtonTextWhileSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="?attr/selectableItemBackground"
            android:clickable="true"
            android:text="@string/str_row_btn" />
    </LinearLayout>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/btn_row_prov_det_added"
        android:layout_width="88dp"
        android:layout_height="28dp"
        android:background="@drawable/button_border"
        android:gravity="center"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="@+id/img_row_det_prov_card"
        app:layout_constraintEnd_toEndOf="parent">

        <TextView
            android:id="@+id/btn_text_row_prov_det_add_remove"
            style="@style/styleButtonTextGreenSmall"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="?attr/selectableItemBackground"
            android:text="-"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/btn_text_row_prov_det_add_count"
            style="@style/styleButtonTextGreenSmall"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="?attr/selectableItemBackground"
            android:text="1"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/btn_text_row_prov_det_add_plus"
            style="@style/styleButtonTextGreenSmall"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="+"
            android:background="?attr/selectableItemBackground"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

这是片段

 <androidx.constraintlayout.widget.ConstraintLayout>
    

            .....

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerViewFoodByProv"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@android:color/transparent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="@+id/card_prov_detail_card_main"
            app:layout_constraintStart_toStartOf="@+id/card_prov_detail_card_main"
            app:layout_constraintTop_toBottomOf="@+id/card_prov_detail_card_main" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <include layout="@layout/fragment_pedido_en_proceso" />




private lateinit var bottomSheetBehavior: BottomSheetBehavior<ConstraintLayout>


    fun openBottomSheetOrder() {
            //Log.v("CLICK", "change order in fragment $order")
            bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet_pedido_proceso)
            bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
        }

适配器发出的ottomSheetBehavior调用

btnText.setOnClickListener {
         
                val fragment = ProveedorDetailFragment()
                fragment.openBottomSheetOrder()
               
            }

错误

Process: com.umbani.umbani, PID: 30074
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.view.View.getLayoutParams()' on a null object reference

0 个答案:

没有答案