Android的ListView控件数组适配器选择 [英] Android listview array adapter selected

查看:150
本文介绍了Android的ListView控件数组适配器选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图上下文动作方式添加到列表视图,但我有一些问题的选择,如果我做一个 List1.setSelection(位置)不选择任何东西,如果我做 List1.setItemChecked(位置,真)它的工作原理,但它只能改变字体颜色一点点,我希望它改变背景或一些更著名的,有没有什么办法来检测并选择手动或更改背景,或者我失去了一些东西?结果

清单:

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>
    < ListView控件
         机器人:ID =@ + ID / list1的
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:choiceMode =singleChoice
      机器人:drawSelectorOnTop =假>
    < /&的ListView GT;
< / RelativeLayout的>

适配器:

 公共类ServicesRowAdapter扩展ArrayAdapter<的String []> {
    私人最终活动范围内;
    私人最终的ArrayList<的String []>名称;
    静态类ViewHolder {
        公共TextView的标识;
        公共TextView的日期;
        公共RelativeLayout的状态栏,bglayout;
    }    公共ServicesRowAdapter(Activity上下文,ArrayList的<的String []>名称){
        超(背景下,R.layout.servicesrowlayout,姓名);
        this.context =背景;
        this.names =名称;
    }
    @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup){
        查看rowView = convertView;
        如果(rowView == NULL){
            LayoutInflater吹气= context.getLayoutInflater();
            rowView = inflater.inflate(R.layout.servicesrowlayout,NULL);
            ViewHolder viewHolder =新ViewHolder();
            viewHolder.Id =(TextView中)rowView.findViewById(R.id.idlabel);
            viewHolder.Date =(TextView中)rowView.findViewById(R.id.datelabel);
            rowView.setTag(viewHolder);
        }        ViewHolder支架=(ViewHolder)rowView.getTag();        holder.Date.setText(names.get(位置)[2]);
        holder.Id.setText(names.get(位置)[1]);
        返回rowView;
    }}

与使用的布局的

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT>
    <的TextView
        机器人:ID =@ + ID / idlabel
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_centerVertical =真
        机器人:重力=右
        机器人:文字=@ + ID / idlabel
        机器人:TEXTSIZE =20dp
        机器人:宽=70dp>    < / TextView的>
        <的TextView
        机器人:ID =@ + ID / datelabel
        机器人:layout_centerVertical =真
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=@ + ID / datelabel
        机器人:TEXTSIZE =20dp
        机器人:layout_marginLeft =90dp>
    < / TextView的>< / RelativeLayout的


解决方案

这是becasue联系方式没有针对性或选择。您正在使用的选中状态在正确的轨道上。你只需要使用一个状态绘制具有不同的状态。

步骤:

1)在你row_layout.xml

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:背景=@绘制/ item_selector
>

2)创建一个新的XML文件(匹配的名称在您的排布置在绘制文件夹中使用),像这样一个(请注意,我有一个color.xml文件中设置了这样我就可以用@色/颜色如果你没有,你可以简单地把一个十六进制颜色code在那里):

 <?XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <项目
        机器人:state_activated =真
        机器人:可绘制=@色/蓝色/>
< /选择>

就是这样。这不是reaql直观,但激活基本上意味着选中当你启用的选中状态。

使用这个配置(并启用列表行是可检查的),你现在有背景从不管它通常是蓝色的选中状态时的变化。添加code设置在onTouch或的onClick的状态,并且您将所有设置。直到触摸另一个项目的背景将保持这种方式。

i'm trying to add a contextual action mode to a listview, but i'm having some problems with the selection, if i make aList1.setSelection(position) it doesn't select anything, and if i make List1.setItemChecked(position, true) it works but it only changes the font color a little and i want it to change the background or something more notable, is there any way to detect the selection and manually and change the background, or i'm missing something?

the list:

<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>

the adapter:

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;
    }

}

with the use of a layout:

<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

解决方案

This is becasue in touch mode there is no focused or selection. You're on the right track using the checked state. You just need to use a state drawable with different states.

Steps:

1) In your 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) Create a new xml file (match the name to the one you used in your row layout) in your drawable folder like so (note that I have a color.xml file set up so I can use @color/color, if you don't you can simply put a hex color code in there):

<?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>

That's it. It's not reaql intuitive, but "activated" basically means "checked" when you have enabled the checked state.

With that setup (and enabling the list rows to be checkable) you will now have the background changed from whatever it is normally to blue when in the checked state. Add code to set that state in onTouch or onClick and you'll be all set. The background will stay that way until you touch another item.

这篇关于Android的ListView控件数组适配器选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆