Contextual Action Bar with RecyclerView in xamarin android?

前端 未结 1 1225
梦毁少年i
梦毁少年i 2021-01-27 23:45

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

1条回答
  •  攒了一身酷
    2021-01-28 00:36

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

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

    1. Create a simple menu resource for your action bar(Resource/menu/ContextualMenu):

      
      
        
        
      
      
    2. Create a class(MyActionMode) implements 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;
          }
      }
      
    3. In PhotoViewHolder modify the constructor to accept a long click action:

      public PhotoViewHolder (View itemView, Action listener,Action longClickListener) 
          : base (itemView)
      {
          // Locate and cache view references:
          Image = itemView.FindViewById (Resource.Id.imageView);
          Caption = itemView.FindViewById (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);
      }
      
    4. Modify PhotoAlbumAdapter like this:

      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;
          }
         ...
      }
      
    5. In MainActivity change the creation of PhotoAlbumAdapter to use the newly created constructor:

      mAdapter = new PhotoAlbumAdapter (mPhotoAlbum,this);
      

    Here is the complete modified demo.

    0 讨论(0)
提交回复
热议问题