Xamarin Android: How to show checkbox when i long click a row in the recyclerview

Hello everyone my name is Taniguchi and i made a recyclerview and put checkboxes in each row, and the default visibility of this checkboxes is gone and i would like to show the corresponding row checkbox when i click.

How can i do that ?

thanks for any help

My Recycler View Click Listener:

public class RecyclerViewHolder : RecyclerView.ViewHolder
{
public TextView mName;
public TextView mSubject;
public TextView mMessage;
public CheckBox cbx;
public int count = 0;
private Action<object, View.LongClickEventArgs, int> onLongClick;

    public RecyclerViewHolder(View itemView) : base(itemView)
    {
        mName = itemView.FindViewById<TextView>(Resource.Id.textView1);
        mSubject = itemView.FindViewById<TextView>(Resource.Id.textView2);
        mMessage = itemView.FindViewById<TextView>(Resource.Id.textView3);
        cbx = itemView.FindViewById<CheckBox>(Resource.Id.checkBox1);
    }

}

My RecyclerView Adapter:

public class RecyclerAdapter : RecyclerView.Adapter, View.IOnClickListener, View.IOnLongClickListener
{
private View view;
private Boolean isSelected = false;

    public Boolean IsSelected()
    {
        return isSelected;
    }

    public void setSelected(Boolean selected)
    {
        isSelected = selected;
    }
    private Activity mActivity;
    private MyActionMode mActionMode;
    private List<Email> mEmails;
    private Context context;
    private ActionMode mode;
    public bool count = false;
    public event EventHandler<int> ItemClick;
    public RecyclerAdapter(List<Email> emails, Context context)
    {
        mEmails = emails;
        this.context = context;
    }

    public RecyclerAdapter(List<Email> emails, Activity activity)
    {
        mEmails = emails;
        mActivity = activity;
    }
    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);
        //RecyclerViewHolder vh = new RecyclerViewHolder(row, OnClick, OnLongClick);
        return vh;

    }
    public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {

        RecyclerViewHolder myHolder = holder as RecyclerViewHolder;
        myHolder.cbx.Checked = mEmails[position].IsSelected;
        myHolder.mName.Text = mEmails[position].Name;
        myHolder.mSubject.Text = mEmails[position].Subject;
        myHolder.mMessage.Text = mEmails[position].Message;
        myHolder.ItemView.SetBackgroundColor(mEmails[position].IsSelected() ? Color.LightBlue : Color.Transparent);
        myHolder.ItemView.Tag = position;
        myHolder.ItemView.SetOnClickListener(this);
        myHolder.ItemView.SetOnLongClickListener(this);
        //if (selectedPosition == position)
        //myHolder.ItemView.SetBackgroundColor(Color.LightBlue);
        //else
        // myHolder.ItemView.SetBackgroundColor(Color.Transparent);


    }
    public RecyclerViewHolder myHolder;

    void View.IOnClickListener.OnClick(View v)
    {


            int position = (int)v.Tag;
            mEmails[position].setSelected(!mEmails[position].IsSelected());
            v.SetBackgroundColor(mEmails[position].IsSelected() ? Color.LightBlue : Color.Transparent);
            Toast.MakeText(v.Context, "Click : " + count + "---" + position, ToastLength.Short).Show();


    }

    public bool OnLongClick(View v)
    {
        int position = (int)v.Tag;
        mEmails[position].setSelected(!mEmails[position].IsSelected());
        v.SetBackgroundColor(mEmails[position].IsSelected() ? Color.LightBlue : Color.Transparent);
        v.FindViewById(Resource.Id.checkBox1).;
        mActionMode = new MyActionMode(mActivity, this, position);
            mode = mActivity.StartActionMode(mActionMode);
        count = true;

        Toast.MakeText(v.Context, "Long Click : " + mEmails[position].IsSelected() + "---" + position, ToastLength.Short).Show();
        return true;
    }
}
public class MyActionMode : Java.Lang.Object, ActionMode.ICallback
{

    private RecyclerViewHolder holder;
    private Context mContext;
    private RecyclerView.Adapter mAdapter;
    private int currentPosition;
    private Button button;
    private IMenu menu;
    private View menuItemView;

    public MyActionMode(Context context) : this(context, null, 0)
    {

    }

    public MyActionMode(Context context, RecyclerView.Adapter adapter, int position)
    {
        mContext = context;
        mAdapter = adapter;
        currentPosition = position;
    }

    public bool OnActionItemClicked(ActionMode mode, IMenuItem item)
    {

        switch (item.ItemId)
        {
            case Resource.Id.itemOneId:
                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);
        button = (Button)menu.FindItem(Resource.Id.itemTwoId).ActionView;
        button.Background = null;
        var draw = ContextCompat.GetDrawable(mContext, Resource.Drawable.three_dots);
        button.SetCompoundDrawablesWithIntrinsicBounds(draw, null, null, null);
        //button.Text = "";
        button.Click += delegate {
            PopupMenu menu1 = new PopupMenu(mContext, button);
            menu1.Inflate(Resource.Menu.popup_menu);
            menu1.Show();
        };

        return true;
    }

    public void OnDestroyActionMode(ActionMode mode)
    {
        mode.Dispose();
    }

    public bool OnPrepareActionMode(ActionMode mode, IMenu menu)
    {
        return false;
    }
}

My Click Event:

void View.IOnClickListener.OnClick(View v)
{

            int position = (int)v.Tag;
            mEmails[position].setSelected(!mEmails[position].IsSelected());
            v.SetBackgroundColor(mEmails[position].IsSelected() ? Color.LightBlue : Color.Transparent);
            Toast.MakeText(v.Context, "Click : " + count + "---" + position, ToastLength.Short).Show();
        }

i would like to show the corresponding row checkbox when i click.

Tagged:

Best Answer

Answers

  • TaniguchiSantosTaniguchiSantos Member ✭✭✭

    Thanks so much @Jarvan

Sign In or Register to comment.