【问题标题】:Android listview array adapter selected选择了 Android listview 数组适配器
【发布时间】:2012-04-11 11:26:12
【问题描述】:

我正在尝试将上下文操作模式添加到列表视图,但我在选择时遇到了一些问题,如果我创建 List1.setSelection(position) 它不会选择任何东西,如果我创建 List1.setItemChecked(position, true) 它有效,但它只会稍微改变字体颜色,我希望它改变背景或更值得注意的东西,有没有办法检测选择并手动更改背景,或者我错过了什么?

名单:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <ListView
         android:id="@+id/list1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:choiceMode="singleChoice"
      android:drawSelectorOnTop="false">
    </ListView>
</RelativeLayout>

适配器:

public class ServicesRowAdapter extends ArrayAdapter<String[]> {
    private final Activity context;
    private final ArrayList<String[]> names;
    static class ViewHolder {
        public TextView Id;
        public TextView Date;
        public RelativeLayout statusbar,bglayout;
    }

    public ServicesRowAdapter(Activity context, ArrayList<String[]> names) {
        super(context, R.layout.servicesrowlayout, names);
        this.context = context;
        this.names = names;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View rowView = convertView;
        if (rowView == null) {
            LayoutInflater inflater = context.getLayoutInflater();
            rowView = inflater.inflate(R.layout.servicesrowlayout, null);
            ViewHolder viewHolder = new ViewHolder();
            viewHolder.Id = (TextView) rowView.findViewById(R.id.idlabel);
            viewHolder.Date = (TextView) rowView.findViewById(R.id.datelabel);
            rowView.setTag(viewHolder);
        }

        ViewHolder holder = (ViewHolder) rowView.getTag();

        holder.Date.setText(names.get(position)[2]);
        holder.Id.setText(names.get(position)[1]);
        return rowView;
    }

}

使用布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    <TextView
        android:id="@+id/idlabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:gravity="right"
        android:text="@+id/idlabel"
        android:textSize="20dp"
        android:width="70dp" >

    </TextView>
        <TextView
        android:id="@+id/datelabel"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/datelabel"
        android:textSize="20dp"
        android:layout_marginLeft="90dp" >
    </TextView>

</RelativeLayout

【问题讨论】:

标签: java android listview layout


【解决方案1】:

这是因为在触摸模式下没有焦点或选择。使用选中状态,您走在正确的轨道上。您只需要使用具有不同状态的可绘制状态。

步骤:

1) 在你的 row_layout.xml 中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/item_selector"
> 

2) 在您的可绘制文件夹中创建一个新的 xml 文件(将名称与您在行布局中使用的名称匹配)(请注意,我设置了一个 color.xml 文件,因此我可以使用 @color/color ,如果你不这样做,你可以简单地把一个十六进制颜色代码放在那里):

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

就是这样。这不是很直观,但是当您启用选中状态时,“激活”基本上意味着“选中”。

通过该设置(并启用列表行可检查),您现在将在处于选中状态时将背景从通常的任何内容更改为蓝色。添加代码以在 onTouch 或 onClick 中设置该状态,然后一切就绪。在您触摸另一个项目之前,背景将保持这种状态。

【讨论】:

  • 我正在使用不同的手机,它们有不同的“主要”颜色,有没有办法获得系统颜色而不是仅仅定义蓝色?
  • 我找到了几种以编程方式执行此操作的方法,但是当您在静态 xml 文件中定义颜色时,这无济于事。
  • 谢谢,这个问题困扰了我一天,直到我发现你提到了 state_activated。我有 state_checked 但它不工作 - 将其更改为 state_activated 并且它工作。
猜你喜欢
  • 1970-01-01
  • 2015-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-28
  • 1970-01-01
  • 2021-03-05
  • 1970-01-01
相关资源
最近更新 更多