RecyclerView是默認(rèn)不能想ListView一樣支持ContextMenu的释移,不過可以通過重寫以下兩個方法來實(shí)現(xiàn)ContextMenu:
protected ContextMenu.ContextMenuInfo getContextMenuInfo(){...}
@Override
public boolean showContextMenuForChild(View originalView) {
int pos = getChildAdapterPosition(originalView);
long itemId= getChildItemId(originalView);
contextMenuInfo = new AdapterView.AdapterContextMenuInfo(originalView,pos,itemId);
return super.showContextMenuForChild(originalView);
}
然后在Activity或者Fragment中重寫為RecyclerView設(shè)置:
listRv.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
//這里添加菜單內(nèi)容
menu.add(0,1,1,"xxxx");
}
});
//重寫
@Override
public boolean onContextItemSelected(MenuItem item) {
swithc(item.getItemId()){
// .... 業(yè)務(wù)邏輯
}
return super.onContextItemSelected(item);
}
具體重寫的代碼細(xì)節(jié)可以參考文章: 為RecycleView添加ContextMenu支持
以上基本流程就完成了正常情況長按RecyclerView中的條目就會彈出選項(xiàng)菜單了氯檐。
最容易忽略的細(xì)節(jié)就是為RecuclerView的item設(shè)置允許長按胖缤。
//代碼設(shè)置
rl.setLongClickable(true);
//xml中設(shè)置
android:longClickable="true"
而且設(shè)置長按必須而且僅能能在最外層布局設(shè)置允許長按
意外時正確設(shè)置(假設(shè)這是RecyclerView的item不的布局文件)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
<!-- 就是這一句話-->
android:longClickable="true"
android:orientation="vertical">
<!--其他view -->
<RelativeLayout
android:id="@+id/parent"
android:layout_width="match_parent"
android:layout_height="100dp" >
<!--其他view -->
</RelativeLayout>
</LinearLayout>
假如設(shè)置了多個 longClickable 就像下面這樣:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
<!-- 就是這一句話-->
android:longClickable="true"
android:orientation="vertical">
<!--其他view -->
<RelativeLayout
android:id="@+id/parent"
<!-- 就是這一句話-->
android:longClickable="true"
android:layout_width="match_parent"
android:layout_height="100dp" >
<!--其他view -->
</RelativeLayout>
</LinearLayout>
這時RecyclerView中的條目時就會發(fā)生如下類似的異常 李茫,主要是xxx.LayoutParams 不能轉(zhuǎn)換成RecyclerView$LayoutParams的ClassCastException
java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v7.widget.RecyclerView$LayoutParams
at android.support.v7.widget.RecyclerView.getChildViewHolderInt(RecyclerView.java:4314)
at android.support.v7.widget.RecyclerView.getChildAdapterPosition(RecyclerView.java:4333)
at dong.lan.test.ContextMenuRecyclerView.showContextMenuForChild(ContextMenuRecyclerView.java:41)
而最外層布局不設(shè)置長按允許筋讨,而在內(nèi)層View或者ViewGroup設(shè)置長按允許赊舶,也會發(fā)生如上類似的 ClassCastException
例如:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
<!-- 這里不設(shè)置了淤刃,這是步行的I顾! android:longClickable="true"-->
android:orientation="vertical">
<!--其他view -->
<RelativeLayout
android:id="@+id/parent"
<!-- 就是這一句話-->
android:longClickable="true"
android:layout_width="match_parent"
android:layout_height="100dp" >
<!--其他view -->
</RelativeLayout>
</LinearLayout>
最后逸贾,設(shè)置長按必須而且僅能能在最外層布局設(shè)置允許長按