0

当我对 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>

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

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

请帮忙。

4

1 回答 1

1

我看不到您在代码片段中的哪个位置长按启动动作模式。

这是一个示例项目,演示了如何通过长按来启动动作模式。在那里,列表以正常模式开始,但是在长按时我切换到多选模式模式并检查长按项目:

  @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);
  }
于 2014-07-26T00:38:40.590 回答