Search code examples
androidexpandablelistviewchildviews

Question regarding the ExpandableListView ChildView's Button ClickListener


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.


Solution

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