Xamarin Android:如何在recyclerview中删除多个iten

问题描述 投票:0回答:1

大家好,我叫谷口

我实现了一个recyclerview,并插入了一个按钮导航视图以删除选定的项目。

[当我在recyclerview中单击某个项目时,我选择了一个项目。当我单击并取消选择该项目时,我插入了一个集合来存储项目位置。

例如,我能够在recyclerview上删除多个iten,但其中一项被错误删除。我选择两个itens位置0和位置1,然后单击以删除这两个位置,但是不是删除位置1,而是删除了项目位置3,并且继续选择了位置2的项目。

我如何正确地删除它?

我的recyclerview适配器:

public class RecyclerAdapter : RecyclerView.Adapter, View.IOnClickListener
{
    private List<Email> mEmails;
    private Activity mActivity;
    public static int listPosition;
    private List<collectPosition> mcollectID;
    private int position_collectID = 0;

    private Boolean isSelected = false;

    public Boolean IsSelected()
    {
        return isSelected;
    }

    public void setSelected(Boolean selected)
    {

        isSelected = selected;
    }

    public RecyclerAdapter(List<Email> emails, Activity activity)
    {
        mEmails = emails;
        mActivity = activity;
    }

    public override int ItemCount => mEmails.Count;

    public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
    {
        View row = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.row, parent, false);
        RecyclerViewHolder view = new RecyclerViewHolder(row);
        return view;
    }
    public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {
        RecyclerViewHolder myHolder = holder as RecyclerViewHolder;
        myHolder.mDate.Text = ((Email)mEmails[position]).date;
        myHolder.mSubject.Text = ((Email)mEmails[position]).Subject;
        myHolder.mMessage.Text = ((Email)mEmails[position]).Message;

        myHolder.ItemView.SetBackgroundColor(((Email)mEmails[position]).IsSelected() ? Color.LightBlue : Color.Transparent);
        myHolder.ItemView.Tag = position;
        myHolder.ItemView.SetOnClickListener(this);
    }

    void View.IOnClickListener.OnClick(View v)
    {
        int position = (int)v.Tag;
        listPosition = (int)v.Tag;

        ((Email)mEmails[position]).setSelected(!((Email)mEmails[position]).IsSelected());

        v.SetBackgroundColor(((Email)mEmails[position]).IsSelected() ? Color.LightBlue : Color.Transparent);

        if (mcollectID == null)
            mcollectID = new List<collectPosition>();

        if (((Email)mEmails[position]).IsSelected())
        {

            mcollectID.Add(new collectPosition() {  getPosition = (int)v.Tag });
            position_collectID = position_collectID + 1;
        }
        else
        {


            position_collectID = position_collectID - 1;

            mcollectID.RemoveAt(position_collectID);
        }



        Toast.MakeText(v.Context, "Click : " + ((Email)mEmails[position]).date + "---" + position, ToastLength.Short).Show();
    }


    public void DeleteItem()
    {
        if (mEmails != null && mEmails.Count > 0)
        {

            foreach (collectPosition collId in mcollectID)
            {
                mEmails.RemoveAt(collId.getPosition);
                NotifyItemChanged(collId.getPosition);
                NotifyDataSetChanged();
            }

        }
    }


}

我在哪里调用recyclerview:

 public class MainActivity : AppCompatActivity
{
    public static RecyclerView mRecyclerView;
    public static RecyclerView.LayoutManager mLayoutManager;
    public RecyclerView.ViewHolder holder;
    public static BottomNavigationView bottomnavigationview1;
    public RecyclerAdapter 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);
        mAdapter = new RecyclerAdapter(mEmails, this);
        mRecyclerView.SetAdapter(mAdapter);

        bottomnavigationview1 = FindViewById<BottomNavigationView>
              (Resource.Id.bottom_navigation);
        bottomnavigationview1.NavigationItemSelected += BottomNavigation_NavigationItemSelected;

    }

    private void SetupList()
    {
        mEmails = new List<Email>();
        mEmails.Add(new Email() { date = "9/25/2019", Subject = "Wanna Hang Out?", Message = "I ' ll   be around  tomorrow!!" });
        mEmails.Add(new Email() { date = "9/25/2019", Subject = "Wanna Hang Out?", Message = "I ' ll   be around  tomorrow!!" });
        mEmails.Add(new Email() { date = "9/25/2019", Subject = "Wanna Hang Out?", Message = "I ' ll   be around  tomorrow!!" });
        mEmails.Add(new Email() { date = "9/24/2019", Subject = "Wanna Hang Out?", Message = "I ' ll   be around  tomorrow!!" });
        mEmails.Add(new Email() { date = "9/24/2019", Subject = "Wanna Hang Out?", Message = "I ' ll   be around  tomorrow!!" });
    }

大家好,我叫Taniguchi,我已经实现了recyclerview,并插入了按钮导航视图以删除选定的项目。当我在recyclerview中单击某个项目时,我选择了一个项目。我...

c# xamarin.android
1个回答
0
投票

如果只想删除选择的项目,则不需要存储位置,因为保存了检查状态,所以在[[电子邮件

© www.soinside.com 2019 - 2024. All rights reserved.