最近要實(shí)現(xiàn)如下的長(zhǎng)按刪除披诗,首先想到的是用PopupWindow甲献,可是又想起上下文菜單宰缤,覺(jué)得可以嘗試下,使用的時(shí)候也是各種搜索晃洒,覺(jué)得有必要記錄下慨灭。
設(shè)置長(zhǎng)按彈出菜單事件
//好像不注冊(cè) ContextMenu ,也可以直接長(zhǎng)按出來(lái)
//registerForContextMenu(recyclerView);
//設(shè)置菜單彈出事件
recyclerView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
menu.add(0, ContextMenu.FIRST+1, 0, "刪除");
}
});
設(shè)置菜單的點(diǎn)擊事件球及,重寫(xiě)onContextItemSelected
@Override
public boolean onContextItemSelected(MenuItem item) {
if (item.getItemId() == ContextMenu.FIRST + 1) {
AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
//刪除
Log.i(TAG, "onContextItemSelected: position="+menuInfo.position);
cancelFavorite(menuInfo.position);
}
return super.onContextItemSelected(item);
}
在這里缘挑,應(yīng)該也會(huì)迷惑,怎么獲取item的 position 位置尼桶略?怎么知道我點(diǎn)擊的是哪一個(gè),我要根據(jù)這個(gè)去獲取對(duì)應(yīng)的數(shù)據(jù)。而方法中的AdapterView.AdapterContextMenuInfo 又是什么鬼际歼?
一惶翻、結(jié)構(gòu)
public static class AdapterContextMenuInfo extends Object
implements ContextMenu.ContextMenuInfo
java.lang.Object
android.widget.AdapterView.AdapterContextMenuInfo
二、概述
當(dāng)顯示 AdapterView 的上下文菜單時(shí)鹅心,為 onCreateContextMenu(ContextMenu, View, ContextMenuInfo) 回調(diào)函數(shù)提供的額外的菜單信息吕粗。
三、字段
public long id
用于顯示上下文菜單的子視圖的行 ID旭愧。
public int position
用于顯示上下文菜單的子視圖在適配器中的位置颅筋。
public View targetView
用于顯示上下文菜單的子視圖。也是 AdapterView 的子視圖之一输枯。
四议泵、構(gòu)造函數(shù)
public AdapterView.AdapterContextMenuInfo (View targetView, int position, long id)
在Listview中是默認(rèn)提供了這個(gè)的,但是RecycleView沒(méi)有實(shí)現(xiàn)這個(gè)桃熄,需要我們自己實(shí)現(xiàn)先口。
public class RecyclerViewImplementsContextMenu extends RecyclerView {
private AdapterView.AdapterContextMenuInfo contextMenuInfo;
public RecyclerViewImplementsContextMenu(Context context) {
super(context);
}
public RecyclerViewImplementsContextMenu(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public RecyclerViewImplementsContextMenu(Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public AdapterView.AdapterContextMenuInfo getContextMenuInfo() {
return contextMenuInfo;
}
@Override
public boolean showContextMenuForChild(View originalView) {
int position = getChildAdapterPosition(originalView);
long longId = getChildItemId(originalView);
contextMenuInfo = new AdapterView.AdapterContextMenuInfo(originalView, position, longId);
return super.showContextMenuForChild(originalView);
}
}
重寫(xiě)getContextMenuInfo 和 showContextMenuForChild。
這里沿用了Listview中的數(shù)據(jù)結(jié)構(gòu)瞳收,當(dāng)然碉京,也可以自定義ContextMenuInfo的數(shù)據(jù)內(nèi)容,類(lèi)似AdapterView.AdapterContextMenuInfo 一樣實(shí)現(xiàn)ContextMenu.ContextMenuInfo接口
public static class AdapterContextMenuInfo implements ContextMenu.ContextMenuInfo {
public AdapterContextMenuInfo(View targetView, int position, long id) {
this.targetView = targetView;
this.position = position;
this.id = id;
}
/**
* The child view for which the context menu is being displayed. This
* will be one of the children of this AdapterView.
*/
public View targetView;
/**
* The position in the adapter for which the context menu is being
* displayed.
*/
public int position;
/**
* The row id of the item for which the context menu is being displayed.
*/
public long id;
}
也可以參考如下兩篇文章的實(shí)現(xiàn)
http://stackoverflow.com/questions/26466877/how-to-create-context-menu-for-recyclerview