如何将ActionMode用于" setOnItemClickListener" ListView-Android中的方法?

时间:2015-06-04 14:24:01

标签: android listview onitemclicklistener actionmode

我的项目中有listView,有一个ActionMode,显示为点击每个listView的项目。

我这样做:

public class MyActivity extends Activity  {
...
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
...
 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            public boolean onCreateActionMode(final ActionMode mode, Menu menu) {
                mode.setTitle("TITLE");
                getMenuInflater().inflate(R.menu.menu, menu);
                return true;
            }

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

            @Override
            public boolean onActionItemClicked(final ActionMode mode, MenuItem item) {
                switch (item.getItemId()) {
                   ...buttons like this
                    case R.id.doneBTN:


                        break;
                }
                return true;
            }

        }
    });

}

RETURNs中存在问题。此注释显示每个RETURN &#34;无法从void返回类型的方法中返回值&#34; ...所以我该如何解决?

1 个答案:

答案 0 :(得分:0)

看起来你已经搞砸了很多听众。

AdapterView.OnItemClickListener只有一种方法,即onItemClick()。请查看HERE以获取有关该

的更多信息

要启动ActionMode,您可以使用

activity.startActionMode(new MyActionModeListener())

你的听众可以是这样的:

private class MyActionModeListener implements ActionMode.Callback {

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        MenuInflater inflater = MyActivity.this.getSupportMenuInflater();
        inflater.inflate(R.menu.my_activity_action_mode, menu);
        return true;
    }

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

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

        switch (item.getItemId()) {
            // handle clicks here
        }

        mode.finish();
        return true;
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {}
}

要基于列表项单击实际启动ActionMode,请使用类似

的内容
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        MyActivity.this.startActionMode(new MyActionModeListener());
    }
}