列表包含checkBox错位以及选中事件

效果图:
这里写图片描述
列表项使用了ExpandableTextView,在上一篇博客中提到了用法和使用中遇到的坑:使用ExpandableTextView遇到的坑,在列表中,包含了checkBox,比较容易出现的问题就是在列表中,选中项会错位,原因参照资料:Android ListView CheckBox状态错乱。我在程序中使用了Map来保存是否选中的状态,第二个问题就是点击事件,如果写在setOnCheckedChangeListener中处理,每次滑动时,就会调用,理论上应该是点击的时候再进行操作,所以把处理逻辑写在了setOnClickListener里,错误代码:

   viewHolder2.checkBoxSignService.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        isChecks.put(position,isChecked);
                        if(isChecked){
                            totalPrice += priceList.get(position);
                            checkCount++;
                        }else{
                            totalPrice -= priceList.get(position);
                            checkCount--;
                        }

                        tvNotice.setText("您总共选择"+checkCount+"项服务,总价共"+totalPrice+"元/年");
                    }
                });

正确的代码:
Activity代码:

public class SignServiceActivity extends BaseCompatActivity {

    @BindView(R.id.tv_sure)
    TextView tvSure;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.lv_signservice)
    ListView lvSignservice;
    @BindView(R.id.tv_notice)
    TextView tvNotice;
    @BindView(R.id.activity_sign_service)
    LinearLayout activitySignService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_service);
        ButterKnife.bind(this);

        toolbar.setNavigationIcon(R.mipmap.ic_navigation);
        toolbar.setTitle(R.string.health_data);
        toolbar.setTitleTextAppearance(this, R.style.Theme_ToolBar_Base_Title);//修改主标题的外观,包括文字颜色,文字大小等
        toolbar.setNavigationOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                SignServiceActivity.this.finish();
            }
        });

        tvNotice.setText("您总共选择0项服务,总价共0元/年");

        lvSignservice.setAdapter(new SingServiceAdapter(SignServiceActivity.this));
    }


    private class SingServiceAdapter extends BaseAdapter{

        Context mContext;
        LayoutInflater inflater;
        private final SparseBooleanArray mCollapsedStatus;
        final int TYPE_1 = 0;
        final int TYPE_2 = 1;
        private final String[] sampleStrings;
        //标记checkBox是否选中
        private final Map<Integer,Boolean> isChecks = new HashMap<>();
        //测试价格
        private ArrayList<Double> priceList = new ArrayList<>();
        //计算总价格
        private Double totalPrice = 0.0;
        //选择几项
        private int checkCount = 0;
        //各个布局的控件资源
        class ViewHolder1 {
            TextView textViewTitle;
        }

        class ViewHolder2 {
            CheckBox checkBoxSignService;
            ExpandableTextView expandableTextView;
            TextView textViewPrice;

        }

        public SingServiceAdapter(Context mContext) {
            this.mContext = mContext;
            inflater = LayoutInflater.from(mContext);
            mCollapsedStatus = new SparseBooleanArray();
            sampleStrings = mContext.getResources().getStringArray(R.array.sampleStrings);

            for (int i = 0;i < sampleStrings.length;i++){
                isChecks.put(i,false);
                priceList.add(i*1.0);
            }
        }

        @Override
        public int getCount() {
            return sampleStrings.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            int type = getItemViewType(position);
            ViewHolder1 viewHolder1 = null;
            ViewHolder2 viewHolder2 = null;
            if (convertView == null) {
                if (type == TYPE_1) {
                    convertView = LayoutInflater.from(mContext).inflate(R.layout.signservice_item1, null);
                    viewHolder1 = new ViewHolder1();
                    viewHolder1 .textViewTitle = (TextView) convertView.findViewById(R.id.tv_title);
                    convertView.setTag(viewHolder1);
                } else if (type == TYPE_2) {
                    viewHolder2 = new ViewHolder2();
                    convertView = LayoutInflater.from(mContext).inflate(R.layout.signservice_item2, null);
                    viewHolder2.checkBoxSignService = (CheckBox) convertView.findViewById(R.id.cb_signService);
                    viewHolder2.expandableTextView = (ExpandableTextView) convertView.findViewById(R.id.expand_text_view);
                    viewHolder2.textViewPrice = (TextView) convertView.findViewById(R.id.tv_price);
                    convertView.setTag(viewHolder2);
                }
            } else {
                if (type == TYPE_1) {
                    viewHolder1 = (ViewHolder1) convertView.getTag();
                } else if (type == TYPE_2) {
                    viewHolder2 = (ViewHolder2) convertView.getTag();
                }
            }

            if (type == TYPE_1) {
                if(position==0){
                    viewHolder1.textViewTitle.setText("高血压");
                }else if(position==4){
                    viewHolder1.textViewTitle.setText("糖尿病");
                }

            }
            if (type == TYPE_2) {
                viewHolder2.expandableTextView.setText(sampleStrings[position], mCollapsedStatus, position);
                viewHolder2.textViewPrice.setText("费用\n"+priceList.get(position)+"元/每年");

                final ViewHolder2 finalViewHolder = viewHolder2;
                viewHolder2.checkBoxSignService.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        boolean isChecked = finalViewHolder.checkBoxSignService.isChecked();

                        isChecks.put(position,isChecked);
                        if(isChecked){
                            totalPrice += priceList.get(position);
                            checkCount++;
                        }else{
                            totalPrice -= priceList.get(position);
                            checkCount--;
                        }

                        tvNotice.setText("您总共选择"+checkCount+"项服务,总价共"+totalPrice+"元/年");

                    }
                });

                viewHolder2.checkBoxSignService.setChecked(isChecks.get(position));

            }

            return convertView;
        }

        @Override
        public int getViewTypeCount() {
            return 2;
        }

        @Override
        public int getItemViewType(int position) {
            if (position == 0 || position == 4) {
                return TYPE_1;
            } else {
                return TYPE_2;
            }
        }
    }
}

列表项布局文件:
signservice_item1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/paleTurquoise3"
    android:orientation="vertical"
    android:padding="5dp">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/black"
        android:textSize="16sp"
        android:text="测试"/>
</LinearLayout>

signservice_item2.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:expandableTextView="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <CheckBox
        android:id="@+id/cb_signService"
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_margin="5dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="40dp"
        android:layout_marginRight="85dp"
        android:orientation="vertical">

        <com.ms.square.android.expandabletextview.ExpandableTextView
            android:id="@+id/expand_text_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            expandableTextView:animAlphaStart="1"
            expandableTextView:maxCollapsedLines="4">

            <TextView
                android:id="@id/expandable_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="sans-serif-light"
                android:textColor="#666666"
                android:textSize="16sp" />

            <ImageButton
                android:id="@id/expand_collapse"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right|bottom"
                android:background="@android:color/transparent"
                android:padding="16dp" />
        </com.ms.square.android.expandabletextview.ExpandableTextView>
    </LinearLayout>

    <TextView
        android:id="@+id/tv_price"
        android:layout_width="70dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_margin="10dp"
        android:gravity="center"
        android:fontFamily="sans-serif-light"
        android:textColor="#666666"
        android:textSize="16sp" />
</RelativeLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值