关于 ExpandableListView ChildView 的 Button ClickListener 的问题

Question regarding the ExpandableListView ChildView's Button ClickListener

我正在尝试为我的餐厅制作一个应用程序,这是我之前关于我遇到的问题的问题,它已得到解决,但是,您会在 ChildView 中注意到一些按钮,它是“添加" 这是我之前的查询 here

你可以看到我有“Add”按钮我点击的地方我想要显示我的相对布局,但是,这里有一些问题是我用过的自定义 expandableListView 是这样的:

class NonScrollExpandableListView : ExpandableListView {

constructor(context: Context) : super(context) {}

constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {}

constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) {}

override fun setAdapter(adapter: ExpandableListAdapter?) {
    super.setAdapter(adapter)
}

override fun setOnChildClickListener(onChildClickListener: OnChildClickListener) {
    super.setOnChildClickListener(onChildClickListener)
}

override fun expandGroup(groupPos: Int) : Boolean {
    return super.expandGroup(groupPos)
}

override fun expandGroup(groupPos: Int, animate: Boolean) : Boolean {
    return super.expandGroup(groupPos, animate)
}

override fun isGroupExpanded(groupPosition: Int): Boolean {
    return super.isGroupExpanded(groupPosition)
}

public override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
    val heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
            Integer.MAX_VALUE shr 2, MeasureSpec.AT_MOST)
    super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom)
    val params = layoutParams
    params.height = measuredHeight
 }
}

这不是可滚动的 listView。

这是我的XML代码:

<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="wrap_content"
android:layout_marginBottom="30dp"
android:orientation="horizontal">

<androidx.cardview.widget.CardView
    android:layout_width="95dp"
    android:layout_height="100dp"
    android:baselineAligned="false"
    android:layout_marginStart="10dp"
    android:layout_marginTop="10dp"
    app:cardBackgroundColor="#FF682F"
    android:layout_marginBottom="20dp"
    app:cardCornerRadius="13dp">

    <ImageView
        android:id="@+id/Restaurant_Image_List"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:layout_marginStart=".5dp"
        android:layout_marginEnd=".5dp"
        android:layout_marginTop=".5dp"
        android:layout_marginBottom=".5dp"
        />

</androidx.cardview.widget.CardView>

<androidx.cardview.widget.CardView
    android:id="@+id/ClickAddToCart"
    android:layout_width="80dp"
    android:layout_height="25dp"
    android:layout_marginTop="93dp"
    android:layout_marginStart="16.5dp"
    android:layout_marginBottom="5dp"
    android:layout_marginEnd="5dp"
    app:cardCornerRadius="3dp">

    <Button
        android:id="@+id/RestaurantAddItem"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="#FFCE55"
        android:background="?attr/selectableItemBackground"
        android:text="ADD"
        android:textStyle="bold"/>

</androidx.cardview.widget.CardView>

<TextView
    android:id="@+id/RestaurantItemAvailability"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="visible"
    android:text="Available"
    android:layout_alignParentEnd="true"
    android:layout_marginTop="50dp"
    android:layout_marginEnd="20dp"/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_marginStart="100dp"
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:layout_height="100dp">

        <TextView
            android:id="@+id/RestaurantItemName"
            android:layout_marginTop="8dp"
            android:layout_marginStart="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:textSize="16sp"
            android:text="Item Name"/>

        <TextView
            android:id="@+id/RestaurantItemPrice"
            android:layout_marginTop="10dp"
            android:layout_marginStart="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="15dp"
            android:textStyle="bold"
            android:text="Price"/>

    </RelativeLayout>

</LinearLayout>

<RelativeLayout
    android:id="@+id/ClickAddToCart2"
    android:visibility="invisible"
    android:layout_marginTop="10dp"
    android:layout_marginEnd="10dp"
    android:layout_width="match_parent"
    android:layout_height="100dp">

    <ImageButton
        android:id="@+id/ButtonPlus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:background="?attr/selectableItemBackground"
        app:srcCompat="@drawable/ic_plus"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="85dp"/>

    <ImageButton
        android:id="@+id/ButtonMinus"
        android:layout_width="16dp"
        android:layout_height="1dp"
        android:layout_marginTop="17dp"
        android:gravity="center"
        android:background="?attr/selectableItemBackground"
        app:srcCompat="@drawable/ic_minus"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="15dp"/>

    <TextView
        android:id="@+id/NumberOfItemAdded"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1"
        android:textSize="30sp"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="50dp"
        android:layout_marginTop="20dp"/>

    <androidx.cardview.widget.CardView
        android:layout_width="70dp"
        android:layout_height="20dp"
        android:layout_marginTop="70dp"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="24dp"
        app:cardCornerRadius="3dp">

        <Button
            android:id="@+id/RestaurantAddItem2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Add"
            android:textSize="10sp"
            android:textColor="#FFCE55"
            android:background="?attr/selectableItemBackground"
            tools:ignore="SmallSp" />


    </androidx.cardview.widget.CardView>
</RelativeLayout>

我的 getChildView 代码:

 @Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_for_restaurant, parent, false);

    restaurantItemName = convertView.findViewById(R.id.RestaurantItemName);
    restaurantItemPrice = convertView.findViewById(R.id.RestaurantItemPrice);
    restaurantItemAvailability = convertView.findViewById(R.id.RestaurantItemAvailability);
    restaurantAddItem = convertView.findViewById(R.id.RestaurantAddItem);
    restaurantAddItem2 = convertView.findViewById(R.id.RestaurantAddItem2);
    restaurant_Image_List = convertView.findViewById(R.id.Restaurant_Image_List);
    buttonPlus = convertView.findViewById(R.id.ButtonPlus);
    buttonMinus = convertView.findViewById(R.id.ButtonMinus);
    clickAddToCart2 = convertView.findViewById(R.id.ClickAddToCart2);
    clickToAddCart = convertView.findViewById(R.id.ClickAddToCart);

    Glide.with(context).load(objectMap.get(headerList.get(groupPosition)).get(childPosition).getITEM_IMAGE()).error(R.drawable.googleg_standard_color_18).into(restaurant_Image_List);
    restaurantItemName.setText(objectMap.get(headerList.get(groupPosition)).get(childPosition).getITEM_NAME());
    restaurantItemPrice.setText("₹ " + objectMap.get(headerList.get(groupPosition)).get(childPosition).getITEM_PRICE());

    if (objectMap.get(headerList.get(groupPosition)).get(childPosition).getAVAILABILITY().equals("false")) {
        restaurantItemAvailability.setText("Unavailable");
        restaurantItemAvailability.setTextColor(Color.parseColor("#FF3600"));
    } else {
        restaurantItemAvailability.setText("Available");
        restaurantItemAvailability.setTextColor(Color.parseColor("#95C869"));
    }

    restaurantAddItem.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (objectMap.get(headerList.get(groupPosition)).get(childPosition).getAVAILABILITY().equals("false")){
                Toast.makeText(context, objectMap.get(headerList.get(groupPosition)).get(childPosition).getITEM_NAME()+
                        " is currently unavailable", Toast.LENGTH_SHORT).show();
            }else{
                restaurantItemAvailability.setVisibility(View.GONE);
                clickToAddCart.setVisibility(View.GONE);
                clickAddToCart2.setVisibility(View.VISIBLE);
            }
        }
    });

    return convertView;
}

在这里,当我单击“添加”按钮时,我希望执行以下代码:

                restaurantItemAvailability.setVisibility(View.GONE);
                clickToAddCart.setVisibility(View.GONE);
                clickAddToCart2.setVisibility(View.VISIBLE);

Add 的点击侦听器工作正常,因为我已经使用 Toast 检查它是否工作正常,并且它完美地执行了 Toast,但是,上面的代码没有执行,我不不知道为什么。 谁能帮帮我?

提前致谢。

我终于通过使用另一个常量解决了这个问题,在这个常量中我保存了点击值并检查了该值并在条件语句中使用它,问题得到了解决。