本文介绍了xamarin android 中带有 RecyclerView 的上下文操作栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉如何在 xamarin android 上长按 listItem Selection 时实现 RecyclerView Contextual Action Bar?

Can any one tell how to implement RecyclerView Contextual Action Bar on long tap of listItem Selection on xamarin android?

推荐答案

Xamarin 官方 RecyclerViewer 为例,你可以关注以下步骤在 recyclerview 上实现上下文操作栏:

Take Xamarin Official RecyclerViewer for example, you can follow the below steps to implement a contextual action bar on the recyclerview:

  1. 为您的操作栏创建一个简单的菜单资源(Resource/menu/ContextualMenu):

<?xml version="1.0" encoding="utf-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:id="@+id/itemOneId"
    android:title="Item One"/>
  <item android:id="@+id/itemTwoId"
    android:title="Item Two" />
</menu>

  • 创建一个类(MyActionMode)实现ActionMode.ICallBack:

    public class MyActionMode : Java.Lang.Object, ActionMode.ICallback
    {
        private Context mContext;
        public MyActionMode(Context context)
        {
            mContext = context;
        }
    
        public bool OnActionItemClicked(ActionMode mode, IMenuItem item)
        {
            switch (item.ItemId)
            {
                case Resource.Id.itemOneId:
                    // do whatever you want
                    return true;
                case Resource.Id.itemTwoId:
                    // do whatever you want
                    return true;
                default:
                    return false;
            }
        }
    
        public bool OnCreateActionMode(ActionMode mode, IMenu menu)
        {
            mode.MenuInflater.Inflate(Resource.Menu.ContextualMenu, menu);
            return true;
        }
    
        public void OnDestroyActionMode(ActionMode mode)
        {
            mode.Dispose();
        }
    
        public bool OnPrepareActionMode(ActionMode mode, IMenu menu)
        {
            return false;
        }
    }
    

  • PhotoViewHolder中修改构造函数以接受长按操作:

  • In PhotoViewHolder modify the constructor to accept a long click action:

    public PhotoViewHolder (View itemView, Action<int> listener,Action<object,View.LongClickEventArgs> longClickListener)
        : base (itemView)
    {
        // Locate and cache view references:
        Image = itemView.FindViewById<ImageView> (Resource.Id.imageView);
        Caption = itemView.FindViewById<TextView> (Resource.Id.textView);
    
        // Detect user clicks on the item view and report which item
        // was clicked (by position) to the listener:
        itemView.Click += (sender, e) => listener (base.Position);
        ItemView.LongClick +=(sender,e)=> longClickListener(sender,e);
    }
    

  • 像这样修改PhotoAlbumAdapter:

    public class PhotoAlbumAdapter : RecyclerView.Adapter
    {
        ...
        // add this variable
        private Activity mActivity;
    
        //
        private MyActionMode mActionMode;
    
        ...
        // add this constructor
        public PhotoAlbumAdapter(PhotoAlbum photoAlbum, Activity activity)
        {
            mPhotoAlbum = photoAlbum;
            mActivity = activity;
        }
    
    
        //add this function
        void OnLongClick(object sender, View.LongClickEventArgs args)
        {
            mActionMode = new MyActionMode(mActivity);
            mActivity.StartActionMode(mActionMode);
            ((View)sender).Selected = true;
            return;
        }
       ...
    }
    

  • MainActivity 中更改 PhotoAlbumAdapter 的创建以使用新创建的构造函数:

  • In MainActivity change the creation of PhotoAlbumAdapter to use the newly created constructor:

    mAdapter = new PhotoAlbumAdapter (mPhotoAlbum,this);
    

  • 这是完整的修改演示.

    这篇关于xamarin android 中带有 RecyclerView 的上下文操作栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    08-29 04:21