本文主要是介绍为RecyclerView添加ContextMenu,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
这里要实现的是RecyclerView的item的长按事件与Menu关联
先看看要写的效果:
1、在res下新建menu目录,在menu目录下新建recyclerview_menu.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"><item
android:id="@+id/item_add"android:title="添加"android:orderInCategory="1"app:showAsAction="never"/><item
android:id="@+id/item_modify"android:title="修改"android:orderInCategory="2"app:showAsAction="never"/><item
android:id="@+id/item_delete"android:title="删除"android:orderInCategory="3"app:showAsAction="never"/><item
android:id="@+id/item_more"android:title="更多"android:orderInCategory="4"app:showAsAction="never"/>
</menu>
2、关键代码:
public class DrawerLayoutActivity extends AppCompatActivity {private Bomb[] mBombArray = {new Bomb("bomb5", R.drawable.bomb5), new Bomb("bomb6", R.drawable.bomb6), new Bomb("bomb7", R.drawable.bomb7),new Bomb("bomb8", R.drawable.bomb8), new Bomb("bomb9", R.drawable.bomb9), new Bomb("bomb10", R.drawable.bomb10),new Bomb("bomb11", R.drawable.bomb11), new Bomb("bomb12", R.drawable.bomb12), new Bomb("bomb13", R.drawable.bomb13),new Bomb("bomb14", R.drawable.bomb14), new Bomb("bomb15", R.drawable.bomb15), new Bomb("bomb16", R.drawable.bomb16)};private List<Bomb> mBombList = new ArrayList<>();private CommonAdapter<Bomb> mAdapter;private RecyclerView mRecyclerView;private int mSelectPosition;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_drawer_layout);initRecyclerView();}private void initRecyclerView() {mAdapter = new CommonAdapter<Bomb>(this,R.layout.item,mBombList) {@Overrideprotected void convert(ViewHolder holder, Bomb bomb, final int position) {holder.setText(R.id.tv,bomb.name);//holder.setImageResource(R.id.iv,bomb.imgId);ImageView iv = (ImageView) holder.getConvertView().findViewById(R.id.iv);Glide.with(DrawerLayoutActivity.this).load(bomb.imgId).into(iv);//长按事件holder.getConvertView().setOnLongClickListener(new View.OnLongClickListener() {@Overridepublic boolean onLongClick(View v) {mSelectPosition = position;return false;}});}};mRecyclerView = (RecyclerView) findViewById(R.id.revyvlerview);mRecyclerView.setLayoutManager(new GridLayoutManager(this,2));mRecyclerView.setAdapter(mAdapter);//为RecyclerView注册ContextMenuregisterForContextMenu(mRecyclerView);}/*** 以下与 RecyclerView 的 menu 相关*/@Overridepublic void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {super.onCreateContextMenu(menu, v, menuInfo);MenuInflater inflater = getMenuInflater();inflater.inflate(R.menu.recyclerview_menu,menu);}@Overridepublic boolean onContextItemSelected(MenuItem item) {switch (item.getItemId()){case R.id.item_add:Toast.makeText(this, "add", Toast.LENGTH_SHORT).show();return true;case R.id.item_modify:Toast.makeText(this, "modify", Toast.LENGTH_SHORT).show();return true;case R.id.item_delete:Toast.makeText(this, "delete", Toast.LENGTH_SHORT).show();mBombList.remove(mSelectPosition);mAdapter.notifyDataSetChanged();return true;case R.id.item_more:Toast.makeText(this, "more", Toast.LENGTH_SHORT).show();return true;default:return super.onContextItemSelected(item);}}
}
这篇关于为RecyclerView添加ContextMenu的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!