从mainActivity中的适配器获取ItemClick

时间:2018-07-11 20:56:09

标签: c# xamarin

我正在使用回收站视图,到目前为止,我已经在适配器内创建了一个项目单击事件。它显示名称和ID。但是如何在我的主要活动中采用此项的点击值?

适配器:

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

        public TextView txt_Name;
        public TextView txt_ID;
        private ItemClickListener itemClickListener;
        public RecyclerViewHolder(View itemView):base(itemView)
        {
            txt_ID = itemView.FindViewById<TextView>(Resource.Id.textID);
            txt_Name = itemView.FindViewById<TextView>(Resource.Id.textName);
            itemView.SetOnClickListener(this);

        }
        public void SetItemClickListener(ItemClickListener itemClickListener)
        {
            this.itemClickListener = itemClickListener;
        }
        public void OnClick(View v)
        {
            itemClickListener.OnClick(v, AdapterPosition, false);
        }


    }

    public class RecyclerAdapter : RecyclerView.Adapter, ItemClickListener
    {
        private List<CategoryClass> listData = new List<CategoryClass>();
        private Context context;
        public RecyclerAdapter(List<CategoryClass> listData,Context context)
        {
            this.listData = listData;
            this.context = context;
        }
        public override int ItemCount
        {
            get
            {
                return listData.Count;
            }
        }

        public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
        {
            RecyclerViewHolder vh = holder as RecyclerViewHolder;
            vh.txt_ID.Text = Convert.ToString(listData[position].CategoryID);
            vh.txt_Name.Text = listData[position].CategoryName;
            vh.SetItemClickListener(this);
        }

        public void OnClick(View itemView, int position, bool isLongClick)
        {
                Toast.MakeText(context, Convert.ToString(listData[position].CategoryID), ToastLength.Short).Show();
        }

        public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
        {
            LayoutInflater inflater = LayoutInflater.From(parent.Context);
            View itemView = inflater.Inflate(Resource.Layout.List_Item_Recycler_View, parent, false);
            return new RecyclerViewHolder(itemView);
        }
    }

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

这是我的MainActivity:

   List<CategoryClass> listData = new List<CategoryClass>();
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);
        SetupList();
        var recyclerView = FindViewById<RecyclerView>(Resource.Id.recuclerView);
        recyclerView.HasFixedSize = true;
        var layoutManager = new LinearLayoutManager(this);
        recyclerView.SetLayoutManager(layoutManager);

        RecyclerAdapter adapter = new RecyclerAdapter(listData, this);
        recyclerView.SetAdapter(adapter);


    }

    private void SetupList()
    {
        for (int i = 0; i < 99; i++)
        {
            listData.Add(new CategoryClass() { CategoryID = i, CategoryName = "MyName" });

        }
    }

如何获取主要活动示例listData [position] .CategoryID? 有什么方法可以在我的主要活动中点击创建?

0 个答案:

没有答案