0

以下是我的源代码,

我也试过了isPressedisClicked但是还是不行。

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 上注册一个 onItemClickListener ......但如果我这样做,似乎会使 getView 方法毫无意义。我在这个问题上很挣扎。

4

4 回答 4

0

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

可能你需要的是使用convertView.setOnFocusChangeListener

于 2012-11-15T04:02:52.903 回答
0

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

我相信你想要一个Color State Selector


(来自上面的链接)XML 文件保存在res/color/button_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" />

或者您可以对 ListViews、GridViews 等使用类似的方法和ListView 状态选择器。

于 2012-11-15T04:10:55.110 回答
0

我假设您希望仅当您将手指放在行上时才更改背景颜色并在您松开时更改回来?为此,只需将 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;
    }
);
于 2012-11-15T06:49:28.827 回答
0

首先,我很感激 Sam 给了我这么有用的提示。以下是我的解决程序。

编写一个选择器文件,下面是它的内容。然后将此 list_background.xml 放入可绘制文件夹

<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 中,使用这样的 list_background :

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

... android:background="@drawable/list_background" ... >

就是这样。源代码中没有调用点击监听器或其他方法。

于 2012-11-15T10:53:44.840 回答