我想删除列表视图(Gmail样式)中的多个选定项目(行)。
如果我选择(使用longClick)一行,则不会发生任何事情。
我在互联网上找到了此代码,并尝试将其插入到我的项目中。

主要活动

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_lista, container,
            false);

    Lista = (ListView) rootView.findViewById(R.id.Lista);
    items = new ArrayList<ListViewItem>();
    items = GetLists.GetRecordList(rootView.getContext());
    adapter = new ListaAdapter(rootView.getContext(),
            R.layout.list_view_item, items);
    Lista.setAdapter(adapter);
    Lista.setMultiChoiceModeListener(this);
    Lista.setChoiceMode(Lista.CHOICE_MODE_MULTIPLE_MODAL);

    return rootView;
}

@Override
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menu) {
    switch (menu.getItemId()) {
    case R.id.menu_delete:
        SparseBooleanArray selected = adapter.getSelectedIds();
        for (int i = (selected.size() - 1); i >= 0; i--) {
            if (selected.valueAt(i)) {
                ListViewItem selectedItem = adapter.getItem(selected
                        .keyAt(i));
                adapter.remove(selectedItem);
            }
        }
        actionMode.finish();
        return true;

    default:
        return false;
    }
}

@Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
    actionMode.getMenuInflater().inflate(R.menu.delete_menu, menu);
    return false;
}

@Override
public void onDestroyActionMode(ActionMode arg0) {
    adapter.removeSelection();

}

@Override
public boolean onPrepareActionMode(ActionMode arg0, Menu arg1) {
    return false;
}

@Override
public void onItemCheckedStateChanged(ActionMode actionMode, int position,
        long arg2, boolean arg3) {
    final int checkedOut = Lista.getCheckedItemCount();
    actionMode.setTitle(checkedOut + " selezionato");
    adapter.toggleSelection(position);

}


这是适配器

public class ListaAdapter extends ArrayAdapter<ListViewItem> {

private Context context;
private ArrayList<ListViewItem> items;
private SparseBooleanArray mSelectedItemsIds;

public ListaAdapter(Context context, int resourceId,
        ArrayList<ListViewItem> items) {
    super(context, resourceId, items);
    mSelectedItemsIds = new SparseBooleanArray();
    this.context = context;
    this.items = items;
}

private class ViewHolder {
    ImageView imageView;
    TextView txtTitle;
    TextView txtSubTitle;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.list_view_item, null);
        holder = new ViewHolder();
        holder.txtTitle = (TextView) convertView
                .findViewById(R.id.TitoloPrincipale);
        holder.txtSubTitle = (TextView) convertView
                .findViewById(R.id.Sottotitolo);
        holder.imageView = (ImageView) convertView.findViewById(R.id.Image);
        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    ListViewItem list = getItem(position);
    holder.txtTitle.setText(list.getNomeFarmaco());
    holder.txtSubTitle.setText(list.getFasceOrarie());
    holder.imageView.setImageResource(list.getIcon());
    // convertView
    // .setBackgroundColor(mSelectedItemsIds.get(position)
    // : Color.TRANSPARENT);
    return convertView;

}

public void remove(ListViewItem item) {
    items.remove(item);
    notifyDataSetChanged();
}

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

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

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

    notifyDataSetChanged();
}

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

public SparseBooleanArray getSelectedIds() {
    return mSelectedItemsIds;
}


你有什么主意吗
ks

最佳答案

该代码与长按无关。 “ long”的唯一出现是方法的long参数。 :-)

This sample project演示了一种操作模式,该操作模式是基于列表行的长按启动的。密钥是onItemLongClick()

  @Override
  public boolean onItemLongClick(AdapterView<?> parent, View view,
                                 int position, long id) {
    getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    getListView().setItemChecked(position, true);

    return(true);
  }


在这里(将活动注册为OnItemLongClickListenerListView),我们打开CHOICE_MODE_MULTIPLE_MODAL并检查长按的项目,从而激活动作模式。

10-06 08:33