onClick:更新可扩展ListView的内容
onClick: update content of expandable ListView
我正在使用以下结构将数据 (Json) 从服务器解析到我的可扩展 ListView:
{
"package":[
"title":"Selection 1",
"price": 200,
"content":[stuff inside the exp listview]
]
} ,
{
"discount":[
"title:"discount 1",
"amount": 100
]
}
结果应该是这样的:
Screenshot
解析数据不是问题,我有两个问题:
折扣按钮应该有一个按钮行为,但只有一个折扣应该是可选的(可点击的),onClick:折扣被应用到上面的 ExpandableListView
我想到了一个 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>
但我的主要问题是 Buttons/CardView 的 OnClickBehaviour,它应该将折扣应用到 OK 按钮旁边的价格。客户做出选择后,OK 按钮应该访问我的 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>
这是我的适配器 class:
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;
}
The Discount Buttons should have a button behaviour but only one
discount should be pickable
那么您应该使用带有可以设置样式的 RadioButtons 的 RadioGroup。
OnClickBehaviour of the Buttons/CardView which should apply the
discount to the price next to the OK Button
您需要在点击折扣时更改数据集,我相信您的情况是 txtListChild。在 packageList.get(groupPosition).setPrice(price-discount)
尝试使用 RadioGroup 和 RadioButtons 选择折扣并在适配器中添加 public 方法。我假设 RadioButton 上的 Text 是折扣金额,所以
单选按钮代码:
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();
}
}
希望对您有所帮助!
我正在使用以下结构将数据 (Json) 从服务器解析到我的可扩展 ListView:
{
"package":[
"title":"Selection 1",
"price": 200,
"content":[stuff inside the exp listview]
]
} ,
{
"discount":[
"title:"discount 1",
"amount": 100
]
}
结果应该是这样的: Screenshot
解析数据不是问题,我有两个问题:
折扣按钮应该有一个按钮行为,但只有一个折扣应该是可选的(可点击的),onClick:折扣被应用到上面的 ExpandableListView
我想到了一个 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>
但我的主要问题是 Buttons/CardView 的 OnClickBehaviour,它应该将折扣应用到 OK 按钮旁边的价格。客户做出选择后,OK 按钮应该访问我的 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>
这是我的适配器 class:
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;
}
The Discount Buttons should have a button behaviour but only one discount should be pickable
那么您应该使用带有可以设置样式的 RadioButtons 的 RadioGroup。
OnClickBehaviour of the Buttons/CardView which should apply the discount to the price next to the OK Button
您需要在点击折扣时更改数据集,我相信您的情况是 txtListChild。在 packageList.get(groupPosition).setPrice(price-discount)
尝试使用 RadioGroup 和 RadioButtons 选择折扣并在适配器中添加 public 方法。我假设 RadioButton 上的 Text 是折扣金额,所以 单选按钮代码:
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();
}
}
希望对您有所帮助!