onClick:更新可展开列表视图的内容



我正在使用以下结构将数据(Json(从服务器解析到我的可扩展列表视图:

{
"package":[ 
"title":"Selection 1",
"price": 200,
"content":[stuff inside the exp listview]
]
} ,
{
"discount":[ 
"title:"discount 1",
"amount": 100
]
}

结果应如下所示: 截图

解析数据不是问题,我有两个问题:

折扣按钮应该有一个按钮行为,但只有一个折扣应该是可选择的(可点击的(,onClick:折扣应用于上面的可扩展列表视图

我想到了单个按钮的 CardView,然后它将乘以我的 json 中的折扣金额(由于适配器(:

<androidx.cardview.widget.CardView
android:layout_width="91dp"
android:layout_height="132dp"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="350dp" >
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="35dp"
android:text="TextView"
tools:text="Discount" />
</androidx.cardview.widget.CardView>

但我这里的主要问题是按钮/卡片视图的 OnClickBehavior,它应该将折扣应用于确定按钮旁边的价格。客户做出选择后,"确定"按钮应访问我的 CardView 的折扣选择。

这是适配器的列表组:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/lblListHeaderLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/default_padding"
android:orientation="horizontal"
>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingStart="?android:attr/expandableListPreferredItemPaddingLeft"
android:paddingEnd="@dimen/feed_item_padding_left_right"
android:layout_weight="0.9"
>
<TextView
android:id="@+id/lblListHeader"
android:textSize="@dimen/text_title_list_header"
android:textColor="@color/Textwhite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/mont_bold"
/>
<TextView
android:id="@+id/lblListHeader_Price"
android:textSize="@dimen/text_title_list_header"
android:textColor="@color/Textwhite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:fontFamily="@font/mont_bold"
/>
</RelativeLayout>
<Button
android:id="@+id/lblListHeaderButton"
android:layout_width="60dp"
android:layout_height="30dp"
android:background="@drawable/bg_btn_ripple_dark"
android:text="@string/btn_ok"
android:textColor="@color/Textwhite"
android:fontFamily="@font/mont_bold"
android:focusable="false"
/>
</LinearLayout>

这是我的适配器类:

public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> listDataHeader,listDataHeaderPrice;
private HashMap<String,List<String>> listHashMap;
private List<Package> packageList;
public ExpandableListAdapter(Context context, List<Package> packageList) {
this.context = context;
this.packageList= packageList;
}

@Override
public int getGroupCount() {
return packageList.size();
}

@Override
public int getChildrenCount(int groupPosition) {
return packageList.get(groupPosition).getContent().size();
}
@Override
public Object getGroup(int groupPosition) {
return listDataHeader.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return packageList.get(groupPosition).getContent();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}

@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View view, ViewGroup parent) {
String headerTitle = (String) packageList.get(groupPosition).getTitle();
/** HEADER TEXT HERE */
float headerPrice = packageList.get(groupPosition).getPrice();
if(view==null) {
LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.vip_package_listgroup,null);
}
final LinearLayout bgcolor = view.findViewById(R.id.lblListHeaderLayout);
TextView lblListHeader = (TextView)view.findViewById(R.id.lblListHeader);
TextView lblListHeaderPrice = (TextView)view.findViewById(R.id.lblListHeader_Price);
Button lblListHeaderButton = view.findViewById(R.id.lblListHeaderButton);

lblListHeaderButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent (context, drinks_selection.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
lblListHeader.setText(headerTitle);
lblListHeaderPrice.setText(String.format("%.02f",headerPrice).replace(".",",") +"USD");
return view;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent) {

final List<String> childTest = packageList.get(groupPosition).getContent();
final String childText =  childTest.get(childPosition);
if(view == null) {
LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.vip_package_listitem,null);
}
TextView txtListChild = (TextView)view.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return view;

}

尝试使用 RadioGroup 和 RadioButton 来选择折扣,并在适配器中添加一个公共方法。我假设单选按钮上的文本是折扣金额,所以 单选按钮的代码:

CompoundButton.OnCheckedChangeListener onCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b) {
mainAdapter.setDiscount(Float.valueOf(compoundButton.getText().toString()));
}
}
};
rb1.setOnCheckedChangeListener(onCheckedChangeListener);
rb2.setOnCheckedChangeListener(onCheckedChangeListener);
rb3.setOnCheckedChangeListener(onCheckedChangeListener);

适配器:

public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
//private List<String> listDataHeader, listDataHeaderPrice;
//private HashMap<String, List<String>> listHashMap;
private List<Package> packageList;
private float discount = 0; //Added
public ExpandableListAdapter(Context context, List<Package> packageList) {
this.context = context;
this.packageList = packageList;
}
@Override
public int getGroupCount() {
return packageList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return packageList.get(groupPosition).getContent().size();
}
@Override
public Object getGroup(int groupPosition) {
return packageList.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return packageList.get(groupPosition).getContent().get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View view, ViewGroup parent) {
String headerTitle = (String) packageList.get(groupPosition).getTitle();
/** HEADER TEXT HERE */
float headerPrice = packageList.get(groupPosition).getPrice();
if (view == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.vip_package_listgroup, null);
}
LinearLayout bgcolor = view.findViewById(R.id.lblListHeaderLayout);
TextView lblListHeader = (TextView) view.findViewById(R.id.lblListHeader);
TextView lblListHeaderPrice = (TextView) view.findViewById(R.id.lblListHeader_Price);
Button lblListHeaderButton = view.findViewById(R.id.lblListHeaderButton);
lblListHeaderButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, drinks_selection.class);
intent.putExtra("discount", discount); //Added
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
lblListHeader.setText(headerTitle);
float endPrice = headerPrice + discount; //Added
if(endPrice < 0) endPrice = 0; //Added
lblListHeaderPrice.setText(String.format("%.02f", endPrice).replace(".", ",") + "USD"); //Changed
return view;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent) {
List<String> childTest = packageList.get(groupPosition).getContent();
String childText = childTest.get(childPosition);
if (view == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.vip_package_listitem, null);
}
TextView txtListChild = (TextView) view.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return view;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return false;
}
//Added this public method
public void setDiscount(float discount){
this.discount = discount;
notifyDataSetChanged();
}
}

希望对您有所帮助!

折扣按钮应该有一个按钮行为,但只有一个 折扣应该是可挑选的

然后,您应该使用带有可以设置样式的单选按钮的单选按钮组。

按钮/卡片视图的点击行为,应应用 折扣到确定按钮旁边的价格

单击折扣时,您需要更改数据集,我相信在您的情况下它是txtListChild。在packageList.get(groupPosition).setPrice(price-discount)

最新更新