2016-02-21 53 views
1

我是Android開發人員的初學者,我正在努力將回收適配器類中Click事件的Clicked_Message傳遞給其他活動。這是使用意圖的好方法嗎?如果是這樣,我怎麼能通過上下文點擊事件?謝謝Xamarin從OnClick事件傳遞上下文

public class RecyclerAdapter : RecyclerView.Adapter 
     { 
      private RecyclerView mRecyclerView; 
      private List<NotificationClass> mEmails; 

      public RecyclerAdapter(List<NotificationClass> emails, RecyclerView recyclerView) 
      { 
       mEmails = emails; 
       mRecyclerView = recyclerView; 
      } 
      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 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.txtName); 
       TextView txtSubject = row.FindViewById<TextView>(Resource.Id.txtSubject); 
       TextView txtMessage = row.FindViewById<TextView>(Resource.Id.txtMessage); 
       MyView view = new MyView(row) { mName = txtName, mSubject = txtSubject, mMessage = txtMessage }; 
       return view; 
      } 
      public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position) 
      { 

       MyView myHolder = holder as MyView; 
       int indexPosition = (mEmails.Count - 1) - position; 
       myHolder.mMainView.Click += mMainView_Click; 
       myHolder.mName.Text = mEmails[position].Name; 
       myHolder.mSubject.Text = mEmails[position].Subject; 
       myHolder.mMessage.Text = mEmails[position].Message; 
      } 
      public override int ItemCount 
      { 
       get { return mEmails.Count; } 
      } 
      public void OnClick(int position) 
      { 
       if (ItemClick != null) 
        ItemClick(this, position); 
      } 
      public void mMainView_Click(object sender, EventArgs e,Context context) 
      { 
       int position = mRecyclerView.GetChildPosition((View)sender); 
       int indexPosition = (mEmails.Count - 1) - position; 
       Console.WriteLine(mEmails[indexPosition].Message); 
       string Clicked_Message = (mEmails[indexPosition].Message); 

       var activity2 = new Intent(context, typeof(ContactActivity)); 
       activity2.PutExtra("MyData", Clicked_Message); 
       context.StartActivity(activity2); 
      } 
     } 

回答

0

您不需要傳遞上下文。只要使用意圖,並​​將想要傳遞的信息作爲附加內容加入意圖。

如果您的適配器需要上下文,請將其通過構造函數傳遞並作爲字段成員存儲。

0

這是我的一個觀點持有者RecyclerView.Adapter的典型實現...

public class ContactsAdapter : V7.RecyclerView.Adapter 
{ 
    private List<Contact> _contacts; 

    public event EventHandler ItemClick; 
    public void OnItemClick(ContactViewHolder holder) 
    { 
     if (ItemClick != null) 
     { 
      ItemClick(holder, EventArgs.Empty); 
     } 
    } 

    public ContactsAdapter(List<Contact> contacts) 
     : base() 
    { 
     _contacts = contacts; 
    } 

    public override void OnBindViewHolder(V7.RecyclerView.ViewHolder holder, int position) 
    { 
     var contactHolder = (ContactViewHolder)holder; 
     contactHolder.BindUI(_contacts[position]); 
    } 

    public override V7.RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) 
    { 
     var view = LayoutInflater.FromContext(parent.Context).Inflate(Resource.Layout.ContactsListItem, parent, false); 
     return new ContactViewHolder(view) 
     { 
      Adapter = this 
     }; 
    } 

    public override int ItemCount 
    { 
     get 
     { 
      return _contacts.Count; 
     } 
    } 
} 

查看持有人(通常是在同一個文件適配器)

public class ContactViewHolder : V7.RecyclerView.ViewHolder, View.IOnClickListener 
{ 
    public TextView ContactNameTextView { get; set; } 
    public TextView ContactPhoneTextView { get; set; } 
    public TextView ContactIntialsTextView { get; set; } 

    public Contact Contact { get; set; } 

    private WeakReference _adapter; 
    public ContactsAdapter Adapter 
    { 
     get { return (ContactsAdapter)_adapter.Target; } 
     set { _adapter = new WeakReference(value); } 
    } 

    public ContactViewHolder(View view) 
     : base(view) 
    { 
     GetUI(view); 
     view.SetOnClickListener(this); 
    } 

    private void GetUI(View view) 
    { 
     ContactNameTextView = view.FindViewById<TextView>(Resource.Id.ContactName); 
     ContactPhoneTextView = view.FindViewById<TextView>(Resource.Id.ContactPhone); 
     ContactIntialsTextView = view.FindViewById<TextView>(Resource.Id.ContactInitialsTextView); 
    } 

    public void BindUI(Contact contact) 
    { 
     Contact = contact; 

     ContactNameTextView.Text = contact.ContactName; 
     ContactPhoneTextView.Text = contact.Phone1; 
     ContactIntialsTextView.Text = contact.Initials; 
    } 

    public void OnClick(View v) 
    { 
     Adapter.OnItemClick(this); 
    } 
} 

這種封裝視圖持有者的功能。我也將適配器的實例作爲WeakReference提供給視圖持有者。這允許我調用OnItemClick,傳遞查看者的實例。如果您會注意到視圖持有者也具有它所代表的對象的實例。這意味着我不必擔心所選的索引。我已經有了可用的對象數據。

所以在活動/片段的實現是這樣的...

public override void OnCreate(Bundle savedInstanceState) 
{ 
    base.OnCreate(savedInstanceState); 

    _contacts = Contact.GetAllContacts(); 

    _adapter = new ContactsAdapter(_contacts); 
    _adapter.ItemClick += ContactSelected; 
} 

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{ 
    var view = inflater.Inflate(Resource.Layout.ContactsFragment, container, false); 

    var layoutManager = new V7.LinearLayoutManager(this.Activity) { Orientation = V7.LinearLayoutManager.Vertical }; 

    _contactsView = view.FindViewById<V7.RecyclerView>(Resource.Id.ContactList); 
    _contactsView.SetAdapter(_adapter); 
    _contactsView.HasFixedSize = true; 
    _contactsView.SetLayoutManager(layoutManager); 

    return view; 
} 

private void ContactSelected (object sender, EventArgs e) 
{ 
    var holder = (ContactViewHolder)sender; 
    var detailFragment = new ContactDetailsFragment(holder.Contact); 
    MainActivity.ShowFragment(detailFragment); 
} 

我給聯繫到一個片段,但你可以做的使用意圖的活動類似的東西。

現在是否這是在RecyclerView中處理點擊行的最有效方式,我不知道。但是這個實現一直在爲我工作。