在自定义列表视图中删除多个所选项目不适用于ImageButton

时间:2018-09-05 11:34:00

标签: android listview imagebutton

对我来说这是非常必要的。我正在尝试创建一个delete multiple selected items ListView。代码正在正确运行,但是当我在custom_list_row.layout中添加一个ImageButton时,它无法正常工作。我不知道这个问题。也许有相同的问题,但我没有找到。

代码:

使用后

smsList.setAdapter(customListViewAdapter);

尝试使用此代码删除所选项目

smsList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
smsList.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
    @Override
    public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
        final int checkedCount = smsList.getCheckedItemCount();
        // Set the CAB title according to total checked items
        mode.setTitle(checkedCount + " Selected");
        // Calls toggleSelection method from ListViewAdapter Class
        customListViewAdapter.toggleSelection(position);
    }

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        mode.getMenuInflater().inflate(R.menu.delete_menu_option, menu);
        return true;
    }

    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        return false;
    }

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        switch (item.getItemId()) {

            case R.id.delete:
                // call getSelectedIds method from customListViewAdapter
                SparseBooleanArray selected = customListViewAdapter.getSelectedIds();
                // Captures all selected ids with a loop
                for (int i = (selected.size() - 1); i >= 0; i--) {
                    if (selected.valueAt(i)) {
                        SmsClass selectedListItem = (SmsClass) customListViewAdapter.getItem(selected.keyAt(i));
                        // Remove selected items using ids
                        customListViewAdapter.remove(selectedListItem);
                    }
                }
                mode.finish();
                return true;
            default:
                return false;
        }
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {
        customListViewAdapter.removeSelection();
    }
});

适配器代码:

public class CustomListViewAdapter extends ArrayAdapter{

    Context context;
    private SparseBooleanArray selectedListItemsIds;
    List multipleSelectionList;

    public CustomListViewAdapter(Context context, int resourceId, List items) {
        super(context, resourceId, items);
        this.context = context;
        selectedListItemsIds = new SparseBooleanArray();
        this.multipleSelectionList = items;
    }
    /*private view holder class*/
    private class ViewHolder {
        ImageView imgCountryFlag;
        TextView txtCountryName;
        TextView txtCountryCode;
        ImageButton click;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        SmsClass rowItem = (SmsClass) getItem(position);

        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate( R.layout.list_item_row, null);
            holder = new ViewHolder();
            holder.txtCountryCode = (TextView) convertView.findViewById(R.id.name);
            holder.txtCountryName = (TextView) convertView.findViewById(R.id.date);
            holder.click = (ImageButton)convertView.findViewById( R.id.click);

            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();

        holder.txtCountryCode.setText(rowItem.getBody());
        holder.txtCountryName.setText(rowItem.getNumberInside());
        return convertView;
    }

    public void remove(SmsClass object) {
        multipleSelectionList.remove(object);
        notifyDataSetChanged();
    }

    public void toggleSelection(int position) {
        selectView(position, !selectedListItemsIds.get(position));
    }

    public void removeSelection() {
        selectedListItemsIds = new SparseBooleanArray();
        notifyDataSetChanged();
    }

    public void selectView(int position, boolean value) {
        if (value)
            selectedListItemsIds.put(position, value);
        else
            selectedListItemsIds.delete(position);
        notifyDataSetChanged();
    }

    public int getSelectedCount() {
        return selectedListItemsIds.size();
    }

    public SparseBooleanArray getSelectedIds() {
        return selectedListItemsIds;
    }
}

2 个答案:

答案 0 :(得分:2)

最后,我找到了答案: 我们必须设置

    android:descendantFocusability="blocksDescendants"

在根列表项元素中并设置

    android:focusableInTouchMode="true"

ImageButton视图中。

答案 1 :(得分:-1)

我正在您的getView方法中添加onClickListner ..请尝试

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    SmsClass rowItem = (SmsClass) getItem(position);

    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate( R.layout.list_item_row, null);
        holder = new ViewHolder();
        holder.txtCountryCode = (TextView) convertView.findViewById(R.id.name);
        holder.txtCountryName = (TextView) convertView.findViewById(R.id.date);
        holder.click = (ImageButton)convertView.findViewById( R.id.click);

        convertView.setTag(holder);
    } else
        holder = (ViewHolder) convertView.getTag();

    holder.txtCountryCode.setText(rowItem.getBody());
    holder.txtCountryName.setText(rowItem.getNumberInside());
    holder.click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              convertView.performClick();
            }
        });
    return convertView;
}