【问题标题】:show Action Mode as LongClick on ListView in android在android中的ListView上将动作模式显示为LongClick
【发布时间】:2015-06-02 10:17:43
【问题描述】:

我想在我的项目中的 ListView 的每一行上通过 LongClicking 显示 ActionMode 菜单(以获得更多选项)。 但是当我点击它们时,什么也没有发生。 注意:我使用本教程编写此代码:http://wptrafficanalyzer.in/blog/creating-a-contextual-menu-bar-contextual-action-mode-for-a-single-view-in-android/ 这是我的代码:

public class MyActivity extends Activity  {

private ListView listView;

private ActionMode actionMode;
 ActionMode.Callback callback;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    listView = (ListView) findViewById(R.id.ListView);


    final ActionMode.Callback callback = new ActionMode.Callback() {
        /** Invoked whenever the action mode is shown. This is invoked immediately after onCreateActionMode */
        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }


        /** Called when user exits action mode */
        @Override
        public void onDestroyActionMode(ActionMode mode) {
            actionMode = null;

        }

        /** This is called when the action mode is created. This is called by startActionMode() */
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            mode.setTitle("Demo");
            getMenuInflater().inflate(R.menu.edit_vow, menu);
            return true;
        }

        /** This is called when an item in the context menu is selected */
        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch(item.getItemId()){
                case R.id.editBTN:
                    Toast.makeText(getBaseContext(), "Selected Action1 ", Toast.LENGTH_LONG).show();
                    mode.finish();  // Automatically exists the action mode, when the user selects this action
                    break;
                case R.id.deleteBTN:
                    Toast.makeText(getBaseContext(), "Selected Action2 ", Toast.LENGTH_LONG).show();
                    break;
                case R.id.doneBTN:
                    Toast.makeText(getBaseContext(), "Selected Action3 ", Toast.LENGTH_LONG).show();
                    break;
            }
            return true;
        }
    };

    //long-click to edit data
     View.OnLongClickListener listener = new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            if(actionMode!=null)
                return false;
            else
                actionMode = startActionMode(callback);
            return true;
        }
    };

    listView.setOnLongClickListener(listener);


}

请帮帮我,谢谢!

【问题讨论】:

    标签: android listview android-actionmode long-click


    【解决方案1】:

    我认为您可以查看 Android SDK 示例中的 Android 演示。 您可以在 Android SDK 示例的 ApiDemos 项目中的 List15.java 中找到答案。示例是:`

    /*
     * Copyright (C) 2010 The Android Open Source Project
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *      http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
     package com.example.android.apis.view;
    
     import com.example.android.apis.R;
    
     import android.app.ListActivity;
     import android.os.Bundle;
     import android.view.ActionMode;
     import android.view.Menu;
     import android.view.MenuInflater;
     import android.view.MenuItem;
     import android.widget.ArrayAdapter;
     import android.widget.ListView;
     import android.widget.Toast;
    
     /**
      * This demo illustrates the use of CHOICE_MODE_MULTIPLE_MODAL, a.k.a.                selection mode on ListView.
      */
     public class List15 extends ListActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ListView lv = getListView();
        lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
        lv.setMultiChoiceModeListener(new ModeCallback());
        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_checked, mStrings));
    }
    
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        getActionBar().setSubtitle("Long press to start selection");
    }
    
    private class ModeCallback implements ListView.MultiChoiceModeListener {
    
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.list_select_menu, menu);
            mode.setTitle("Select Items");
            setSubtitle(mode);
            return true;
        }
    
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return true;
        }
    
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch (item.getItemId()) {
            case R.id.share:
                Toast.makeText(List15.this, "Shared " + getListView().getCheckedItemCount() +
                        " items", Toast.LENGTH_SHORT).show();
                mode.finish();
                break;
            default:
                Toast.makeText(List15.this, "Clicked " + item.getTitle(),
                        Toast.LENGTH_SHORT).show();
                break;
            }
            return true;
        }
    
        public void onDestroyActionMode(ActionMode mode) {
        }
    
        public void onItemCheckedStateChanged(ActionMode mode,
                int position, long id, boolean checked) {
            setSubtitle(mode);
        }
    
        private void setSubtitle(ActionMode mode) {
            final int checkedCount = getListView().getCheckedItemCount();
            switch (checkedCount) {
                case 0:
                    mode.setSubtitle(null);
                    break;
                case 1:
                    mode.setSubtitle("One item selected");
                    break;
                default:
                    mode.setSubtitle("" + checkedCount + " items selected");
                    break;
            }
        }
    }
    
        private String[] mStrings = Cheeses.sCheeseStrings;
    }
    

    `

    你可以用你的代码试试这个

    `

        public class MyActivity extends Activity  {
    
    private ListView listView;
    
    ActionMode.Callback callback;
    
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
    
         listView = (ListView) findViewById(R.id.ListView);
         listView .setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
         listView .setMultiChoiceModeListener(callback);
    
         final ActionMode.Callback callback = new ActionMode.Callback() {
             /** Invoked whenever the action mode is shown. This is invoked immediately after onCreateActionMode */
     @Override
     public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
         return true;
     }
    
    
     /** Called when user exits action mode */
     @Override
     public void onDestroyActionMode(ActionMode mode) {
    
     }
    
     /** This is called when the action mode is created. This is called by startActionMode() */
     @Override
     public boolean onCreateActionMode(ActionMode mode, Menu menu) {
         mode.setTitle("Demo");
         getMenuInflater().inflate(R.menu.edit_vow, menu);
         return true;
     }
    
     /** This is called when an item in the context menu is selected */
     @Override
     public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
         switch(item.getItemId()){
             case R.id.editBTN:
                 Toast.makeText(getBaseContext(), "Selected Action1 ", Toast.LENGTH_LONG).show();
                 mode.finish();  // Automatically exists the action mode, when the user selects this action
                 break;
             case R.id.deleteBTN:
                 Toast.makeText(getBaseContext(), "Selected Action2 ", Toast.LENGTH_LONG).show();
                 break;
             case R.id.doneBTN:
                 Toast.makeText(getBaseContext(), "Selected Action3 ", Toast.LENGTH_LONG).show();
                         break;
                 }
                 return true;
             }
         };
    }`
    

    【讨论】:

    • @windleafs.you 的意思是我必须更改我所有的 ListView 代码?还是只使用 ModeCallback.java 类?
    • @windleafs.i 这样做。但是当我点击列表视图的项目时,什么也没有发生(操作模式不显示),当我长按它们时,“应用程序已停止”显示.我该怎么办?!请帮帮我。
    • 你能给我你的项目吗?我的 gmail 是 windleafs@gmail.com
    • 打扰一下,你收到我的邮件,主题是“mina dahesh.AAction mode android”吗?
    猜你喜欢
    • 1970-01-01
    • 2014-10-18
    • 1970-01-01
    • 1970-01-01
    • 2014-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多