Android ListView图像单击可在滚动时更改列表中的其他图像

Android ListView图像单击可在滚动时更改列表中的其他图像,android,listview,android-arrayadapter,onclicklistener,onitemclicklistener,Android,Listview,Android Arrayadapter,Onclicklistener,Onitemclicklistener,我有一个列表视图,在相对布局中有一个图像和一个文本,在线性布局中有两个文本。。我已将图像设置为在ListView中单击时变为文本。。但问题是当列表视图被滚动时,其他图像也会变为文本视图 我在其他帖子中读到,在GetView上使用setOnClickListener是错误的,我们可以使用setOnItemClickListener,但我不知道如何实现它? 请帮忙 下面是我的适配器类 public class WordAdapter extends ArrayAdapter<Word>

我有一个列表视图,在相对布局中有一个图像和一个文本,在线性布局中有两个文本。。我已将图像设置为在ListView中单击时变为文本。。但问题是当列表视图被滚动时,其他图像也会变为文本视图

我在其他帖子中读到,在GetView上使用
setOnClickListener
是错误的,我们可以使用
setOnItemClickListener
,但我不知道如何实现它? 请帮忙

下面是我的适配器类

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;
}
}
公共类WordAdapter扩展了ArrayAdapter{
/**此单词列表背景颜色的资源ID*/
私有int mColorResourceId;
/**
*创建一个新的{@link WordAdapter}对象。
*
*@param context是在其中创建适配器的当前上下文(即活动)。
*@param words是要显示的{@link Word}列表。
*@param colorResourceId是此单词列表背景颜色的资源ID
*/
publicWordAdapter(上下文上下文、ArrayList单词、int-colorResourceId){
超级(上下文,0,单词);
mColorResourceId=colorResourceId;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//检查是否正在重用现有视图,否则会膨胀视图
View listItemView=convertView;
如果(listItemView==null){
listItemView=LayoutFlater.from(getContext()).inflate(
R.layout.list_项,父项,false);
}
//获取位于列表中此位置的{@link Word}对象
Word currentWord=getItem(位置);
//在list_item.xml布局中查找ID为miwok_text_视图的文本视图。
TextView miwokTextView=(TextView)listItemView.findViewById(R.id.miwok\u text\u视图);
//从currentWord对象获取Miwok翻译,并将此文本设置为打开
//Miwok文本视图。
setText(currentWord.getMiwoktTranslationId());
//在list_item.xml布局中查找ID为default_text_视图的文本视图。
TextView defaultTextView=(TextView)listItemView.findViewById(R.id.default\u text\u视图);
//从currentWord对象获取默认翻译并将此文本设置为打开
//默认的文本视图。
defaultTextView.setText(currentWord.getDefaultTranslationId());
final TextView onClickTextView=(TextView)listItemView.findViewById(R.id.miwok\u text\u单击时查看);
onClickTextView.setText((currentWord.GetTextConclicKid());
//在list_item.xml布局中找到具有ID图像的ImageView。
最终ImageView ImageView=(ImageView)listItemView.findViewById(R.id.image);
//检查是否为此单词提供了图像
if(currentWord.hasImage()){
//如果图像可用,则根据资源ID显示提供的图像
setImageResource(currentWord.getImageResourceId());
//确保视图可见
设置可见性(View.VISIBLE);
}否则{
//否则隐藏ImageView(将可见性设置为GONE)
设置可见性(View.GONE);
}
//设置列表项的主题颜色
View textContainer=listItemView.findViewById(R.id.text\u容器);
//查找资源ID映射到的颜色
int color=ContextCompat.getColor(getContext(),mColorResourceId);
//设置文本容器视图的背景色
textContainer.setBackgroundColor(颜色);
//返回整个列表项布局(包含2个文本视图),以便在
//列表视图。
//ImageView ImageView=(ImageView)listItemView.findViewById(R.id.list_项);
setTag(新的整数(位置));
imageView.setOnClickListener(新的imageView.OnClickListener(){
布尔标志=假;
@凌驾
公共void onClick(视图){
//Toast.makeText(getContext(),“ImageView已单击行=“+view.getTag().toString(),Toast.LENGTH_SHORT).show();
国际单项体育联合会(旗){
imageView.setAlpha(255);
onClickTextView.setVisibility(View.INVISIBLE);
flag=false;
}否则{
imageView.setAlpha(0);
onClickTextView.setVisibility(View.VISIBLE);
flag=true;
}
}
});
返回listItemView;
}
}

因为
视图
列表视图
中得到重用。单击某个项目后,视图项目中的图像将更改为文本,当向下滚动时重复使用同一视图项目时,它将保留旧状态。这就是为什么你会看到其他图像也变为文本

为了解决这个问题,在
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中单击listener

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;
尝试更改此选项:

致:

在Word类中创建isFlag变量
public boolean isFlag=false

无论何时初始化单词类型列表,都要设置setFlag的值(false)


这种情况发生在您身上,因为listview总是在滚动过程中获取刷新项,所以您必须在列表中也使用flag

我在
Word=getItem(Word)
行中得到一个错误。变量Word可能没有初始化。抱歉,应该是
getItem(position)
,我已经编辑了答案,在这一行得到警告
Word=getItem(position)。。。变量字从不使用@RickIt这不是一个完整的代码,我刚刚给出了方向。基于您可能创建的“isImage”变量。将变量设置为false,并使文本可见,将图像alpha设置为false
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);
    }