Am working saving the current state of a check item while scrolling, and it worked, but i couldn't save the state of the TextView noting the count when the checkbox is checked or unchecked.
this is my Adapter,
NewsAdapter.java
public class TestNewsAdapter extends RealmBaseRecyclerViewAdapter<NewsTrend, TestNewsAdapter.PostsViewHolder> {
public RealmResults<NewsTrend> realmResults;
public Context context;
static String fbid;
private User user;
private RetrofitInterface restApi;
ArrayList<Boolean> positionArray;
public TestNewsAdapter(Context context, RealmResults<NewsTrend> realmResults,
boolean automaticUpdate, User user) {
super(context, realmResults, automaticUpdate);
this.realmResults = realmResults;
this.context = context;
this.user = user;
positionArray = new ArrayList<>(realmResults.size());
for(int i =0;i<realmResults.size();i++){
positionArray.add(false);
}
}
@Override
public PostsViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
fbid = user.getId();
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_news, viewGroup, false);
PostsViewHolder mediaViewHolder = new PostsViewHolder(v);
return mediaViewHolder;
}
@Override
public void onBindViewHolder(final PostsViewHolder holder, final int position) {
final NewsTrend postsData = getItem(position);
holder.itemView.setTag(postsData);
holder.tvNewsCountLike.setText(""+ postsData.getLike_count());
Glide.with(context)
.load(postsData.getImage())
.centerCrop()
.placeholder(R.drawable.tw_logo)
.into(holder.ivNewsImage);
if (postsData.getLike_status() == 1) {
holder.ivLike.setChecked(true);
} else {
holder.ivLike.setChecked(false);
}
holder.ivLike.setOnCheckStateChangeListener(new ShineButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(View view, boolean checked) {
if (checked) {
like(postsData.getNews_id());
holder.tvNewsCountLike.setText("" + (Integer.parseInt(holder.tvNewsCountLike.getText().toString()) + 1));
positionArray.set(position, true);
} else {
like(postsData.getNews_id());
holder.tvNewsCountLike.setText("" + (Integer.parseInt(holder.tvNewsCountLike.getText().toString()) - 1));
positionArray.set(position, false);
}
}
});
}
@Override
public NewsTrend getItem(int i) {
return realmResults.get(i);
}
public void swapData(RealmResults<NewsTrend> realmResults) {
this.realmResults = realmResults;
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public int getItemCount() {
return realmResults.size();
}
public static class PostsViewHolder extends RecyclerView.ViewHolder {
public ImageView ivFavorite;
public TextView tvNewsCountLike;
PostsViewHolder(View itemView) {
super(itemView);
ivLike = (ShineButton) itemView.findViewById(R.id.ivLike);
tvNewsCountLike = (TextView) itemView.findViewById(R.id.tvNewsCountLike);
ivLike.setOnCheckStateChangeListener(null);
}
}
}
Aucun commentaire:
Enregistrer un commentaire