Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 当我单击一个listview项目并更改其背景时,为什么它不工作?_Android_Android Listview - Fatal编程技术网

Android 当我单击一个listview项目并更改其背景时,为什么它不工作?

Android 当我单击一个listview项目并更改其背景时,为什么它不工作?,android,android-listview,Android,Android Listview,下面是我的源代码 我也试过了被按下,被点击,但还是不起作用 public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { // This a new view we inflate the new layout LayoutInflater inflater = (LayoutInflater) context.getSy

下面是我的源代码

我也试过了
被按下
被点击
,但还是不起作用

public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        // This a new view we inflate the new layout
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.my_item_layout, parent, false);
    }
    MyItem myItem = getItem(position);


    TextView direction = (TextView) convertView.findViewById(R.id.direction);
    direction.setText(myItem .getDirection());

    if(convertView.isSelected()){
        convertView.setBackgroundResource(R.drawable.list_select_bar);
        setTextColor(convertView, textIDs , R.color.white);
    }else{
        convertView.setBackgroundResource(R.color.light_white);
        setTextColor(convertView, textIDs , R.color.black);
    }

    return convertView;
}

实际上,如果我删除convertView检查块,我只需要在listview上注册一个监听器。。。但如果我这样做,似乎会使getView方法变得毫无意义。我很难解决这个问题。

getView
仅在呈现列表视图时调用。当选择或单击某个项目时,不会调用它

您可能需要使用
convertView.setOnFocusChangeListener

getView()
创建(或重新创建)一个不可见的视图。因此,无法选择、按下或单击这样的新视图

我相信你想要一份工作


(从上面的链接) XML文件保存在
res/color/button\u text.XML

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:color="#ffff0000"/> <!-- pressed -->
    <item android:state_focused="true"
          android:color="#ff0000ff"/> <!-- focused -->
    <item android:color="#ff000000"/> <!-- default -->
</selector>

此布局XML将颜色列表应用于视图:

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/button_text"
    android:textColor="@color/button_text" />


或者,您也可以使用类似的方法处理ListView、GridView等。我假设您只希望在手指放在行上时更改背景颜色,然后在放手时再更改背景颜色?为此,只需将OnTouchListener添加到行项目中

convertView.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            convertView.setBackgroundResource(R.drawable.list_select_bar);
        } else if (event.getAction() == MotionEvent.ACTION_UP 
                || event.getAction() == MotionEvent.ACTION_CANCEL) {
            convertView.setBackgroundResource(R.color.light_white);
        }
        return false;
    }
);

首先,我很感激山姆给了我这么有用的建议。 以下是我的解决程序

写一个选择器文件,下面是它的内容。然后将这个list_background.xml放入drawable文件夹

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/list_select_bar" android:state_pressed="true"/><!--pressed -->
<item android:drawable="@color/light_white"/><!-- default --></selector>

然后在项目布局xml中,使用如下列表背景:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"


就这样。没有click listener或在源代码中调用的其他方法。

什么是setTextColor方法,您是否尝试调用invalidate方法?我尝试了“convertView.setOnClickListener”,但我没有单击的其他项也会更改其背景。一个规则的模式出现了。例如:索引0、9、18中的项目也受到影响。这是因为您正在重新使用convertView,只需删除代码
convertView==null
,并为每个convertView充气即可。您好,您的建议奏效了。。。但是我现在无法滚动我的listview。抱歉,我忘记添加返回值(我已经编辑了答案)。你返回的是什么,对还是错?如果返回true,OnTouchListener将使用事件,这可能会阻止列表视图滚动。只需返回false,触摸事件将被传递。我添加了return false,正如您的回答一样。Listview仍然是粘滞的,我想有什么东西锁定了Listview。实际上,我现在可以更改背景颜色,但我也很难理解为什么其他项目也会受到影响。一个规则的模式出现了。例如:索引0、9、18中的项目也受到影响,甚至我只单击了索引1。这是因为convertView被重用,而不是为同一视图分配n个实例。如果您试图选择行(而不是只在触摸时突出显示),请尝试在模型上记录行(例如,“MyItem”),并在getView()中检查行并进行适当设置,否则您将获得刚才描述的行为。