在Dialog Fragment Android中,选择模式多模态无法正常工作

时间:2016-05-30 11:47:24

标签: android listview android-fragments

我在片段上有一个ListView 我选择一个项目,我得到一个Dialog片段 这个Dialog片段有一个带有一些Answers的ListView,一个EditText和一个Dismiss Button

当我尝试选择多个项目作为答案时,我首先需要在列表中点按。这是为什么?即使出现同样的情况:我使用setOnItemLongClickListener或setOnItemClickListener

以下是onViewCreated和setOnItemLongClickListener

AnswerList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
    {

        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
        {
            AnswerList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
            Log.d("Select Item",String.valueOf(position));
            AnswerList.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener()
            {

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

                @Override
                public boolean onPrepareActionMode(ActionMode mode, Menu menu)
                {


                    return false;
                }

                @Override
                public boolean onActionItemClicked(ActionMode mode, MenuItem item)
                {
                    if (item.getItemId() == R.id.action_settings){
                        SparseBooleanArray selected = mAdapter.getSelectedIds();
                        short size = (short)selected.size();
                        for (byte I = 0; I<size; I++){
                            if (selected.valueAt(I)) {
                                String selectedItem = mAdapter.getItem(selected.keyAt(I));

                            }
                        }


                        // Close CAB (Contextual Action Bar)
                        mode.finish();
                        return true;
                    }
                    return false;
                }

                @Override
                public void onDestroyActionMode(ActionMode mode)
                {

                }

                @Override
                public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked)
                {
                    // Prints the count of selected Items in title
                    mode.setTitle(AnswerList.getCheckedItemCount() + " Selected");


                    // Toggle the state of item after every click on it
                    mAdapter.toggleSelection(position);
                }
            });

DialogFragment XML中ListView的一部分

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="220dp"
        android:id="@+id/AnswerList"
        android:layout_margin="4dp" />
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

AnswerList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
        Log.d("Select Item",String.valueOf(position));
        AnswerList.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener()
        {
....
}

必须在外面而不是嵌套在OnItemLongClickListener