0

ListView通常当我在简单的 ArrayAdapter 中创建一个。它无需额外设置即可自动获得此突出显示功能。但是,当我ListView使用自定义创建此功能时,CursorAdapter此功能似乎丢失了,我找不到解决方法。

这是我的自定义 CursorAdapter public class RecentCallAdapter extends CursorAdapter {

    private final String tag = this.getClass().getSimpleName();

    public RecentCallAdapter(Context context, Cursor c, int flags) {
        super(context, c, flags);
        // TODO Auto-generated constructor stub
    }

    private static class ViewHolder {
        TextView name;
        TextView date;
        int nameCol;
        int dateCol;
        int numberCol;
        Calendar cal;
    }

    @Override
    public void bindView(View v, Context context, Cursor cursor) {
        // TODO Auto-generated method stub

        ViewHolder holder = (ViewHolder) v.getTag();
        if (holder == null) {
            holder = new ViewHolder();
            holder.name = (TextView) v.findViewById(R.id.recentcall_item_name);
            holder.date = (TextView) v.findViewById(R.id.recentcall_item_date);
            holder.nameCol = cursor.getColumnIndex(Calls.CACHED_NAME);
            holder.dateCol = cursor.getColumnIndex(Calls.DATE);
            holder.numberCol = cursor.getColumnIndex(Calls.NUMBER);
            holder.cal = Calendar.getInstance();
            v.setTag(holder);
        }

        String name = cursor.getString(holder.nameCol);
        if(name == null){
            name = cursor.getString(holder.numberCol);
        }       
        holder.name.setText(name);

        holder.cal.setTimeInMillis(Long.valueOf(cursor.getString(holder.dateCol)));
        holder.date.setText(Utility.calculateTimePass(holder.cal.getTime()));
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        // TODO Auto-generated method stub
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.recentcall_item, parent, false);
        bindView(v, context, cursor);
        return v;
    }

有什么办法可以解决这个问题?谢谢。

4

3 回答 3

1

使用相同的选择器

    <selector xmlns:android="http://schemas.android.com/apk/res/android" >
     <item android:state_focused="true" android:drawable="@android:drawable/list_selector_background">

     </item>  
<item android:state_focused="true" android:state_selected="true" android:drawable="@android:drawable/list_selector_background"> 

     </item> 
    </selector>

并在 listview 标记中使用此代码设置选择器

android:drawSelectorOnTop="true"
android:listSelector="@drawable/selector"
于 2013-06-25T08:32:06.993 回答
0

我会给你一个对我有用的例子。

要在按下时保持列表视图项目的颜色,请在您的

列表视图布局:

android:background="@drawable/bg_key"

然后在 drawable 文件夹中定义 bg_key.xml,如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item 
       android:state_selected="true"
       android:drawable="@color/pressed_color"/>
    <item
     android:drawable="@color/default_color" />
</selector>

最后,将其包含在您的列表视图 onClickListener 中:

listView.setOnItemClickListener(new OnItemClickListener() {

 @Override
 public void onItemClick(AdapterView<?> parent, View view, int position,long arg3) {
    view.setSelected(true);
    ...
}
}

这样,任何时候都只会选择一个项目的颜色。你可以在 res/values/colors.xml 中定义你的颜色值,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="pressed_color">#4d90fe</color>
    <color name="default_color">#ffffff</color>
</resources>
于 2013-06-25T08:26:40.690 回答
0

在您的自定义布局中,将背景设置为android:background="@drawable/bkg"

bkg.xml

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

正常的.xml

  <?xml version="1.0" encoding="UTF-8"?> 
  <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"> 
  <solid android:color="#10EB0A"/>    
  <stroke android:width="3dp"
        android:color="#0FECFF" />  
  </shape>   

按下的.xml

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="rectangle"> 
<solid android:color="#FF1A47"/>    
<stroke android:width="3dp"
        android:color="#0FECFF"/>
</shape>  
于 2013-06-25T08:27:34.213 回答