Forum Xamarin.Android

The RecyclerView item click on xamarin android is not working

Hello everyone my name is Taniguchi and i created a recyclerview and i want to create i click listener to show an contextual action bar according to this documentation Contextual Action Bar with RecyclerView in xamarin android? but its not working, when i click a item on recyclerview nothing happens.

my click listener:

public class RecyclerViewHolder : RecyclerView.ViewHolder, View.IOnClickListener, View.IOnLongClickListener
{

    public TextView mName;
    public TextView mSubject;
    public TextView mMessage;
    private ItemClickListener itemClickListener;
    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);
        itemView.SetOnClickListener(this);
        itemView.SetOnLongClickListener(this);
    }
    public void SetItemClickListener(ItemClickListener itemClickListener)
    {
        this.itemClickListener = itemClickListener;
    }

    public bool OnLongClick(View v)
    {
        itemClickListener.OnClick(v, AdapterPosition, true);
        return true;
    }

    public void OnClick(View v)
    {
        itemClickListener.OnClick(v, AdapterPosition, false);
    }
}

The click listener interface:

public interface ItemClickListener
{
void OnClick(View itemView, int position, bool isLongClick);
}

The class to create to contextual action bar:

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;
}

}

MyRecyclerView Class

public class RecyclerAdapter : RecyclerView.Adapter, ItemClickListener
{
// add this variable

    private Activity mActivity;
    private MyActionMode mActionMode;
    private List<Email> mEmails;
    private Context context;
    public RecyclerAdapter (List<Email> emails, Context context)
    {
        mEmails = emails;
        this.context = context;
    }
    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 void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {
        RecyclerViewHolder vh = holder as RecyclerViewHolder;
        MyView myHolder = holder as MyView;
        myHolder.mName.Text = mEmails[position].Name;
        myHolder.mSubject.Text = mEmails[position].Subject;
        myHolder.mMessage.Text = mEmails[position].Message;

    }

    public void OnClick(View itemView, int position, bool isLongClick)
    {
        if (isLongClick)
        {
            Toast.MakeText(context, "Long Click : " + mEmails[position], ToastLength.Short).Show();

        }
        else
        {
            Toast.MakeText(context, "Long Click : " + mEmails[position], ToastLength.Short).Show();
        }
    }

    public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
    {
        View row = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.row, parent, false);
        TextView txtName = row.FindViewById<TextView>(Resource.Id.textView1);
        TextView txtSubject = row.FindViewById<TextView>(Resource.Id.textView2);
        TextView txtMessage = row.FindViewById<TextView>(Resource.Id.textView3);

        MyView view = new MyView(row) { mName = txtName, mSubject = txtSubject, mMessage = txtMessage };
        return view;
    }

}

when i Call the recyclerview:

protected override void OnCreate(Bundle bundle)
{ //set our view from the Main layout Resource
base.OnCreate(bundle);
SetContentView(Resource.Layout.activity_main);
mRecyclerView = FindViewById(Resource.Id.recyclerViwer);
mRecyclerView.HasFixedSize = true;
SetupList();

    //Create our layout Manager
    mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.SetLayoutManager(mLayoutManager);
    RecyclerAdapter mAdapter = new RecyclerAdapter(mEmails,this);
    mRecyclerView.SetAdapter(mAdapter);

}

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!!" });
    }
}

Best Answer

  • TaniguchiSantosTaniguchiSantos Member ✭✭✭
    Accepted Answer

    Thanks for answering @Jarvan

    I had two issues

    one: the item click listener wasnst working so to fix this here the code:

    On the RecyclerViewr Adapter;
    On the OncreateViewHolder

    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, OnClick, OnLongClick);
    return vh;

            }
    

    two: the contextual action bar wanst showing, so to fix this here s the code:
    the mActivity was returning null
    so in he recyclerview adapter i included

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

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai

    It seems that you didn't add the StartActionMode code.

    public class CustomAdapter : RecyclerView.Adapter
    {
        private List<model> list;
        Activity mActivity;
    
    
        public CustomAdapter(List<model> list, Activity activity)
        {
            this.list = list;
            mActivity = activity;
        }
        void OnLongClick(object sender, View.LongClickEventArgs args, int position)
        {
            MyActionMode mActionMode = new MyActionMode(mActivity, this, position);
            ActionMode mode = mActivity.StartActionMode(mActionMode);
            return;
        }
    }
    public class MyActionMode : Java.Lang.Object, ActionMode.ICallback
    {
        private Context mContext;
        private CustomAdapter mAdapter;
        private int currentPosition;
        public MyActionMode(Context context) : this(context, null, 0)
        {
    
        }
    
        public MyActionMode(Context context, CustomAdapter adapter, int position)
        {
            mContext = context;
            mAdapter = adapter;
            currentPosition = position;
        }
        ...
    
        public bool OnCreateActionMode(ActionMode mode, IMenu menu)
        {
            mode.MenuInflater.Inflate(Resource.Menu.ContextualMenu, menu);
            return true;
        }
        ...
    }
    

    Sample code, you can refer to: https://github.com/elvisxia/RecyclerViewer

    Similar issue:
    https://stackoverflow.com/questions/45929315/how-to-delete-row-in-recyclerlistview-on-click-of-menu-button-in-contextual-acti

  • JarvanJarvan Member, Xamarin Team Xamurai

    @TaniguchiSantos
    Has the problem been solved? If not, try to post the particular error with the corresponding codes here.

  • TaniguchiSantosTaniguchiSantos Member ✭✭✭
    Accepted Answer

    Thanks for answering @Jarvan

    I had two issues

    one: the item click listener wasnst working so to fix this here the code:

    On the RecyclerViewr Adapter;
    On the OncreateViewHolder

    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, OnClick, OnLongClick);
    return vh;

            }
    

    two: the contextual action bar wanst showing, so to fix this here s the code:
    the mActivity was returning null
    so in he recyclerview adapter i included

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

Sign In or Register to comment.