9

Ich habe eine Zeile in der Recycleransicht erstellt und darin habe ich zwei oder mehr Zeilen aufgebläht, aber beim Scrollen werden die Elemente wieder verwendet. Ich bekomme nicht, wo die Ansicht zu recyceln oder sie dynamisch zu entfernen Ich brauche einen Feed und ihre Kommentare.Für Kommentare muss ich Layouts aufblasen, um sie unterhalb Feeds anzuzeigen, für die getCommentInflaterView-Methode erstellt wird.So verhindern Sie, dass Artikel beim Scrollen der Recycler-Ansicht doppelt angezeigt werden

Die mit getCommentInflaterView() erstellte Ansicht wird jedoch dupliziert. MyAdapterClass:

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

    ArrayList<Data> arrayListAllFeedsData; 
    Comment objComment; 


    Context context; 

    public AllFeedsAdapter(Context context, ArrayList<Data> data) { 
     this.context = context; 
     arrayListAllFeedsData = new ArrayList<>(); 
     arrayListAllFeedsData = data; 
    } 


    @Override 
    public AllFeedsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     Context context = parent.getContext(); 
     LayoutInflater inflater = LayoutInflater.from(context); 

     // Inflate the custom layout 
     View post_row_view = inflater.inflate(R.layout.row_teacher_post, parent, false); 

     // Return a new holder instance 
     ViewHolder viewHolder = new ViewHolder(post_row_view); 
     return viewHolder; 
    } 

    @Override 
    public void onBindViewHolder(AllFeedsAdapter.ViewHolder holder, int position) { 

     holder.txtUsernamePostCreator.setText(arrayListAllFeedsData.get(position).getFull_name()); 
     holder.txtPostContent.setText(arrayListAllFeedsData.get(position).getFeed_text()); 
     holder.txtPostLikeCounter.setText(arrayListAllFeedsData.get(position).getTotal_like()); 
     holder.txtPostCommentsCounter.setText(arrayListAllFeedsData.get(position).getTotal_comment()); 

     if (arrayListAllFeedsData.get(position).getComment().size() > 0) { 


      if (arrayListAllFeedsData.get(position).getComment().size() > 2) { 
       for (int i = 0; i < 2; i++) { 

        objComment = arrayListAllFeedsData.get(position).getComment().get(i); 

        if (getCommentInflaterView(objComment) == null) { 
         holder.llCommentRowInflater.addView(getCommentInflaterView(objComment)); 
        } else { 
         holder.llCommentRowInflater.removeAllViews(); 
        } 


       } 
      } else { 
       for (int i = 0; i < arrayListAllFeedsData.get(position).getComment().size(); i++) { 

        objComment = arrayListAllFeedsData.get(position).getComment().get(i); 
        if (getCommentInflaterView(objComment) == null) { 
         holder.llCommentRowInflater.addView(getCommentInflaterView(objComment)); 

        } else { 
         holder.llCommentRowInflater.removeAllViews(); 
        } 

       } 
      } 

     } 


    } 

    private View getCommentInflaterView(Comment commentData) { 

     LayoutInflater layoutInflater = LayoutInflater.from(context); 
     View v = layoutInflater.inflate(R.layout.post_comments_list_item, null, false); 

     TextView txtCommenterUsername = (TextView) v.findViewById(R.id.txt_username_commenter); 
     TextView txtCommenterComment = (TextView) v.findViewById(R.id.txt_comments_from_commenter); 
     TextView txtCommentDuration = (TextView) v.findViewById(R.id.txt_comment_duration); 


     txtCommenterUsername.setText(commentData.getUsername()); 
     txtCommenterComment.setText(commentData.getComment()); 
     txtCommentDuration.setText(commentData.getCommentBy()); 


     return v; 

    } 

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

    /** 
    * USed to create static class for all the view if listitem child 
    */ 
    public static class ViewHolder extends RecyclerView.ViewHolder { 
     // Your holder should contain a member variable 
     // for any view that will be set as you render a row 

     public LinearLayout llParentTeacherPost, llCommentRowInflater; 

     // We also create a constructor that accepts the entire item row 
     // and does the view lookups to find each subview 
     public ViewHolder(View itemView) { 
      // Stores the itemView in a public final member variable that can be used 
      // to access the context from any ViewHolder instance. 
      super(itemView); 
      llParentTeacherPost = (LinearLayout) itemView.findViewById(R.id.ll_parent_teacher_post); 
      llCommentRowInflater = (LinearLayout) itemView.findViewById(R.id.ll_comment_row_inflater); 

      imgDpPostCreator = (ImageView) itemView.findViewById(R.id.img_dp_post_creator); 
      txtUsernamePostCreator = (TextView) 
     } 


    } 
} 

Antwort

-2

Am Ende onBindViewHolder Verfahren umfassen: holder.itemView.setTag(arrayListAllFeedsData.get(position));

+0

Die Lösung hat nicht funktioniert –

3

in Ihrem Adapter-Klasse AllFeedsAdapter, müssen Sie die Methode außer Kraft zu setzen:

@Override 
public long getItemId(int position){ 
// return specific item's id here 
} 
+1

Bitte genau auf was Sie wollen? Werfen Sie einen Blick darauf, wie Sie eine Frage stellen können http://stackoverflow.com/help/how-to-ask – selva

57

außer Kraft setzen diese beiden methodes in Adapter .

@Override 
public long getItemId(int position) { 
    return position; 
} 

@Override 
public int getItemViewType(int position) { 
    return position; 
} 
+2

Nach fast 1 Stunde suchen und ausprobieren. Vielen Dank so viel – Alex

+3

Es funktioniert! Aber ich weiß nicht warum. Könntest du es bitte erklären? –

+0

Vielen Dank. Hätte nie geglaubt, dass dies der Grund für das Problem sein könnte, und warteten eifrig auf die Erklärung. – Syn3sthete

1

Ich hatte dieses Problem auch. Ich kann bestätigen, dass das Hinzufügen von getItemId und getItemViewType zum Adapter die Duplizierung von Bildern in Recyclerview-Elementen beim Scrollen löst.

@Override 
public long getItemId(int position) { 
    return position; 
} 

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

Ich hätte dies als Kommentar hinterlassen, aber ich habe weniger als 5 Feedback.