Android 如何在单击时为listview项目设置背景并在其他单击时动态更改

Android 如何在单击时为listview项目设置背景并在其他单击时动态更改,android,listview,Android,Listview,您好,我有一个列表视图,单击我想为所选列表项显示背景图像,如果选择被更改,则更改背景。我一直试图实现这一点,但未能做到。请帮我做这件事,我将非常感激 请找到我正在使用的代码 @Override public View getView(final int position, View convertView, ViewGroup parent) { View view = convertView; final int getstar

您好,我有一个列表视图,单击我想为所选列表项显示背景图像,如果选择被更改,则更改背景。我一直试图实现这一点,但未能做到。请帮我做这件事,我将非常感激

请找到我正在使用的代码

 @Override
    public View getView(final int position, View convertView,
            ViewGroup parent) {
        View view = convertView;
        final int getstartedItemPos = position;
        Resources res = getResources();

        if (convertView == null) {
            view = LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.getting_starteditem, null);
        }
        synchronized (view) {
            TextView textTopic = (TextView) view
                    .findViewById(R.id.indexItems);
            textTopic.setText(getStartedItems[getstartedItemPos]);
            textTopic.setTypeface(tf);
            view.setBackgroundColor(Color.TRANSPARENT);

        }
        return view;
    }

};

private OnItemClickListener getStartedListItem = new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position,
            long arg3) {
        // TODO Auto-generated method stub
         Intent myintent = new  Intent(getApplicationContext(),GetStartedWebview.class);
         myintent.putExtra("SelectedItem", getStartedItems[position]);
         startActivity(myintent);
    }
};
@覆盖
公共视图getView(最终整型位置,视图转换视图,
视图组(父级){
视图=转换视图;
最终int getstartedItemPos=位置;
Resources res=getResources();
if(convertView==null){
view=LayoutInflater.from(parent.getContext()).flate(
R.layout.get_starteditem,空);
}
已同步(视图){
TextView textTopic=(TextView)视图
.findviewbyd(R.id.indexItems);
setText(getStartedItems[getstartedItemPos]);
textTopic.setTypeface(tf);
视图.setBackgroundColor(颜色.透明);
}
返回视图;
}
};
私有OnItemClickListener getStartedListItem=新建OnItemClickListener(){
@凌驾
公共单击(适配器视图arg0,视图arg1,内部位置,
长arg3){
//TODO自动生成的方法存根
Intent myintent=newintent(getApplicationContext(),GetStartedWebview.class);
myintent.putExtra(“SelectedItem”,getStartedItems[position]);
星触觉(myintent);
}
};

如果您已将
OnItemClickListener
附加到ListView,我无法从您的代码中看到,如果您没有,则应该这样做。在
onItemClick
中,您可以抓取单击的视图(arg1),并在其上使用
.setBackgroundResource(int resID)
更改其背景

编辑:您可以使用一个局部变量来保存显示背景的当前列表项,因此执行以下操作:

private View pressedView = null; 

private OnItemClickListener getStartedListItem = new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position,
            long arg3) {
        // TODO Auto-generated method stub
         Intent myintent = new  Intent(getApplicationContext(),GetStartedWebview.class);
         myintent.putExtra("SelectedItem", getStartedItems[position]);
         startActivity(myintent);

         if(pressedView != null) {
             pressedView.setBackgroundResource(..); // reset background of old item
             pressedView = arg1; // Point pressedView to new item
         }
    }
};
private View按View=null;
私有OnItemClickListener getStartedListItem=新建OnItemClickListener(){
@凌驾
公共单击(适配器视图arg0,视图arg1,内部位置,
长arg3){
//TODO自动生成的方法存根
Intent myintent=newintent(getApplicationContext(),GetStartedWebview.class);
myintent.putExtra(“SelectedItem”,getStartedItems[position]);
星触觉(myintent);
如果(按View!=空){
按EdView.setBackgroundResource(..);//重置旧项目的背景
pressedView=arg1;//将pressedView指向新项目
}
}
};

如果您已将
OnItemClickListener
附加到ListView,我无法从您的代码中看到,如果您没有,则应该这样做。在
onItemClick
中,您可以抓取单击的视图(arg1),并在其上使用
.setBackgroundResource(int resID)
更改其背景

编辑:您可以使用一个局部变量来保存显示背景的当前列表项,因此执行以下操作:

private View pressedView = null; 

private OnItemClickListener getStartedListItem = new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position,
            long arg3) {
        // TODO Auto-generated method stub
         Intent myintent = new  Intent(getApplicationContext(),GetStartedWebview.class);
         myintent.putExtra("SelectedItem", getStartedItems[position]);
         startActivity(myintent);

         if(pressedView != null) {
             pressedView.setBackgroundResource(..); // reset background of old item
             pressedView = arg1; // Point pressedView to new item
         }
    }
};
private View按View=null;
私有OnItemClickListener getStartedListItem=新建OnItemClickListener(){
@凌驾
公共单击(适配器视图arg0,视图arg1,内部位置,
长arg3){
//TODO自动生成的方法存根
Intent myintent=newintent(getApplicationContext(),GetStartedWebview.class);
myintent.putExtra(“SelectedItem”,getStartedItems[position]);
星触觉(myintent);
如果(按View!=空){
按EdView.setBackgroundResource(..);//重置旧项目的背景
pressedView=arg1;//将pressedView指向新项目
}
}
};

PinkCand使用此,
查看lastView

public void onItemClick(AdapterView<?> arg0, View v, int pos, long arg3) 
    {


        if(lastView==null){
            v.setBackgroundColor(Color.BLACK);

            lastView=v;
            }
            else{
            lastView.setBackgroundColor(Color.WHITE); 
             v.setBackgroundColor(Color.BLACK);

            lastView=v;
            }

    }
public-void-onItemClick(适配器视图arg0,视图v,内部位置,长arg3)
{
if(lastView==null){
v、 setBackgroundColor(颜色:黑色);
lastView=v;
}
否则{
lastView.setBackgroundColor(颜色:白色);
v、 setBackgroundColor(颜色:黑色);
lastView=v;
}
}

它可能会帮助您

点击并使用此,
查看lastView

public void onItemClick(AdapterView<?> arg0, View v, int pos, long arg3) 
    {


        if(lastView==null){
            v.setBackgroundColor(Color.BLACK);

            lastView=v;
            }
            else{
            lastView.setBackgroundColor(Color.WHITE); 
             v.setBackgroundColor(Color.BLACK);

            lastView=v;
            }

    }
public-void-onItemClick(适配器视图arg0,视图v,内部位置,长arg3)
{
if(lastView==null){
v、 setBackgroundColor(颜色:黑色);
lastView=v;
}
否则{
lastView.setBackgroundColor(颜色:白色);
v、 setBackgroundColor(颜色:黑色);
lastView=v;
}
}

它可能会对您有所帮助

我不确定,但如果我理解正确,您只需设置视图的背景一次,但它应该是可选择绘制的,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:state_pressed="true"
        android:drawable="*background for pressed state*" />
    <item
        android:state_focused="true"
        android:drawable="*background for focused state*" />
    <item
        android:drawable="*default background*" />

</selector>

我不确定,但如果我理解正确,您只需设置视图的背景一次,但它应该是可选择绘制的,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:state_pressed="true"
        android:drawable="*background for pressed state*" />
    <item
        android:state_focused="true"
        android:drawable="*background for focused state*" />
    <item
        android:drawable="*default background*" />

</selector>

在listview中尝试以下操作:-

android:listSelector="@color/orange"
在listview中尝试以下操作:-

android:listSelector="@color/orange"

谢谢soren,我这么做了,但问题是如果我点击其他项目,背景不会改变,背景设置为所有点击的项目,而应该只有一个项目。我按照你说的方法做了,但仍然不起作用,背景不会改变。。。请帮助谢谢soren,我这么做了,但问题是如果我单击其他项目,背景不会改变,背景设置为所有单击的项目,而应该只有一个项目。我按照你所说的方法做了,但仍然不起作用,背景不会改变。。。请帮忙