3

我想做一个这样的应用

在 AbsListView 中批处理上下文操作

我已按照Google 的指南在AbsListView. 就我而言,我AbsListView在 a 中实现了,Fragment因为我希望用户能够更改布局,我有几个布局用于Activity(a ListView、aGridView和另一个AbsListView子类)。我还在AbsListView.MultiChoiceModeListener一个单独的类中实现了它,因为我想在几个片段和活动中重用它。

我的问题是:当我长按列表项时,上下文操作栏不显示。然而,它响应了一次点击(表明选择器正在工作)。

我一遍又一遍地检查并尝试错误我的代码,以确保我正确地遵循了指南上的所有内容,浏览了许多关于这个主题的故障排除,但我仍然无法弄清楚为什么上下文操作栏没有别上来

这是片段之一:

public class List extends Bookshelf {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_list, container, false);
        ListView list = (ListView) rootView.findViewById(R.id.book_list);
        list.setAdapter(new BookListAdapter(getActivity()));

        list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
        list.setMultiChoiceModeListener(new MultiChoiceCallback(list));

        return rootView;
    }

    private static class BookListAdapter extends BaseAdapter {
        private LayoutInflater mInflater;

        public BookListAdapter(Context context) {
            mInflater = LayoutInflater.from(context);
        }

        public int getCount() {
            return 100;
        }

        public Object getItem(int position) {
            return position;
        }

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

        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;

            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.view_item_book_1text, null);
                holder = new ViewHolder();
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            return convertView;
        }

        static class ViewHolder {
            TextView title;
            ImageView cover;
        }
    }
}

这是 MultiChoiceModeListener ( MultiChoiceCallback.java):

public class MultiChoiceCallback implements MultiChoiceModeListener {
    AbsListView usingView;

    public MultiChoiceCallback(AbsListView view) {
        usingView = view;
    }

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        switch (item.getItemId()) {
        case R.id.bookDetails:
            break;
        case R.id.category:
            break;
        case R.id.delete:
            break;
        }
        return false;
    }

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

    @Override
    public void onDestroyActionMode(ActionMode mode) {
    }

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

    @Override
    public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
        final int checkedCount = usingView.getCheckedItemCount();
        switch (checkedCount) {
            case 0:
                mode.setTitle(null);
                break;
            case 1:
                mode.setTitle("1 item selected");
                break;
            default:
                mode.setTitle("" + checkedCount + " items selected");
                break;
        }
    }
}

Fragment ( layout/fragment_list.xml) 的布局:

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/book_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</ListView>

膨胀的视图列表项 ( layout/view_item_book_1text.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="51dp"
    android:background="@color/blue_when_clicked"
    android:clickable="true"
    android:focusable="true"
    android:layout_marginLeft="3dp"
    android:layout_marginRight="3dp"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/cover"
        android:contentDescription="@string/cover"
        android:layout_width="75dp"
        android:layout_height="45dp"
        android:layout_marginTop="3dp"
        android:layout_marginBottom="3dp"
        android:scaleType="fitCenter"
        android:src="@drawable/cover_02" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="3dp"
        android:text="A Book Title"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

每个列表项 ( color/blue_when_clicked.xml) 的颜色选择器:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/color_light_blue" android:state_pressed="true"/>  <!-- pressed -->
    <item android:drawable="@drawable/color_light_gray" android:state_focused="true"/>  <!-- focused -->
    <item android:drawable="@drawable/color_light_gray" android:state_activated="true"/> <!-- selected -->
</selector>

任何帮助将不胜感激!

4

2 回答 2

3

我还没有测试过,但乍一看,我认为你的问题出在你的onCreateActionMode (ActionMode mode, Menu menu)方法上。

根据文档- 您应该返回true以创建ActionMode

如果应该创建操作模式,则为 true,如果应中止进入此模式,则为 false。


onCreateOptionsMenu (Menu menu)该机制与方法 from 中的相同Activity文档在这里以更清晰的方式说明了这一点:

您必须返回 true 才能显示菜单;如果您返回 false 则不会显示。


编辑:

您是否尝试过从您提到的教程中启动代码?ActionMode我已经完成了on的最小实现ListView。甚至没有菜单资源膨胀,只要返回true那里,长按就会出现ActionMode。

final ListView listView = new ListView(getActivity());
listView.setAdapter(new BookListAdapter(getActivity()));
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {
    @Override
    public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
    }

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        return false;
    }

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        // not even need to inflate anything in order to ActionMode to appear
        return true;
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {
    }

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

来自适配器的代码(修改了您的适配器):

private static class BookListAdapter extends BaseAdapter {
    private LayoutInflater mInflater;

    public BookListAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
    }

    public int getCount() {
        return 100;
    }

    public Object getItem(int position) {
        return position;
    }

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

    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mInflater.inflate(android.R.layout.simple_list_item_1, null);
        }

        ((TextView)convertView).setText("TEST");
        return convertView;
    }
}


结果的屏幕截图 - 正如您所看到的那样ActionMode

在此处输入图像描述

于 2014-07-21T13:22:48.843 回答
0

啊哈!发现错误!这是因为

android:clickable="true"
android:focusable="true"

在里面layout/view_item_book_1text.xml

不好意思打扰大家了!希望这成为其他人的故障排除说明

于 2014-07-21T20:10:22.263 回答