gpt4 book ai didi

android - 在 Recycler View 中保持项目状态

转载 作者:行者123 更新时间:2023-12-04 23:06:44 25 4
gpt4 key购买 nike

我有一个从 API 填充数据的 recyclerview。每行都有一个点赞按钮,用户可以按下该按钮并更改图标的颜色。

当用户按下按钮时,它工作正常,但当他向上滚动页面然后返回到喜欢的行时,点赞消失了,看起来以前从未按下过。

如何保持这种“喜欢”的状态?

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

...

public SocialAdapter(Context context, JSONArray pages){
mContext = context;
mPages = pages;
...
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

View view = LayoutInflater.from(mContext).inflate(R.layout.social_row, parent, false);
MyHolder holder=new MyHolder(view);
return holder;
}

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
MyHolder myHolder= (MyHolder) holder;

try {
JSONObject mpackid = mPages.getJSONObject(position);

//myHolder.button_like.setText("Eu quero!");
myHolder.button_like.setEnabled(true);
myHolder.button_like.setBackground(mContext.getResources().getDrawable(R.drawable.empty_star));
myHolder.number_likes.setText(mpackid.optJSONObject("info").optString("likes")+" foo");
myHolder.number_likes.setTextColor(Color.parseColor("#525252"));

myHolder.user_name.setText(mpackid.optString("user_full_name"));
myHolder.user_text.setText(mpackid.optJSONObject("info").optString("text"));
myHolder.feed_id.setText(mpackid.optJSONObject("info").optString("feed_suggestion_id"));
myHolder.number_likes.setText(mpackid.optJSONObject("info").optString("likes")+" foo");
myHolder.like_int.setText(mpackid.optJSONObject("info").optString("likes"));

myHolder.tags.setText("#"+mpackid.optJSONObject("info").optJSONArray("tags").toString().replace("[", "").replace("]", "").replace("\"",""));
myHolder.date.setText(mpackid.optJSONObject("info").optString("elapsed_time"));

String likes = mpackid.optJSONObject("info").optJSONArray("liked_by").toString().replace("[", "").replace("]", "").replace("\"","");

if(likes.contains(mUserId)){
myHolder.button_like.setEnabled(false);
myHolder.button_like.setBackground(mContext.getResources().getDrawable(R.drawable.filled_star));
myHolder.number_likes.setText("foo "+mpackid.optJSONObject("info").optString("likes")+" foo");
myHolder.number_likes.setTextColor(Color.parseColor("#525252"));
}

Glide.with(mContext).load(mpackid.optString("user_avatar_url"))
.asBitmap()
.thumbnail(Glide.with(mContext).load(R.drawable.ajax_loader).asBitmap())
.fitCenter()
.into(myHolder.user_image);

} catch (JSONException e) {
e.printStackTrace();
}
}

@Override
public long getItemId(int position) {
return 0;// not using this
}

@Override
public int getItemCount() {
return mPages.length();
}


private static class ViewHolder{
ImageView icon;
TextView itemName;
TextView url;
}
class MyHolder extends RecyclerView.ViewHolder{

CircleImageView user_image;
TextView user_name;
TextView user_text;
TextView feed_id;
TextView number_likes;
TextView like_int;
TextView date;
TextView tags;
ImageButton button_like;

// create constructor to get widget reference
public MyHolder(View itemView) {
super(itemView);
user_image = (CircleImageView) itemView.findViewById(R.id.user_image);
user_name = (TextView) itemView.findViewById(R.id.user_name);
user_text = (TextView) itemView.findViewById(R.id.headline);
number_likes = (TextView) itemView.findViewById(R.id.number_likes);
feed_id = (TextView) itemView.findViewById(R.id._id);
like_int = (TextView) itemView.findViewById(R.id.likes_nb);
button_like = (ImageButton) itemView.findViewById(R.id.button5);
date = (TextView) itemView.findViewById(R.id.date);
tags = (TextView) itemView.findViewById(R.id.feed_tag);
}

}

public JSONObject getItem(int position) {
try {
return mPages.getJSONObject(position);
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}
public void addMoreData(JSONArray loadMoreArray) throws JSONException {
//Seta data pra LoadArray
mPages.length();
this.mPages = mPages.put(loadMoreArray.getJSONObject(0));
notifyItemRangeInserted(mPages.length(), mPages.length()+1);
SmoothRecyclerView recyclerView = (SmoothRecyclerView) ((Activity) this.mContext).findViewById(R.id.chat_social);
recyclerView.scrollToPosition(mPages.length()-1);

}

最佳答案

你必须在 onBindViewHolder 中使用 if else。

if(likes.contains(mUserId)){
myHolder.button_like.setEnabled(false);
myHolder.button_like.setBackground(mContext.getResources().getDrawable(R.drawable.filled_star));
myHolder.number_likes.setText("foo "+mpackid.optJSONObject("info").optString("likes")+" foo");
myHolder.number_likes.setTextColor(Color.parseColor("#525252"));
}
else{
//behaviour when like.contains(userId) is false.
}

这是因为 view holder 会在用户滚动时回收前一个项目的 View ,所以你必须在这两种情况下都使用 if else,这样 view holder 才会按预期运行。

关于android - 在 Recycler View 中保持项目状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45624120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com