我正在尝试为我的餐厅制作一个应用程序,这是我之前关于我遇到的问题的问题并已解决,但是,您会在 ChildView 中注意到一些按钮,这里是“添加”,就像对于我以前的查询here
您可以看到我有“添加”按钮,我在哪里单击我希望显示我的相对布局,但是,这里有一些问题是我使用了自定义的 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);
添加的点击监听器工作正常,因为我使用 Toast 检查它是否工作正常,并且它完美地执行了 Toast,但是,上面的代码没有被执行,我不知道为什么。谁能帮我吗?
先感谢您。