我有一个ListView,它在相对布局中有一个图像和一个文本,在线性布局中有两个文本..我已经将图像设置为在ListView中单击时更改为文本..但问题是当滚动列表视图,其他图像也会更改为文本视图..
我在其他帖子中读到GetView上的setOnClickListener
错误,我们可以使用setOnItemClickListener
代替,但我不知道如何实现它?
请帮忙..
下面是我的Adapter类
public class WordAdapter extends ArrayAdapter<Word> {
/** Resource ID for the background color for this list of words */
private int mColorResourceId;
/**
* Create a new {@link WordAdapter} object.
*
* @param context is the current context (i.e. Activity) that the adapter is being created in.
* @param words is the list of {@link Word}s to be displayed.
* @param colorResourceId is the resource ID for the background color for this list of words
*/
public WordAdapter(Context context, ArrayList<Word> words, int colorResourceId) {
super(context, 0, words);
mColorResourceId = colorResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Check if an existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
// Get the {@link Word} object located at this position in the list
Word currentWord = getItem(position);
// Find the TextView in the list_item.xml layout with the ID miwok_text_view.
TextView miwokTextView = (TextView) listItemView.findViewById(R.id.miwok_text_view);
// Get the Miwok translation from the currentWord object and set this text on
// the Miwok TextView.
miwokTextView.setText(currentWord.getMiwokTranslationId());
// Find the TextView in the list_item.xml layout with the ID default_text_view.
TextView defaultTextView = (TextView) listItemView.findViewById(R.id.default_text_view);
// Get the default translation from the currentWord object and set this text on
// the default TextView.
defaultTextView.setText(currentWord.getDefaultTranslationId());
final TextView onClickTextView = (TextView) listItemView.findViewById(R.id.miwok_text_view_on_click);
onClickTextView.setText((currentWord.getTextOnClickId()));
// Find the ImageView in the list_item.xml layout with the ID image.
final ImageView imageView = (ImageView) listItemView.findViewById(R.id.image);
// Check if an image is provided for this word or not
if (currentWord.hasImage()) {
// If an image is available, display the provided image based on the resource ID
imageView.setImageResource(currentWord.getImageResourceId());
// Make sure the view is visible
imageView.setVisibility(View.VISIBLE);
} else {
// Otherwise hide the ImageView (set visibility to GONE)
imageView.setVisibility(View.GONE);
}
// Set the theme color for the list item
View textContainer = listItemView.findViewById(R.id.text_container);
// Find the color that the resource ID maps to
int color = ContextCompat.getColor(getContext(), mColorResourceId);
// Set the background color of the text container View
textContainer.setBackgroundColor(color);
// Return the whole list item layout (containing 2 TextViews) so that it can be shown in
// the ListView.
// ImageView imageView = (ImageView) listItemView.findViewById(R.id.list_item);
imageView.setTag(new Integer(position));
imageView.setOnClickListener(new ImageView.OnClickListener() {
Boolean flag =false;
@Override
public void onClick(View view) {
// Toast.makeText(getContext(), "ImageView clicked for the row = "+view.getTag().toString(), Toast.LENGTH_SHORT).show();
if(flag){
imageView.setAlpha(255);
onClickTextView.setVisibility(View.INVISIBLE);
flag = false;
}else{
imageView.setAlpha(0);
onClickTextView.setVisibility(View.VISIBLE);
flag =true;
}
}
});
return listItemView;
}
}
答案 0 :(得分:1)
由于View
在ListView
中被重复使用。单击项目后,视图项目中的图像将更改为文本,当向下滚动时重复使用相同的视图项目时,它将保留旧状态。这就是为什么你看到其他图像也改为文字的原因。
因此,为了解决这个问题,请在Word
类中创建一个变量,该变量确定视图项的状态(图像或文本)。我们称之为isImage
,这是一个布尔值
然后在getView
方法中,根据其当前值,初始化您的视图
if(currentWord.isImage){
imageView.setAlpha(255);
onClickTextView.setVisibility(View.INVISIBLE);
}else{
imageView.setAlpha(0);
onClickTextView.setVisibility(View.VISIBLE);
}
// add a tag to determine the position of the view, when the view is clicked.
imageView.setTag(position)
并在imageView中单击侦听器
public void onClick(View view) {
// Toast.makeText(getContext(), "ImageView clicked for the row = "+view.getTag().toString(), Toast.LENGTH_SHORT).show();
int position = (int) view.getTag()
Word word = getItem(position)
// using the new variable, do the required UI changes
if(word.isImage){
currentWord.isImage = false;
}else{
word.isImage = true;
}
notifyDataSetChanged();
}
我也在Word类中声明了isImage变量,如下所示:
public boolean isImage = true;
答案 1 :(得分:1)
尝试改变这个:
imageView.setTag(new Integer(position));
imageView.setOnClickListener(new ImageView.OnClickListener() {
Boolean flag =false;
@Override
public void onClick(View view) {
// Toast.makeText(getContext(), "ImageView clicked for the row = "+view.getTag().toString(), Toast.LENGTH_SHORT).show();
if(flag){
imageView.setAlpha(255);
onClickTextView.setVisibility(View.INVISIBLE);
flag = false;
}else{
imageView.setAlpha(0);
onClickTextView.setVisibility(View.VISIBLE);
flag =true;
}
}
});
为:
imageView.setOnClickListener(new ImageView.OnClickListener() {
@Override
public void onClick(View view) {
if(currentWord.isFlag){
currentWord.isFlag = false;
}else{
currentWord.isFlag = true;
}
notifyDataSetChanged();
}
});
if(currentWord.isFlag)
{
imageView.setAlpha(0);
onClickTextView.setVisibility(View.VISIBLE);
}
else
{
imageView.setAlpha(255);
onClickTextView.setVisibility(View.INVISIBLE);
}
在Word类中创建一个isFlag变量
public boolean isFlag = false;
并且无论何时初始化Word类型列表,都要设置setFlag(false)的值;
并且这个正在发生,因为listview在滚动期间总是获取刷新项目,因此您还必须在列表中使用标记