Reputation: 117
Hello everyone my name is Taniguchi
i've created a recyclerview and inserted a swipe on the recyclerview items.
the swipe callback is working i can swipe on recyclerview items, but the code i've made to delete is not working.
When i swipe the line stays red and the item stays on recyclerview.
My notifydatachanged is not rebilding the recyclerview.
my swipe callback
public class SwipeToDeleteCallback : ItemTouchHelper.SimpleCallback
{
private View v;
private List<Email> mEmails;
private Context context;
private RecyclerView.Adapter mdapter;
public static RecyclerView.Adapter mAdapter;
private Android.Graphics.Drawables.Drawable deleteIcon;
private int intrinsicWidth;
private int intrinsicHeight;
private Android.Graphics.Drawables.ColorDrawable background;
private Color backgroundColor;
private Paint clearPaint;
public SwipeToDeleteCallback(int dragDirs, int swipeDirs, Context context) : base(dragDirs, swipeDirs)
{
this.context = context;
deleteIcon = ContextCompat.GetDrawable(context, Resource.Drawable.alter_delete);
intrinsicWidth = deleteIcon.IntrinsicWidth;
intrinsicHeight = deleteIcon.IntrinsicHeight;
background = new Android.Graphics.Drawables.ColorDrawable();
backgroundColor = Color.ParseColor("#f44336");
clearPaint = new Paint();
clearPaint.SetXfermode(new PorterDuffXfermode(PorterDuff.Mode.Clear));
}
public SwipeToDeleteCallback(int dragDirs, int swipeDirs, Context context, RecyclerView.Adapter mRecyclerView) : this(dragDirs, swipeDirs, context)
{
this.context = context;
this.mdapter = mRecyclerView;
deleteIcon = ContextCompat.GetDrawable(context, Resource.Drawable.alter_delete);
intrinsicWidth = deleteIcon.IntrinsicWidth;
intrinsicHeight = deleteIcon.IntrinsicHeight;
background = new Android.Graphics.Drawables.ColorDrawable();
backgroundColor = Color.ParseColor("#f44336");
clearPaint = new Paint();
clearPaint.SetXfermode(new PorterDuffXfermode(PorterDuff.Mode.Clear));
}
public override int GetMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder)
{
if (viewHolder.AdapterPosition == 10)
{
return 0;
}
return base.GetMovementFlags(recyclerView, viewHolder);
}
public override void OnChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, bool isCurrentlyActive)
{
base.OnChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
public override bool OnMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target)
{
//throw new NotImplementedException();
return false;
}
public override void OnChildDrawOver(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, bool isCurrentlyActive)
{
var itemView = viewHolder.ItemView;
var itemHeight = itemView.Bottom - itemView.Top;
var isCanceled = dX == 0f && !isCurrentlyActive;
if (isCanceled)
{
clearCanvas(c, itemView.Right + dX, (float)itemView.Top, (float)itemView.Right, (float)itemView.Bottom);
base.OnChildDrawOver(c, recyclerView
, viewHolder, dX, dY, actionState, isCurrentlyActive);
return;
}
background.Color = backgroundColor;
background.SetBounds(itemView.Right + (int)dX, itemView.Top, itemView.Right, itemView.Bottom);
background.Draw(c);
var deleteIconTop = itemView.Top + (itemHeight - intrinsicHeight) / 2;
var deleteIconMargin = (itemHeight - intrinsicHeight) / 2;
var deleteIconLeft = itemView.Right - deleteIconMargin - intrinsicWidth;
var deleteIconRight = itemView.Right - deleteIconMargin;
var deleteIconBottom = deleteIconTop + intrinsicHeight;
deleteIcon.SetBounds(deleteIconLeft, deleteIconTop, deleteIconRight, deleteIconBottom);
deleteIcon.Draw(c);
base.OnChildDrawOver(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
private void clearCanvas(Canvas c, float v, float top, float right, float bottom)
{
c.DrawRect(v, top, right, bottom, clearPaint);
}
public override void OnSwiped(RecyclerView.ViewHolder viewHolder, int direction)
{
//Invoke Removing Item method from
int position = viewHolder.AdapterPosition;
RecyclerAdapter mAdapter = new RecyclerAdapter(mEmails, this);
mAdapter.RemoveItem(viewHolder.AdapterPosition);
}
public override void ClearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder)
{
base.ClearView(recyclerView, viewHolder);
}
}
my recyclerview adapter:
public class RecyclerAdapter : RecyclerView.Adapter
{
public static RecyclerView.Adapter mAdapter;
public static bool isActionMode = true;
public static bool unselect = false;
private Activity mActivity;
private List<Email> mEmails;
private Context context;
private View p;
public static bool count = false;
public static int CountAuxiliar = 0;
private SwipeToDeleteCallback swipeToDeleteCallback;
public RecyclerAdapter(List<Email> emails, Context context)
{
mEmails = emails;
this.context = context;
}
public RecyclerAdapter(List<Email> emails, Activity activity)
{
mEmails = emails;
mActivity = activity;
}
public RecyclerAdapter(List<Email> mEmails, SwipeToDeleteCallback swipeToDeleteCallback)
{
this.mEmails = mEmails;
this.swipeToDeleteCallback = swipeToDeleteCallback;
}
public class MyView : RecyclerView.ViewHolder
{
public View mMainView { get; set; }
public TextView mName { get; set; }
public TextView mSubject { get; set; }
public TextView mMessage { get; set; }
public MyView(View view) : base(view)
{
mMainView = view;
}
}
public override int ItemCount
{
get { return mEmails.Count; }
}
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
LayoutInflater inflater = LayoutInflater.From(parent.Context);
View row = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.row, parent, false);
RecyclerViewHolder vh = new RecyclerViewHolder(row);
return vh;
}
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
RecyclerViewHolder myHolder = holder as RecyclerViewHolder;
myHolder.mName.Text = mEmails[position].Name;
myHolder.mSubject.Text = mEmails[position].Subject;
myHolder.mMessage.Text = mEmails[position].Message;
}
public void RemoveItem(int position)
{
ArrayList al = new ArrayList(MainActivity.mEmails);
al.RemoveAt(position);
NotifyDataSetChanged();
NotifyItemChanged(position);
}
}
where i calling the recylerview and the swipe callback
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
public static RecyclerView mRecyclerView;
public static RecyclerView.LayoutManager mLayoutManager;
public RecyclerView.ViewHolder holder;
public static BottomNavigationView bottomnavigationview1;
public static RecyclerView.Adapter mAdapter;
public static List<Email> mEmails;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
Xamarin.Essentials.Platform.Init(this, bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
mRecyclerView = FindViewById<RecyclerView>(Resource.Id.recyclerViwer);
mRecyclerView.AddItemDecoration(new DividerItemDecoration(mRecyclerView.Context, DividerItemDecoration.Vertical));
mRecyclerView.HasFixedSize = true;
SetupList();
//Create our layout Manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.SetLayoutManager(mLayoutManager);
RecyclerAdapter mAdapter = new RecyclerAdapter(mEmails, this);
mRecyclerView.SetAdapter(mAdapter);
var swipeHandler = new SwipeToDeleteCallback(0, ItemTouchHelper.Left, this, mAdapter);
var itemTouchHelper = new ItemTouchHelper(swipeHandler);
itemTouchHelper.AttachToRecyclerView(mRecyclerView);
}
private void SetupList()
{
for (int i = 1; i <= 2; i++)
{
mEmails = new List<Email>();
mEmails.Add(new Email() { Name = "tom", Subject = "Wanna Hang Out?", Message = "I ' ll be around tomorrow!!" });
mEmails.Add(new Email() { Name = "tom", Subject = "Wanna Hang Out?", Message = "I ' ll be around tomorrow!!" });
mEmails.Add(new Email() { Name = "tom", Subject = "Wanna Hang Out?", Message = "I ' ll be around tomorrow!!" });
mEmails.Add(new Email() { Name = "tom", Subject = "Wanna Hang Out?", Message = "I ' ll be around tomorrow!!" });
mEmails.Add(new Email() { Name = "tom", Subject = "Wanna Hang Out?", Message = "I ' ll be around tomorrow!!" });
mEmails.Add(new Email() { Name = "tom", Subject = "Wanna Hang Out?", Message = "I ' ll be around tomorrow!!" });
mEmails.Add(new Email() { Name = "tom", Subject = "Wanna Hang Out?", Message = "I ' ll be around tomorrow!!" });
mEmails.Add(new Email() { Name = "tom", Subject = "Wanna Hang Out?", Message = "I ' ll be around tomorrow!!" });
mEmails.Add(new Email() { Name = "tom", Subject = "Wanna Hang Out?", Message = "I ' ll be around tomorrow!!" });
mEmails.Add(new Email() { Name = "tom", Subject = "Wanna Hang Out?", Message = "I ' ll be around tomorrow!!" });
mEmails.Add(new Email() { Name = "tom", Subject = "Wanna Hang Out?", Message = "I ' ll be around tomorrow!!" });
mEmails.Add(new Email() { Name = "tom", Subject = "Wanna Hang Out?", Message = "I ' ll be around tomorrow!!" });
mEmails.Add(new Email() { Name = "tom", Subject = "Wanna Hang Out?", Message = "I ' ll be around tomorrow!!" });
mEmails.Add(new Email() { Name = "tom", Subject = "Wanna Hang Out?", Message = "I ' ll be around tomorrow!!" });
}
}
Upvotes: 1
Views: 1008
Reputation: 18861
In the method RemoveItem
public void RemoveItem(int position)
{
ArrayList al = new ArrayList(MainActivity.mEmails);
al.RemoveAt(position);
NotifyDataSetChanged();
NotifyItemChanged(position);
}
You create a new array al
.But you should remove the item from the ItemsSource of Recyclerview .
public void RemoveItem(int position)
{
mEmails.RemoveAt(position);
NotifyDataSetChanged();
NotifyItemChanged(position);
}
Improve your code as follow
private RecyclerAdapter mdapter;
public SwipeToDeleteCallback(int dragDirs, int swipeDirs, Context context, RecyclerAdapter mRecyclerView , List<Email> mails) : this(dragDirs, swipeDirs, context)
{
this.context = context;
this.mdapter = mRecyclerView;
deleteIcon = ContextCompat.GetDrawable(context, Resource.Drawable.alter_delete);
intrinsicWidth = deleteIcon.IntrinsicWidth;
intrinsicHeight = deleteIcon.IntrinsicHeight;
background = new Android.Graphics.Drawables.ColorDrawable();
backgroundColor = Color.ParseColor("#f44336");
clearPaint = new Paint();
mEmails = mails;
clearPaint.SetXfermode(new PorterDuffXfermode(PorterDuff.Mode.Clear));
}
public override void OnSwiped(RecyclerView.ViewHolder viewHolder, int direction)
{
//Invoke Removing Item method from
mdapter.RemoveItem(viewHolder.AdapterPosition);
}
public void RemoveItem(int position)
{
mEmails.RemoveAt(position);
NotifyDataSetChanged();
NotifyItemChanged(position);
}
RecyclerAdapter mAdapter = new RecyclerAdapter(mEmails, this);
mRecyclerView.SetAdapter(mAdapter);
var swipeHandler = new SwipeToDeleteCallback(0, ItemTouchHelper.Left, this, mAdapter,mEmails);
var itemTouchHelper = new ItemTouchHelper(swipeHandler);
itemTouchHelper.AttachToRecyclerView(mRecyclerView);
Upvotes: 1