Android Custom Listview以编程方式单击项目

时间:2015-02-25 13:23:04

标签: android android-listview

我正在使用自定义单选列表视图。我想以编程方式调用ItemOnClickListener。我在我的xml中使用android:listSelector="#47D149"属性。我看到了this postperformItemClick函数正在运行,但不会更改列表项的背景。

我该怎么办? 有什么建议。谢谢!

3 个答案:

答案 0 :(得分:1)

<强>样品

的ListView

<ListView
            android:id="@+id/navigation_menu_container"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_below="@id/rlBanner"
            android:layout_gravity="start"
            android:background="@color/nyc_black"
            android:choiceMode="singleChoice"
            android:divider="@color/border_black"
            android:dividerHeight="@dimen/divider_height"
            android:listSelector="@drawable/item_selector" >

可绘/ item_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_activated="false" android:drawable="@drawable/item_normal"></item>
    <item android:state_pressed="true" android:drawable="@drawable/item_pressed"></item>
    <item android:state_activated="true" android:drawable="@drawable/item_pressed"></item>
</selector>

可绘/ item_normal.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
     >
    <solid 
        android:color="@color/black"
        />
</shape>

可绘/ item_pressed.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
     >
     <solid 
         android:color="@color/red"
         />
</shape>

尝试上面的示例。 :)告诉我它是否有效

答案 1 :(得分:0)

也许您可以在适配器本身中处理它?<​​/ p>

@Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        if (null == convertView) {
            LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_item, parent, false);
        }

        if(this.getItem(position).isChecked()){
            convertView.setBackgroundResource(android.R.color.holo_blue_bright);
        }else{
            convertView.setBackgroundResource(android.R.color.white);
        }

        final View tempFinalView = convertView;
        tempFinalView.setOnClickListener(new View.OnClickListener() {
                                                              @Override
                                                              public void onClick(View v) {
                                                                  if (getItem(position).isChecked()) {
                                                                      getItem(position).setChecked(false);
                                                                      tempFinalView.setBackgroundResource(R.color.white);
                                                                  } else {
                                                                      getItem(position).setChecked(true);
                                                                      tempFinalView.setBackgroundResource(R.color.pressed);
                                                                  }
                                                              }
                                                          }
        );

   return convertView;
}

修改: 如果您不想在项目中添加其他变量。您可以使用标记功能。

只需设置获取标记。

private static int VIEW_TAG = 99;

@覆盖     public View getView(final int position,View convertView,final ViewGroup parent){         if(null == convertView){             LayoutInflater inflater =(LayoutInflater)_context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);             convertView = inflater.inflate(R.layout.list_item,parent,false);         }

    if((Boolean)convertView.getTag(VIEW_TAG)){
        convertView.setBackgroundResource(android.R.color.holo_blue_bright);
    }else{
        convertView.setBackgroundResource(android.R.color.white);
    }

    final View tempFinalView = convertView;
    tempFinalView.setOnClickListener(new View.OnClickListener() {
                                                          @Override
                                                          public void onClick(View v) {
                                                              if (getItem(position).isChecked()) {
                                                                  convertView.setTag(VIEW_TAG,new Boolean(false));
                                                                  tempFinalView.setBackgroundResource(R.color.white);
                                                              } else {
                                                                  convertView.setTag(VIEW_TAG,new Boolean(true));
                                                                  tempFinalView.setBackgroundResource(R.color.pressed);
                                                              }
                                                          }
                                                      }
    );

return convertView; }

答案 2 :(得分:0)

确定。我找到了解决方案。 :) @Shifar Shifz回答,不要使用

android:listSelector="@drawable/item_selector"

而不是将此drawable xml添加到row.xml布局中作为背景

android:background="@drawable/item_selector"