2016-10-01 3 views
0

ExpandableListView私は選択(ショートクリック)と削除(ロングクリック)を実装しました。短いリストアイテムのクリックはonChildClick()で処理され、長いクリックはonCreateContextMenu()によって処理されます。Long-Clickは、リストビューのコンテキストメニューとAndroid 7デバイスの選択の両方をトリガーします。

@Override 
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { 
    super.onCreateContextMenu(menu, v, menuInfo); 

    ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo; 
    mDeleteItemGroup = ExpandableListView.getPackedPositionGroup(info.packedPosition); 
    mDeleteItemChild = ExpandableListView.getPackedPositionChild(info.packedPosition); 

    menu.setHeaderTitle("some title"); 
    MenuInflater inflater = mActivity.getMenuInflater(); 
    inflater.inflate(R.menu.menu_my_view_context, menu); 
} 

上記は、長いクリックをうまく処理したコンテキストメニューコードを示しています。問題は、スタイル性の欠如で、一部のデバイスで長いタイトルを切り詰めることでした。次のようにだから私は、カスタムダイアログボックスの代わりに、標準のコンテキストメニューを使用:

@Override 
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { 
    super.onCreateContextMenu(menu, v, menuInfo); 

    ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo; 
    mDeleteItemGroup = ExpandableListView.getPackedPositionGroup(info.packedPosition); 
    mDeleteItemChild = ExpandableListView.getPackedPositionChild(info.packedPosition); 

    String title = "some title"; 

    ConfirmDeletePopupFragment confirmDeletePopupFragment = ConfirmDeletePopupFragment.newInstance(title); 
    confirmDeletePopupFragment.setTargetFragment(this, 0); 
    confirmDeletePopupFragment.show(getActivity().getSupportFragmentManager(), "tag"); 
} 

これは、ここでAndroidの7を実行してネクサス5Xを除くすべてのデバイス上でうまく動作しますが、長いクリックでコンテキストメニューと選択の両方をトリガーonChildClickを介して、これは明らかに私が欲しいものではありません。

カスタムダイアログを使用してアイテムを選択できないようにするにはどうしたらいいですか?

+0

もちろん、コンテクストメニューがイベントを処理している間に選択をミュートするにはフラグを使用できますが、それは間違ったものにパッチを当てるようです。 – jerry

答えて

0

私の現在の解決策や回避策は、コンテキストメニューをカスタムダイアログボックスに置き換えて壊したものを修正するので、最適ではないと感じています。アイデアは、削除処理が開始されたときに選択処理をミュートし、ダイアログのコールバックでミュートを解除することです。

これは機能しますが、私は最初にそれを中断したくないです。だから、おそらくもっと良い方法があります。

public class MyListFragment extends ExpandableListFragment implements ConfirmDeletePopupFragment.DialogListener { 

    (...) 

    private boolean mMuteSelection = false; 

    (...) 

    @Override 
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { 
     super.onCreateContextMenu(menu, v, menuInfo); 

     mMuteSelection = true; 

     ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo; 
     mDeleteItemGroup = ExpandableListView.getPackedPositionGroup(info.packedPosition); 
     mDeleteItemChild = ExpandableListView.getPackedPositionChild(info.packedPosition); 

     String title = "some title" 

     ConfirmDeletePopupFragment confirmDeleteWeakPopupFragment = ConfirmDeletePopupFragment.newInstance(title); 
     confirmDeletePopupFragment.setTargetFragment(this, 0); 
     confirmDeletePopupFragment.show(getActivity().getSupportFragmentManager(), "tag"); 
    } 

    (...) 

    @Override 
    public boolean onChildClick(ExpandableListView arg0, View arg1, int group, int child, long arg4) { 
     super.onChildClick(arg0, arg1, group, child, arg4); 

     if (!mMuteSelection) { 
      (handle selection) 
     } 
     return false; 
    } 

    (...) 

    @Override 
    public void onDeleteConfirm(boolean delete) { 
     if (delete) { 
      (handle deletion) 
     } 
     mMuteSelection = false; 
    } 
} 
関連する問題