在滚动Recyclerview上,更改消失了
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在滚动Recyclerview上,更改消失了相关的知识,希望对你有一定的参考价值。
我创建了一个词汇安卓应用程序,我们可以通过单击单词附近的星形图标将文字添加到收藏夹。单击星形图标后,它将变为实心星形图标但如果我们滚动RecyclerView
而不是实心星形图标将再次更改为正常的星形图标。
Image before scrolling:
Image after scrolling:
Here is the code:
public class WordListAdapter extends RecyclerView.Adapter<WordListViewHolder> {
TextToSpeech ttsObject;
private List<WordListModel> mWordList;
private Context context;
private Activity activity;
private int result;
public WordListAdapter(TextToSpeech ttsObject, int result) {
this.ttsObject = ttsObject;
this.result = result;
}
public WordListAdapter(List<WordListModel> mWordList, Context context, TextToSpeech ttsObject, int result) {
this.mWordList = mWordList;
this.context = context;
activity = (Activity)context;
this.result = result;
this.ttsObject = ttsObject;
}
@Override
public WordListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.word_list_layout,parent,false);
return new WordListViewHolder(view);
}
@Override
public void onBindViewHolder(final WordListViewHolder holder, final int position) {
final WordListModel wordmeaning = mWordList.get(position);
holder.mNameTv.setText(wordmeaning.getmWord());
holder.mNameTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (result == TextToSpeech.LANG_NOT_SUPPORTED || result == TextToSpeech.LANG_MISSING_DATA){
Toast.makeText(activity,"Feature Not Support in your Device",Toast.LENGTH_LONG).show();
}else {
ttsObject.speak(wordmeaning.getmWord(), TextToSpeech.QUEUE_FLUSH, null);
}
}
});
holder.mMeaningTv.setText(wordmeaning.getmMeaning());
holder.mMeaningTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (result == TextToSpeech.LANG_NOT_SUPPORTED || result == TextToSpeech.LANG_MISSING_DATA){
Toast.makeText(activity,"Feature Not Support in your Device",Toast.LENGTH_LONG).show();
}else {
ttsObject.speak(wordmeaning.getmMeaning(), TextToSpeech.QUEUE_FLUSH, null);
}
}
});
holder.mFavIv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DatabaseHelper mydb = new DatabaseHelper(context);
try {
if (wordmeaning.getIsFav().equals("false")){
boolean check = mydb.updateData("true",wordmeaning.getmWord(),wordmeaning.getDbTable());
if (check){
holder.mFavIv.setImageResource(R.drawable.ic_star_black_24dp);
}
Log.d("TAG",Boolean.toString(check));
}else {
boolean check = mydb.updateData("false",wordmeaning.getmWord(),wordmeaning.getDbTable());
if (check){
holder.mFavIv.setImageResource(R.drawable.ic_star_border_black_24dp);
}
Log.d("TAG",Boolean.toString(check));
}
}catch (Exception e){
Log.d("TAG",e.toString());
}
}
});
if (wordmeaning.getIsFav().equals("false")){
holder.mFavIv.setImageResource(R.drawable.ic_star_border_black_24dp);
}else {
holder.mFavIv.setImageResource(R.drawable.ic_star_black_24dp);
}
}
@Override
public int getItemCount() {
return mWordList.size();
}
答案
滚动时,视图会被回收。这里你刚刚更新了db中的值但没有在列表中更新,所以你也想要更新arrayList中的值,因为你想跟踪项目所以改变你的mFavIv Onclick
方法的代码,如下所示
holder.mFavIv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DatabaseHelper mydb = new DatabaseHelper(context);
try {
if (wordmeaning.getIsFav().equals("false")){
boolean check = mydb.updateData("true",wordmeaning.getmWord(),wordmeaning.getDbTable());
if (check){
holder.mFavIv.setImageResource(R.drawable.ic_star_black_24dp);
wordmeaning.setIsFav("true"); // updted your arryListvalue
}
Log.d("TAG",Boolean.toString(check));
}else {
boolean check = mydb.updateData("false",wordmeaning.getmWord(),wordmeaning.getDbTable());
if (check){
holder.mFavIv.setImageResource(R.drawable.ic_star_border_black_24dp);
wordmeaning.setIsFav("false"); // updted your arryListvalue
}
Log.d("TAG",Boolean.toString(check));
}
}catch (Exception e){
Log.d("TAG",e.toString());
}
}
});
以上是关于在滚动Recyclerview上,更改消失了的主要内容,如果未能解决你的问题,请参考以下文章
使用 Glide 和 RecyclerView 上下滚动时图像消失,图像之间的空间增加
焦点更改时强制 RecyclerView 滚动(Android TV)