11

में दृश्य की स्थिति प्राप्त करना मैं एक ImageView और टेक्स्ट व्यू के साथ एक पंक्ति पंक्ति के साथ एक RecyclerView का उपयोग कर रहा हूँ।ऑन क्रिएटिव्यूहोल्डर

मैं दृश्य के लिए ऑनक्लिक लिस्टनर को कार्यान्वित करना चाहता हूं और अलग दृश्यहोल्डर ऑब्जेक्ट्स के लिए नहीं। मैं एडाप्टर में दृश्य की स्थिति कैसे प्राप्त कर सकता हूं?

अभी मैं क्लिक पर टिप्पणियां हटा रहा हूं, लेकिन मैं क्लिक किए गए व्यू का चयन नहीं कर सकता। मैंने उपयुक्त लाइन में एक TODO जोड़ा।

public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.ViewHolder> { 

    /** List of Comment objects */ 
    private List<Comment> mCommentList; 

    /** Database with Comment objects */ 
    private CommentsDataSource mDataSource; 

    /** 
    * Construcutor for CommentAdapter 
    * 
    * @param commentList List of Comment objects 
    * @param dataSource Database with Comment objects 
    */ 
    public CommentAdapter(List<Comment> commentList, CommentsDataSource dataSource) { 
     this.mCommentList = commentList; 
     this.mDataSource = dataSource; 
    } 

    /** 
    * Add Comment objects to RecyclerView 
    * 
    * @param position The position where the Comment object is added 
    * @param comment Comment Object 
    */ 
    public void add(int position, Comment comment) { 
     mCommentList.add(position, comment); 
     notifyItemInserted(position); 
    } 

    /** 
    * Remove Comment objects from RecyclerView 
    * 
    * @param comment Comment Object 
    */ 
    public void remove(Comment comment) { 
     int position = mCommentList.indexOf(comment); 
     // Avoid double tap remove 
     if (position != -1) { 
      mCommentList.remove(position); 
      mDataSource.deleteComment(comment); 
      notifyItemRemoved(position); 
     } 
    } 

    @Override 
    public ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) { 
     final View view = LayoutInflater.from(parent.getContext()) 
       .inflate(R.layout.single_line_row, parent, false); 
     view.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // TODO get position 
       remove(mCommentList.get(getItemCount() - 1)); 
      } 
     }); 
     return new ViewHolder(view); 
    } 

    @Override 
    public void onBindViewHolder(ViewHolder holder, int position) { 
     final Comment comment = mCommentList.get(position); 
     holder.comment.setText(comment.getComment()); 
    } 

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

    public static class ViewHolder extends RecyclerView.ViewHolder { 

     /** ImageView icon */ 
     public ImageView icon; 

     /** TextView comment */ 
     public TextView comment; 

     /** 
     * Constructor for ViewHolder 
     * 
     * @param itemView Layout for each row of RecyclerView 
     */ 
     public ViewHolder(final View itemView) { 
      super(itemView); 
      icon = (ImageView) itemView.findViewById(R.id.icon); 
      comment = (TextView) itemView.findViewById(R.id.comment); 
     } 
    } 
} 
+0

तुम सच में ViewHolder में अपने OnClickListener स्थापित करना चाहिए (अंतिम दृश्य itemView) निर्माता, उस मामले में ViewHolder को लागू करना चाहिए OnClickListener – pskink

उत्तर

31

आप एक कॉलबैक में onBindViewHolder की position पैरामीटर का उपयोग नहीं कर सकते। यदि कोई नया आइटम ऊपर जोड़ा गया है, तो रीसाइक्लर व्यू आपके आइटम को दोबारा नहीं करेगा, इसलिए स्थिति अप्रचलित है। इसके बजाय, RecyclerView ViewHolder पर getAdapterPosition विधि प्रदान करता है।

@Override 
public ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) { 
    final View view = LayoutInflater.from(parent.getContext()) 
      .inflate(R.layout.single_line_row, parent, false); 
    final ViewHolder holder = new ViewHolder(view); 
    view.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      final int position = holder.getAdapterPosition(); 
      if (position != RecyclerView.NO_POSITION) { 
       remove(mCommentList.get(position)); 
      } 
     } 
    }); 
    return holder; 
} 

मैं क्योंकि जब आइटम निकाल दिया जाता है, RecyclerView देखें तो यह अभी भी उपयोगकर्ता द्वारा क्लिक किया जा सकता है, लेकिन इसके एडाप्टर स्थिति NO_POSITION वापस आ जाएगी बाहर फीका करना होगा position != RecyclerView.NO_POSITION जांच जोड़ दिया है।

+2

जो भी मैं क्लिक करता हूं, मुझे स्थिति -1 (RecyclerView.NO_POSITION) मिल रहा है, यह समस्या क्या होगी? – akki

-1

ओवरराइड getItemViewType विधि:

@Override 
public int getItemViewType(int position) { 
    //your code 
    return position; 
} 

अब स्थिति viewType

पर उपलब्ध है
@Override 
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     int position=viewType; 

     View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recursive, parent, false); 
     return new ViewHolder(view); 
    } 
+5

ऐसा न करें - यह एडाप्टर को आपकी सूची में प्रत्येक आइटम के लिए रीसायकल करने के बजाए एक नया दृश्य बना देगा, क्योंकि ऐसा लगता है कि वे सभी अलग-अलग प्रकार हैं। – starkej2

संबंधित मुद्दे