我有这样的清单项目
当我单击此列表项的锁定图像时,该图像将转换为该图像
它正在工作,并且按预期方式更改了当前列表项的图像,但是问题在于,这也是由于回收列表视图而更改了其他行的图像。https://stackoverflow.com/a/14108676/5989049 所以我能做些什么,使该项目在图像上单击仅影响被单击的行。
这是我的适配器代码
public class WordAdapter extends ArrayAdapter<Word> {
public WordAdapter(Activity context, ArrayList<Word> words) {
super(context, 0, words);
}
@Override
public View getView(final int position, @Nullable final View convertView, @NonNull final ViewGroup parent) {
View listItem = convertView;
if (listItem == null) {
listItem = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
final Word word = getItem(position);
String englishWord = null;
if (word != null) {
englishWord = word.getEnglishWord();
}
String arabicWord = null;
if (word != null) {
arabicWord = word.getArabicWord();
}
TextView englishText = listItem.findViewById(R.id.english_text_view);
TextView arabicText = listItem.findViewById(R.id.arabic_text_view);
ImageView mUnLocked = listItem.findViewById(R.id.unlock);
mUnLocked.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!word.isLocked()) {
word.setLocked(true);
ImageView icon = (ImageView) view;
icon.setImageResource(R.drawable.ic_lock_outline_black_24dp);
icon.setVisibility(View.VISIBLE);
} else {
ImageView icon = (ImageView) view;
icon.setImageResource(R.drawable.ic_lock_open_black_24dp);
icon.setVisibility(View.VISIBLE);
}
}
});
Typeface enTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/fontfamily.ttf");
Typeface arTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/ShahdRegular.ttf");
englishText.setTypeface(enTypeface);
arabicText.setTypeface(arTypeface);
englishText.setText(englishWord);
arabicText.setText(arabicWord);
return listItem;
}
}
答案 0 :(得分:0)
您将需要在列表适配器模型中保留标志“ isLocked”。 例如,在适配器中,您应该有一个数据列表,比方说A类。
在A类中添加一个布尔isLocked。
现在在列表中,在onClick侦听器中切换此标志的值,然后根据该标志选择正确的图像资源。
编辑:基于您的适配器:
public class WordAdapter extends ArrayAdapter<Word> {
public WordAdapter(Activity context, ArrayList<Word> words) {
super(context, 0, words);
}
@Override
public View getView(final int position, @Nullable final View convertView, @NonNull final ViewGroup parent) {
View listItem = convertView;
if (listItem == null) {
listItem = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
final Word word = getItem(position);
String englishWord = null;
if (word != null) {
englishWord = word.getEnglishWord();
}
String arabicWord = null;
if (word != null) {
arabicWord = word.getArabicWord();
}
TextView englishText = listItem.findViewById(R.id.english_text_view);
TextView arabicText = listItem.findViewById(R.id.arabic_text_view);
ImageView mUnLocked = listItem.findViewById(R.id.unlock);
if (!word.isLocked()) {
word.setLocked(true);
ImageView icon = (ImageView) view;
icon.setImageResource(R.drawable.ic_lock_outline_black_24dp);
icon.setVisibility(View.VISIBLE);
} else {
ImageView icon = (ImageView) view;
icon.setImageResource(R.drawable.ic_lock_open_black_24dp);
icon.setVisibility(View.VISIBLE);
}
mUnLocked.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!word.isLocked()) {
word.setLocked(true);
ImageView icon = (ImageView) view;
icon.setImageResource(R.drawable.ic_lock_outline_black_24dp);
icon.setVisibility(View.VISIBLE);
} else {
ImageView icon = (ImageView) view;
icon.setImageResource(R.drawable.ic_lock_open_black_24dp);
icon.setVisibility(View.VISIBLE);
}
}
});
Typeface enTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/fontfamily.ttf");
Typeface arTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/ShahdRegular.ttf");
englishText.setTypeface(enTypeface);
arabicText.setTypeface(arTypeface);
englishText.setText(englishWord);
arabicText.setText(arabicWord);
return listItem;
}
}