2016-02-23 123 views
3

我在一個片段中製作了水平回收站。現在,當我點擊任何項目時,我看不到點擊監聽器正在工作。這裏是我的適配器類代碼:
RecyclerView OnClick不能正常工作

public class FeaturedProductsAdapter extends RecyclerView.Adapter<FeaturedProductsAdapter.CustomViewHolder> { 
private List<FeaturedProductInfo> feedItemList; 
private Context mContext; 

public FeaturedProductsAdapter(Context context, List<FeaturedProductInfo> feedItemList) { 
    this.feedItemList = feedItemList; 
    this.mContext = context; 
} 
public class CustomViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 
    protected ImageView imageView; 
    protected TextView textView,priceView; 
    private Context context; 


    public CustomViewHolder(View view,Context context) { 

     super(view); 
     this.context=context; 
     this.imageView = (ImageView) view.findViewById(R.id.thumbnail); 
     this.textView = (TextView) view.findViewById(R.id.prodTitle); 
     this.priceView = (TextView) view.findViewById(R.id.prodPrice); 
     view.setOnClickListener(this); 

    } 

    @Override 
    public void onClick(View view) { 

     int position = getLayoutPosition(); // gets item position 
     Log.e("Check", position + ""); 
     FeaturedProductInfo user = feedItemList.get(position);//[position]; 
     // We can access the data within the views 
     Intent intent = new Intent(context, ProductDescription.class); 
     intent.putExtra("id", user.getId()); 
     mContext.startActivity(intent); 


    } 

} 

@Override 
public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 
    View view = LayoutInflater.from(mContext).inflate(R.layout.featured_product_list_item_card, null); 
    Context context = viewGroup.getContext(); 

    CustomViewHolder viewHolder = new CustomViewHolder(view,context); 

    return viewHolder; 
} 

@Override 
public void onBindViewHolder(CustomViewHolder customViewHolder, int i) { 
    FeaturedProductInfo feedItem = feedItemList.get(i); 

    //Download image using picasso library 
    if(!feedItem.getUrl().contains(".")) 
    { 
     feedItem.setUrl("nothing"); 
    } 
    Picasso.with(mContext).load(feedItem.getUrl()) 
      .error(R.drawable.unavailable) 
      .placeholder(R.drawable.unavailable) 
      .resize(110,110) 
      .into(customViewHolder.imageView); 

    //Setting text view title 
    customViewHolder.textView.setText(feedItem.getTitle()); 
    customViewHolder.priceView.setText(feedItem.getPrice()); 
    //Log.e("Featured: ","SET"); 
} 

@Override 
public int getItemCount() { 
    return (null != feedItemList ? feedItemList.size() : 0); 
} 

}

我覺得我沒有得到如何正確使用的觀點持有者。雖然我在其他活動中使用了與recyclerView相同的代碼,但它的作用與魅力相似。

+0

它是不是在所有的射擊或它只是正確的射擊。您應該在bindViewHolder()期間設置onClick,因爲您只在初始時創建視圖保持器,然後在滾動列表時「回收」它。 – Helix

+0

@Helix它根本沒有發射。 –

+0

未設置視圖的背景嗎?也爲調試添加敬酒。 – Helix

回答

8

1.Simple內ViewHolder

RecyclerView點擊處理器不具備的點擊處理程序附加到項目不像ListView它具有方法setOnItemClickListener()特別規定。爲了達到類似的效果,我們可以將點擊我們的適配器內ViewHolder內的事件:

public class ContactsAdapter extends RecyclerView.Adapter<ContactsAdapter.ViewHolder> { 
    // ... 

    // Used to cache the views within the item layout for fast access 
    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 
     public TextView tvName; 
     public TextView tvHometown; 
     private Context context; 

     public ViewHolder(Context context, View itemView) { 
      super(itemView); 
      this.tvName = (TextView) itemView.findViewById(R.id.tvName); 
      this.tvHometown = (TextView) itemView.findViewById(R.id.tvHometown); 
      // Store the context 
      this.context = context; 
      // Attach a click listener to the entire row view 
      itemView.setOnClickListener(this); 
     } 

     // Handles the row being being clicked 
     @Override 
     public void onClick(View view) { 
      int position = getLayoutPosition(); // gets item position 
      User user = users.get(position); 
      // We can access the data within the views 
      Toast.makeText(context, tvName.getText(), Toast.LENGTH_SHORT).show(); 
     } 
    } 

    // ... 
} 

另一種方式是我的首選方法。但是這也是一個好方法去做。

我onBindViewHolder

@Override 
    public void onBindViewHolder(CategoryViewHolder holder, int position) { 
     Category category = mCategories.get(position); 

     holder.tvTitle.setText(category.getTitle()); 
     holder.tvDescription.setText(category.getDescription()); 

     holder.rlContainer.setOnClickListener(mClickListener); 
     holder.rlContainer.setTag(holder); 
    } 

我的職業等級(View.OnClickListner的Adapter對象)

View.OnClickListener mClickListener = new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      CategoryViewHolder holder = (CategoryViewHolder) view.getTag(); 
      int position = holder.getAdapterPosition(); 

      startAppointmentBookingFor(mCategories.get(position)); 
     } 
    }; 

所以基本上監聽器附加在你持有任何觀點(我儘量把它放在容器只有),然後提取出來在onclick和處理位置等

+0

嘿,我在那裏使用了相同的方法,而且沒有工作。 –

+0

這就是爲什麼我用另一種方式輸入我最喜歡的方式:),試試這個@AkshayBhasin – yUdoDis

+0

這是什麼rlContainer? –

0

進行以下修改您的適配器:

public class FeaturedProductsAdapter extends RecyclerView.Adapter<FeaturedProductsAdapter.CustomViewHolder> { 
private List<FeaturedProductInfo> feedItemList; 
private Context mContext; 
private OnItemClickListener onItemClickListener; 

public FeaturedProductsAdapter(Context context, List<FeaturedProductInfo,OnItemClickListener onItemClickListener> feedItemList) { 
    this.feedItemList = feedItemList; 
    this.mContext = context; 
    this.onItemClickListener = onItemClickListener; 

} 
public class CustomViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 
    protected ImageView imageView; 
    protected TextView textView,priceView; 
    private Context context; 


    public CustomViewHolder(View view,Context context) { 

     super(view); 
     this.context=context; 
     this.imageView = (ImageView) view.findViewById(R.id.thumbnail); 
     this.textView = (TextView) view.findViewById(R.id.prodTitle); 
     this.priceView = (TextView) view.findViewById(R.id.prodPrice); 
     view.setOnClickListener(this); 

    } 

    @Override 
    public void onClick(View view) { 

     onItemClickListener.onItemClick(getLayoutPosition()); 
     Log.e("Check", position + ""); 
     FeaturedProductInfo user = feedItemList.get(position);//[position]; 
     // We can access the data within the views 
     Intent intent = new Intent(context, ProductDescription.class); 
     intent.putExtra("id", user.getId()); 
     mContext.startActivity(intent); 


    } 

} 

public interface OnItemClickListener{ 
    void onItemClick(int position); 
} 

@Override 
public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 
    View view = LayoutInflater.from(mContext).inflate(R.layout.featured_product_list_item_card, null); 
    Context context = viewGroup.getContext(); 

    CustomViewHolder viewHolder = new CustomViewHolder(view,context); 

    return viewHolder; 
} 

@Override 
public void onBindViewHolder(CustomViewHolder customViewHolder, int i) { 
    FeaturedProductInfo feedItem = feedItemList.get(i); 

    //Download image using picasso library 
    if(!feedItem.getUrl().contains(".")) 
    { 
     feedItem.setUrl("nothing"); 
    } 
    Picasso.with(mContext).load(feedItem.getUrl()) 
      .error(R.drawable.unavailable) 
      .placeholder(R.drawable.unavailable) 
      .resize(110,110) 
      .into(customViewHolder.imageView); 

    //Setting text view title 
    customViewHolder.textView.setText(feedItem.getTitle()); 
    customViewHolder.priceView.setText(feedItem.getPrice()); 
    //Log.e("Featured: ","SET"); 
} 

@Override 
public int getItemCount() { 
    return (null != feedItemList ? feedItemList.size() : 0); 
} 
+0

不起作用。現在我甚至看不到物品上的onClick效果。 –

+0

顯示您設置適配器的MainActivity的代碼 –

+0

檢查更新的答案 –