【问题标题】:onClick: update content of expandable ListViewonClick:更新可展开ListView的内容
【发布时间】:2019-12-22 18:37:53
【问题描述】:

我正在使用以下结构将数据 (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>

但我的主要问题是按钮/CardView 的 OnClickBehaviour 应该将折扣应用于 OK 按钮旁边的价格。客户做出选择后,OK 按钮应该可以访问我 CardView 的折扣选项。

这是适配器的 ListGroup:

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


    }

【问题讨论】:

    标签: java android android-recyclerview expandablelistview


    【解决方案1】:

    尝试使用 RadioGroup 和 RadioButtons 来选择折扣并在适配器内添加一个公共方法。我假设 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();
    }
    }
    

    希望有帮助!

    【讨论】:

    • 您好,先生,这应该可以。 CompoundButton 的用法对我来说不是很清楚,为什么我们不使用 radioGroup.setOnCheckedChangeListener();反而?谢谢你:)!!
    • 是的,您可以使用 radioGroup.setOnCheckedChangeListener()。我认为您正在解析数据并将文本单独设置为 RadioButton,因此我使用 CompoundButton.OnCheckedChangeListener 用于单个 RadioButton。
    【解决方案2】:

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

    那么您应该使用带有您可以设置样式的 RadioButtons 的 RadioGroup。

    应应用的 Buttons/CardView 的 OnClickBehaviour 折扣到确定按钮旁边的价格

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

    【讨论】:

    • 是的,但是,“packageList.get(groupPosition).setPrice(price-discount)”只能在适配器内部访问,而我的 RadioGroup 在主类的适配器外部。因此,我无法使用 onClickListener 在适配器外部访问它(或者在这种情况下是 e onSelectedListener?)
    猜你喜欢
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-07
    • 1970-01-01
    相关资源
    最近更新 更多