在Recycler中单击一个卡片视图会自动启用其他卡片视图

时间:2016-02-01 04:36:14

标签: android android-recyclerview recycler-adapter

我正在创建一个应用程序。当用户点击编辑按钮时,我想检查用户并启用编辑,如果同一用户是评论的所有者。

我可以检查用户。但我的问题是当用户点击一个评论的编辑按钮时,许多评论变得可编辑而不是一个。我没有得到如何解决这个问题。欢迎提出所有建议。

下面的

是我的适配器代码:

public class ToadlineCommentAdapter extends RecyclerView.Adapter<ToadlineCommentAdapter.MyViewHolder> {

    private ClickListener clickListener;
    private SwipeRefreshLayout.OnRefreshListener clickListener1;
    private LayoutInflater inflater;
    String newEnteredCommentText, neededCommentId, text;
    ArrayList<String> postCreatorId;
    ArrayList<String> postIdForComments;
    HttpURLConnection conn;
    String userId;
    int clickedPosition;
    Context mContext;
    ArrayList<String> commentId;
    List<CommentDataStore> data = Collections.EMPTY_LIST;
    private String userCredentials = "toadwaysIfocus:toadwaysIfocus";
    private String basicAuth = "Basic " + new String(Base64.encode(userCredentials.getBytes(), 0));


    public ToadlineCommentAdapter(Context context, List<CommentDataStore> data, ArrayList<String> commentId, ArrayList<String> postCreatorId, String userId, ArrayList<String> postIdForComments ) {
        inflater = LayoutInflater.from(context);
        this.data = data;
        this.commentId = commentId;
        mContext = context;
        this.postCreatorId = postCreatorId;
        this.userId = userId;
        this.postIdForComments = postIdForComments;


    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

 View view = inflater.inflate(R.layout.custom_comment_layout, parent, false);
  MyViewHolder viewHolder = new MyViewHolder(view);
   return viewHolder;
    }


    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        CommentDataStore current = data.get(position);

        Picasso.with(mContext)
                .load(current.commentProfileImageLink)
                .placeholder(R.mipmap.human_image)
                .into(holder.commentImage);
        holder.commentUserName.setText(current.commentUserName);
        holder.commentDays.setText(current.commentDays);
        holder.commentDescription.setText(current.commentDescription);
        holder.commentAgreeCount.setText(current.commentAgreeCount);
        holder.commentDisAgreeCount.setText(current.commentDisAgreeCount);


    }

    @Override
    public int getItemCount() {

        return data.size();
    }

    public void setClickListener(ClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public void setClickListener1(SwipeRefreshLayout.OnRefreshListener clickListener1) {
        this.clickListener1 = clickListener1;
    }


    // View Holder object for Recycler View
    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener {

        TextView commentUserName, commentDays, commentAgreeCount, commentDisAgreeCount;
        EditText commentDescription;
        ImageView commentImage;
        ImageButton commentEdit, commentDelete;
        Button commentEditSubmit;


        public MyViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener((View.OnClickListener) this);
commentImage = (ImageView) itemView.findViewById(R.id.imageViewUser);
commentUserName = (TextView) itemView.findViewById(R.id.textViewTitle);
commentDays = (TextView) itemView.findViewById(R.id.textViewNoOfDays);
commentDescription = (EditText) itemView.findViewById(R.id.textViewCommentDescription);
commentAgreeCount = (TextView) itemView.findViewById(R.id.textViewAgreeCount);
commentDisAgreeCount = (TextView) itemView.findViewById(R.id.textViewDisAgreeCount);
commentEdit = (ImageButton) itemView.findViewById(R.id.imageButtonCommentEdit);
commentDelete = (ImageButton) itemView.findViewById(R.id.imageButtonCommentDelete);
commentEditSubmit = (Button) itemView.findViewById(R.id.commentEditSubmit);

commentDescription.setEnabled(false);

commentEdit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
  int clickedPosition = getAdapterPosition();

  if (postIdForComments.get(clickedPosition).equals(userId)){

 commentDescription.setEnabled(true);
                        commentDescription.setBackgroundColor(mContext.getResources().getColor(R.color.colorHighlight));
 commentEditSubmit.setVisibility(View.VISIBLE);
  neededCommentId = commentId.get(clickedPosition);

  }
else {
Toast.makeText(mContext, mContext.getResources().getString(R.string.comment_not_editable), Toast.LENGTH_SHORT).show();
 return;
}
  }
    });

}

@Override
 public void onClick(View view) {

  if (clickListener != null) {
 clickListener.itemClicked(view, getPosition());
  }

}

@Override
  public void onRefresh() {
 onClick(itemView);

}

public interface ClickListener {

public void itemClicked(View view, int position);
    }
}

例如,如果我点击第5张卡片行中的编辑按钮,我只想让第5张卡片行进行编辑。目前编辑第10行或第14行的buon将自动选中。为什么会这样?

2 个答案:

答案 0 :(得分:1)

这些观点正在被回收。在onBindViewHolder()中,您需要将其重置为初始状态。

修改

RecyclerView回收视图。当其中一个孩子在屏幕上滚动得足够远时,将孩子分开并放在一个可循环/可循环播放的视图池中。当RecyclerView需要显示一个新子项时,它可以从该池中获取视图(而不是从头开始实例化)并简单地将新数据绑定到视图 - 换句话说,它不会调用{{1} },但会调用onCreateViewHolder()

单击编辑按钮时,可以更改ViewHolder中某些视图的状态。然后滚动直到它被回收,但它保持了之前的所有状态。您需要将这些视图返回到onBindViewHolder()内的初始状态以处理此问题。

答案 1 :(得分:0)

您应该使用SparseBooleanArray

查看此代码并尝试执行此操作。

SparseBooleanArray mArray;

现在定义它的大小。

mArray = new SparseBooleanArray(yourArray.size());

现在执行onClick

commentEdit.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {

         int clickedPosition = getAdapterPosition();
         if (postIdForComments.get(clickedPosition).equals(userId)){
            mArray.put(clickedPosition,true);
         }else {
             Toast.makeText(mContext, mContext.getResources().getString(R.string.comment_not_editable), Toast.LENGTH_SHORT).show();
             return;
         }
      }
});

最后在你的onBindViewHolder

if(mArray.get(position) == true){
      commentDescription.setEnabled(true);
      commentDescription.setBackgroundColor(mContext.getResources().getColor(R.color.colorHighlight));
      commentEditSubmit.setVisibility(View.VISIBLE);
      neededCommentId = commentId.get(clickedPosition);
}else if(mArray.get(position) = true)){
}

我希望这会对你有所帮助。

修改: 您应该在适配器中初始化SparseBooleanArray,而不是ViewHolder