自定义ListView与复选框在Android中滚动时更改位置

时间:2017-08-18 08:14:01

标签: android listview checkbox checkboxlist

我是Android的新手,正在进行网络视图演示,我已经实现了它。但问题是,当我选中任何复选框并滚动列表视图时,选中的位置会发生变化。

我已经尝试了一些来自堆栈的链接而没有运气,所以我请求有人可以帮我解决这个问题 我的适配器如下,

适配器

    public class FilterAdapter extends BaseAdapter {

        private static final int TYPE_ITEM = 0;
        private static final int TYPE_SEPARATOR = 1;
        private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;

        private ArrayList<String> mData = new ArrayList<String>();
        private LayoutInflater mInflater;

        private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();

        public FilterAdapter(Context context) {
            mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        public void addItem(final String item) {
            mData.add(item);
            notifyDataSetChanged();
        }

        public void addSeparatorItem(final String item) {
            mData.add(item);
            // save separator position
            mSeparatorsSet.add(mData.size() - 1);
            notifyDataSetChanged();
        }

        @Override
        public int getItemViewType(int position) {
            return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
        }

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

        public int getCount() {
            return mData.size();
        }

        public String getItem(int position) {
            return mData.get(position);
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            int type = getItemViewType(position);
            System.out.println("getView " + position + " " + convertView + " type = " + type);

            if (convertView == null) {
                holder = new ViewHolder();
                switch (type) {
                    case TYPE_ITEM:
                        convertView = mInflater.inflate(R.layout.raw_filter, null);
                        holder.textView = (TextView) convertView.findViewById(R.id.tv_filter);
                        holder.chk_filter = (CheckBox) convertView.findViewById(R.id.chk_filter);
                        break;
                    case TYPE_SEPARATOR:
                        convertView = mInflater.inflate(R.layout.raw_headr, null);
                        holder.textView = (TextView) convertView.findViewById(R.id.tv_hdr);
                        break;
                }
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.textView.setText(mData.get(position));
            return convertView;
        }

}

class ViewHolder {
    public TextView textView;
    public CheckBox chk_filter;
}

1 个答案:

答案 0 :(得分:0)

clickListener方法中添加getView(),它会修复整个listView中的设置复选框,如下所示:

checkBoxSelected.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                instrumentClickListener.onCheckBoxClick(instrumentDTO, checkBoxSelected.isChecked(), position);
                Log.d(TAG, "checked instruments is check: " + checkBoxSelected.isChecked() + " \ninstrument: " + position);
            }
        });