【问题标题】:Action mode does not start for ListViewListView 的操作模式未启动
【发布时间】:2014-07-26 08:38:40
【问题描述】:

当我对 ListView 项目执行长按时,操作模式不会启动。 ListView 的实现通过一个项目都是一样的:

ListView listView = (ListView) view.findViewById(android.R.id.list);         

listView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL);              

listView.setMultiChoiceModeListener(new NutritionMultiChoiceModeListener() { 

    @Override                                                                
    public void deleteSelectedItems() {                                      
        for (int i = 0; i < componentAdapter.getCount(); i++) {              
            if (getListView().isItemChecked(i)) {                            
                //Some actions                          
            }                                                                
        }                                                                    
    }                                                                        
});                                                                          

NutritionMultiChoiceModeListener 是自定义的 MultiChoiceModeListener,包含抽象方法 deleteSelectedItems(),调用删除。

这是 ListView 项目的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/backround_activated"
android:orientation="vertical" >

<Button
    android:id="@+id/component_button"
    style="?android:attr/buttonBarButtonStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<EditText
    android:id="@+id/component_grams_edit_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/ingredient_grams"
    android:inputType="number" />

</LinearLayout>

当我执行长按时,我要么按下按钮,要么开始编辑文本。

项目中只有一个这样的地方。对于仅包含 TextViews 的其他项目布局,一切正常。

请帮忙。

【问题讨论】:

    标签: android listview android-listview android-actionmode actionmode


    【解决方案1】:

    我没有看到您在代码 sn-p 中的哪个位置长按启动动作模式。

    Here is a sample project 演示了通过长按启动动作模式。在那里,列表以正常模式开始,但是在长按时我切换到多选模式模式并检查长按项目:

      @Override
      public boolean onItemLongClick(AdapterView<?> parent, View view,
                                     int position, long id) {
        getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
        getListView().setItemChecked(position, true);
    
        return(true);
      }
    

    【讨论】:

    • 谢谢,这是工作。我仍然需要弄清楚,如何使其他项目可选择,即使在动作模式下,现在按钮和 edittext 拦截点击也是如此。我相信,我自己可以做到。但如果你不介意的话,我还有一个简短的问题。项目中的另一个 ListView 仅包含带有 TextViews 的项目。而且我不需要为该 ListView 设置 OnItemLongClickListener - 一切正常。那是因为 TextViews 不是“可聚焦的”什么的?
    • @Shakenbeer:在ListView 行中肯定有可聚焦小部件会导致行为发生变化,这就是为什么我尽量避免在TextView 行中使用可聚焦小部件。 :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-30
    • 1970-01-01
    • 2018-08-23
    • 2014-11-13
    • 1970-01-01
    相关资源
    最近更新 更多