问题
I have followed below link on how to implement Contextual action bar with Recycler View https://stackoverflow.com/a/45911314/6117355...Can any one tell me how to perform some action like (edit/delete) operations on rows of Recyclerview on based of button actions of Contextual action bar in xamarin Android?
回答1:
Can any one tell me how to perform some action like (edit/delete) operations on rows of Recyclerview on based of button actions of Contextual action bar in xamarin Android?
Firstly, you need to modify PhotoAlbum.cs's Photo[] to a List<Photo> to let it support delete/update operation:
public class PhotoAlbum
{
// Built-in photo collection - this could be replaced with
// a photo database:
...
// Array of photos that make up the album:
private List<Photo> mPhotos;
// Random number generator for shuffling the photos:
Random mRandom;
// Create an instance copy of the built-in photo list and
// create the random number generator:
public PhotoAlbum ()
{
mPhotos =new List<Photo>( mBuiltInPhotos);
mRandom = new Random();
}
// Return the number of photos in the photo album:
public int NumPhotos
{
get { return mPhotos.Count; }
}
// Indexer (read only) for accessing a photo:
public Photo this[int i]
{
get { return mPhotos[i]; }
}
// Add this Method to support update operation
public void UpdateAt(int position, Photo photo)
{
if (photo != null)
mPhotos[position] = photo;
}
public Photo GetAt(int position)
{
return mPhotos[position];
}
// Add this Method to support delete operation
public void RemoveAt(int position)
{
mPhotos.RemoveAt(position);
}
// Pick a random photo and swap it with the top:
public int RandomSwap()
{
// Save the photo at the top:
Photo tmpPhoto = mPhotos[0];
// Generate a next random index between 1 and
// Length (noninclusive):
int rnd = mRandom.Next(1, mPhotos.Count);
// Exchange top photo with randomly-chosen photo:
mPhotos[0] = mPhotos[rnd];
mPhotos[rnd] = tmpPhoto;
// Return the index of which photo was swapped with the top:
return rnd;
}
// Shuffle the order of the photos:
public void Shuffle ()
{
// Use the Fisher-Yates shuffle algorithm:
for (int idx = 0; idx < mPhotos.Count; ++idx)
{
// Save the photo at idx:
Photo tmpPhoto = mPhotos[idx];
// Generate a next random index between idx (inclusive) and
// Length (noninclusive):
int rnd = mRandom.Next(idx, mPhotos.Count);
// Exchange photo at idx with randomly-chosen (later) photo:
mPhotos[idx] = mPhotos[rnd];
mPhotos[rnd] = tmpPhoto;
}
}
}
Then, modify the longClick Event to accept an position param:
public class PhotoViewHolder : RecyclerView.ViewHolder
{
public ImageView Image { get; private set; }
public TextView Caption { get; private set; }
// Get references to the views defined in the CardView layout.
public PhotoViewHolder (View itemView, Action<int> listener,Action<object,View.LongClickEventArgs,int> 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,base.Position);
}
}
Then start action mode with position param and inside OnActionItemClicked call the Remove function added to the Adapter:
void OnLongClick(object sender, View.LongClickEventArgs args,int position)
{
mActionMode = new MyActionMode(mActivity,this,position);
mode=mActivity.StartActionMode(mActionMode);
((View)sender).Selected = true;
return;
}
...
public class MyActionMode : Java.Lang.Object, ActionMode.ICallback
{
private Context mContext;
private PhotoAlbumAdapter mAdapter;
private int currentPosition;
public MyActionMode(Context context):this(context,null,0)
{
}
public MyActionMode(Context context, PhotoAlbumAdapter adapter,int position)
{
mContext = context;
mAdapter = adapter;
currentPosition = position;
}
public bool OnActionItemClicked(ActionMode mode, IMenuItem item)
{
switch (item.ItemId)
{
case Resource.Id.itemOneId:
// do Delete
mAdapter.RemoveAt(currentPosition);
mAdapter.FinishActionMode();
return true;
case Resource.Id.itemTwoId:
// do Update
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;
}
}
I've committed the codes to demo project.
Notes: It is the same logic that can apply to the update operation.
来源:https://stackoverflow.com/questions/45929315/how-to-delete-row-in-recyclerlistview-on-click-of-menu-button-in-contextual-acti