有关ExpandableListView子视图的Button ClickListener的问题 [英] Question regarding the ExpandableListView ChildView's Button ClickListener

查看:65
本文介绍了有关ExpandableListView子视图的Button ClickListener的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的餐厅制作一个应用程序,这是我先前遇到的有关我所遇到的问题的问题,该问题已解决,但是,您会在ChildView中注意到一些按钮,即添加".这类似于我以前的查询这里

您可以看到我有"添加"在此处单击按钮,我要显示我的相对布局,但是,这里有一个问题是我使用了自定义的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,但是,上面的代码没有得到执行,而且我没有知道为什么. 有人可以帮我吗?

谢谢.

解决方案

我终于使用另一个常量解决了该问题,在该常量中,我保存了click值并检查了该值,并将其用于条件语句中,此问题已解决. /p>

I am trying to make an app for my restaurant, here is my previous question regarding the issue which I have faced and it was resolved, however, there is some button which you will notice in the ChildView which is "Add" here is the like for my previous query here

You can see that I have "Add" Button there where is I click I want my relative layout to be displayed, however, here is some catch is that I have used the custom expandableListView which is this:

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
 }
}

Which is not scrollable listView.

this is my XML Code:

<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>

Code for my 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;
}

here when I click the "ADD" button I want the below code to be execute:

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

the click listener for Add is working fine because I have used the Toast to check whether it was working fine or not, and it executed the Toast perfectly, however, the above code doesn't get executed, and I don't know why. Can anyone help me out?

Thank you in advance.

解决方案

I have finally resolved the issue by using another constant where I save the click value and check the value and used it in conditional statements and the issue was resolve.

这篇关于有关ExpandableListView子视图的Button ClickListener的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆