只需点击一下即可使用setMultiChoiceModeListener?

时间:2015-11-30 23:53:29

标签: android android-layout android-activity

我想在短暂的一次点击时使用setMultiChoiceModeListener。 但它只有在长时间点击时才有效..如何短暂点击一下设置工作setMultiChoiceModeListener?

例如:" Root Explorer"应用程序执行此操作。

FavoritesActivity.java

public class FavoritesActivity extends AppCompatActivity {
    private ListView listView;
    private ArrayList<FavoritesList> favList;
    private FavoritesAdapter favAdapter;
    private int checkedCount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_favorites);

        DBHandler dbhandler = DBHandler.open(this);
        Cursor cursorFav = dbhandler.selectFav();

        favList = new ArrayList<FavoritesList>();

        if (cursorFav.getCount() != 0) {
            while (cursorFav.moveToNext()) {
                String no = cursorFav.getString(cursorFav.getColumnIndex(DBHelper.FAV_COL_1));
                String title = cursorFav.getString(cursorFav.getColumnIndex(DBHelper.FAV_COL_2));
                String page = cursorFav.getString(cursorFav.getColumnIndex(DBHelper.FAV_COL_3));

                favList.add(new FavoritesList(no, title, page));
            }
            listView = (ListView) findViewById(R.id.fav_listview);
            favAdapter = new FavoritesAdapter(this, R.layout.activity_favorites_entry, favList);
            listView.setAdapter(favAdapter);

            final boolean[] delete_status = {false};
            listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
            listView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
                @Override
                public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
                    checkedCount = listView.getCheckedItemCount();
                    mode.setTitle(checkedCount + " Selected");
                    favAdapter.toggleSelection(position);
                }

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

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

                @Override
                public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                    SparseBooleanArray selected = favAdapter.getSelectedIds();

                    switch (item.getItemId()) {
                        case R.id.action_check_all:
                            if (selected.size() != favList.size()) {
                                favAdapter.removeSelection();
                                checkedCount = favList.size();
                                mode.setTitle(checkedCount + " Selected");

                                for (int i = 0; i < favList.size(); i++) {
                                    listView.setItemChecked(i, true);
                                }
                            }

                            return true;

                        case R.id.action_delete:
                            DBHandler dbhandler = DBHandler.open(FavoritesActivity.this);

                            for (int i = (selected.size() - 1); i >= 0; i--) {
                                FavoritesList selectedItem = favAdapter.getItem(selected.keyAt(i));
                                favAdapter.remove(selectedItem);
                                dbhandler.deleteFav(selectedItem.getNo_int());
                            }

                            delete_status[0] = true;

                            dbhandler.close();
                            mode.finish();
                            return true;

                        default:
                            return false;
                    }
                }

                @Override
                public void onDestroyActionMode(ActionMode mode) {
                    SparseBooleanArray selected = favAdapter.getSelectedIds();

                    if (!delete_status[0]) {
                        for (int i = selected.size() - 1; i >= 0; i--) {
                            FavoritesList selectedItem = favAdapter.getItem(selected.keyAt(i));
                            selectedItem.setChecked(!selectedItem.getChecked());
                        }
                    }

                    favAdapter.removeSelection();
                }
            });
        }

        dbhandler.close();
    }
}

FavoritesAdapter.java

public class FavoritesAdapter extends ArrayAdapter<FavoritesList> {
    Context context;
    LayoutInflater inflater;
    List<FavoritesList> favList;
    private SparseBooleanArray mSelectedItemsIds;

    public FavoritesAdapter(Context context, int resourceId, List<FavoritesList> favList) {
        super(context, resourceId, favList);

        mSelectedItemsIds = new SparseBooleanArray();
        this.context = context;
        this.favList = favList;
        inflater = LayoutInflater.from(context);
    }

    private static class FavHolder {
        TextView favNo;
        TextView favTitle;
        CheckBox favCheckBox;
        TextView favPage;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        final FavHolder holder;

        if (view == null) {
            holder = new FavHolder();
            view = inflater.inflate(R.layout.activity_favorites_entry, null);

            holder.favNo = (TextView) view.findViewById(R.id.fav_listview_no);
            holder.favTitle = (TextView) view.findViewById(R.id.fav_listview_title);
            holder.favCheckBox = (CheckBox) view.findViewById(R.id.fav_listview_chkbox);
            holder.favPage = (TextView) view.findViewById(R.id.fav_listview_page);

            view.setTag(holder);
        } else {
            holder = (FavHolder) view.getTag();
        }

        holder.favNo.setText(favList.get(position).getNo());
        holder.favTitle.setText(favList.get(position).getTitle());
        holder.favCheckBox.setChecked(favList.get(position).getChecked());
        holder.favPage.setText(favList.get(position).getPage());

        return view;
    }

    public void remove(FavoritesList object) {
        favList.remove(object);
        notifyDataSetChanged();
    }

    public List<FavoritesList> getFavList() {
        return favList;
    }

    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);
            favList.get(position).setChecked(true);
        } else {
            mSelectedItemsIds.delete(position);
            favList.get(position).setChecked(!favList.get(position).getChecked());
        }
        notifyDataSetChanged();
    }

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

    public SparseBooleanArray getSelectedIds() {
        return mSelectedItemsIds;
    }
}

class FavoritesList {
    private String no;
    private String title;
    private String page;
    private boolean checked;

    public FavoritesList(String no, String title, String page) {
        super();
        this.no = no;
        this.title = title;
        this.page = page;
        this.checked = false;
    }

    public String getNo() { return no;}
    public int getNo_int() { return Integer.parseInt(no); }
    public String getTitle() { return title; }
    public boolean getChecked() { return checked; }
    public String getPage() { return page; }

    public void setNo(String no) { this.no = no; }
    public void setTitle(String title) { this.title = title; }
    public void setChecked(boolean checked) { this.checked = checked; }
    public void setPage(String page) { this.page = page; }
}

FavoritesListView_entry.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="match_parent"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <TextView
            android:id="@+id/fav_listview_no"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:textSize="24sp" />

        <TextView
            android:id="@+id/fav_listview_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textSize="24sp" />
    </LinearLayout>

    <CheckBox
        android:id="@+id/fav_listview_chkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:clickable="false" />

    <TextView
        android:id="@+id/fav_listview_page"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone" />
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

回答我自己的问题...... 只需在FavoritesActivity.java中添加setOnItemClickListener

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        listView.setItemChecked(position, !favAdapter.getItem(position).getChecked());
    }
});