2017-04-08 66 views
1

我有一個RecyclerView與我想在我的主要活動中實現的自定義偵聽器,以便在RecyclerView項目被點擊時得到通知。RecyclerView適配器自定義偵聽器接口

但是,我不知道在哪裏把setMyAdapterListener放在適配器中。我會在哪裏放?

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { 
    private Context context; 

    private List<Data> data; 

    private MyAdapterListener myAdapterListener; 

    public MyAdapter(Context context, List<Object> data) { 
     this.context = context; 
     this.data = data; 
    } 

    public interface MyAdapterListener { 
     void onContainerClick(); 
    } 

    public void setMyAdapterListener(MyAdapterListener myAdapterListener) { 
     this.myAdapterListener = myAdapterListener; 
    } 

    public class ViewHolderItem extends RecyclerView.ViewHolder implements View.OnClickListener { 
     public LinearLayout container; 

     public ImageView poster; 

     public ViewHolderItem(View v) { 
      super(v); 

      container = (LinearLayout) v.findViewById(R.id.container); 

      poster = (ImageView) v.findViewById(R.id.poster); 
     } 

     @Override 
     public void onClick(View v) { 
      int id = v.getId(); 

      if (id == R.id.container) { 
       if (myAdapterListener != null) { 
        myAdapterListener.onContainerClick(); 
       } 
      } 
     } 
    } 

    @Override 
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     RecyclerView.ViewHolder viewHolder = null; 
     LayoutInflater inflater = LayoutInflater.from(parent.getContext()); 

     View v2 = inflater.inflate(R.layout.item_layout, parent, false); 
     viewHolder = new ViewHolderItem(v2); 

     return viewHolder; 
    } 

    @Override 
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 
     Data item = (Data) data.get(position); 

     holder.poster.setImageDrawable(someDrawable); 
    } 

    @Override 
    public int getItemCount() { 
     return data.size(); 
    } 

} 

回答

1

取出setMyAdapter()只是通過你目前在你的setMyAdapterListener爲您recyclerview的適配器構造器的一部分參數,見下圖:

public MyAdapter(Context context, List<Object> data, MyAdapterListener myAdapterListener) { 
     this.context = context; 
     this.data = data; 
     this.myAdapterListener = myAdapterListener; 
    } 

//你的活動,你必須執行你的界面的方法。

0

作爲@ScottS表示您不需要setMyAdapter()方法,只需將onclick listener接口類作爲參數傳遞給適配器構造函數,然後在Activity類中實現接口方法即可。代碼如下所示。

public MyAdapter(Context context, List<Object> data) { 
 
     this.context = context; 
 
     this.data = data; 
 
      this.myAdapterListener = myAdapterListener; 
 
    } 
 
    
 
    public interface MyAdapterListener { 
 
    void onContainerClick(); // please provide 2 parameter that will help you more for manipulation like shown below 
 
/// void onContainerClick(View view, int position); 
 
} 
 

 
} 
 

 
public class ViewHolderItem extends RecyclerView.ViewHolder implements View.OnClickListener { 
 
    public LinearLayout container; 
 

 
    public ImageView poster; 
 

 
    public ViewHolderItem(View v) { 
 
     super(v); 
 

 
     container = (LinearLayout) v.findViewById(R.id.container); 
 

 
     poster = (ImageView) v.findViewById(R.id.poster); 
 

 
     container =setOnClickListener(this); /// this line must include 
 
    } 
 

 
    @Override 
 
    public void onClick(View v) { 
 
     int id = v.getId(); 
 

 
     if (id == R.id.container) { 
 
      if (myAdapterListener != null) { 
 
       myAdapterListener.onContainerClick(); 
 
       // myAdapterListener.onContainerClick(v,getAdapterPosition()); 
 
      } 
 
     } 
 
    } 
 
}

然後你就可以處理你的活動或其它地方發生的事件您RecyclerView正在使用:

mAdapter = new MyAdapter(getApplicationContext() , data, new MyAdapter.MyAdapterListener() { 
 
        @Override 
 
        public void onContainerClick(View v, int position) { 
 
         Log.d(TAG, "iconTextViewOnClick at position "+position); 
 
        } 
 

 
       }); 
 
mRecycler.setAdapter(mAdapter);