如何突出Lis​​tView项上的联系? [英] How to highlight ListView item on touch?

查看:157
本文介绍了如何突出Lis​​tView项上的联系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的的ListView ,我想每一个项目是对用户的触摸加以强调。我想这应该是默认发生,但事实并非如此。你能指点?

ListView的XML:

 <的ListView
    机器人:ID =@ + ID / list_view
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:填充=10dp
    机器人:分隔=#206600
    机器人:dividerHeight =2DP
    机器人:smoothScrollbar =真
    机器人:后台=#FFFFFF
    >
< / ListView控件>
 

和$ C $我的适配器C:

 私有类MyAdapter扩展ArrayAdapter<任务> {
        私人LayoutInflater mInflater;

        公共MyAdapter(上下文的背景下,INT资源,列表和LT;任务>列表){
            超(背景下,资源清单);
            mInflater = LayoutInflater.from(上下文);
        }

        @覆盖
        公共查看getView(INT位置,查看convertView,ViewGroup中父){
            视图V = convertView;

            如果(V == NULL){
                V = mInflater.inflate(R.layout.list_item,NULL);
            }

            任务task = taskList.get(位置);

            / *安装程序使用的数据对象在这里你布局视图* /

            返回伏;
        }
 

解决方案

您可能想发表您的实际行布局code,但我怀疑这个问题将是你设置你的列表行背景颜色。默认情况下,选择的绘制列表项(其中看起来更好,因为高亮颜色是文本后面)的后面。要么不设置你的列表项的背景颜色,或将其设置为利用顶部的选择,而不是与ListView的<一个href="http://developer.android.com/reference/android/widget/AbsListView.html#attr_android%3adrawSelectorOnTop"><$c$c>drawSelectorOnTop XML属性。

编辑:如果你真的必须有一个不透明的背景默认状态,不希望使用 drawSelectorOnTop ,你也可以试试这个:设置背景对你表行,但使用StateListDrawable使用 @android:绘制/ list_selector_background 为所有,但默认状态(可以定义您可绘制文件夹这个xml文件,看StateList <一href="http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList">documentation).

您也可以嵌套,其背景设置为你的外表中背景行布局中的布局@android:绘制/ list_selector_background ;这样的背景将利用你的背景之上,但低于的内容。

I have a simple ListView and I want each of it items to be highlighted on user's touch. I thought this should happen by default but it isn't. Can you advice?

ListView xml:

<ListView
    android:id="@+id/list_view"     
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:divider="#206600"
    android:dividerHeight="2dp"
    android:smoothScrollbar="true"
    android:background="#ffffff"
    >
</ListView>

And code of my Adapter:

private class MyAdapter extends ArrayAdapter<Task> {
        private LayoutInflater mInflater;

        public MyAdapter(Context context, int resource, List<Task> list) {
            super(context, resource, list);
            mInflater = LayoutInflater.from(context);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;

            if (v == null) {
                v = mInflater.inflate(R.layout.list_item, null);
            }

            Task task = taskList.get(position);

            /* Setup views from your layout using data in Object here */

            return v;
        }

解决方案

You may want to post your actual row layout code, but I suspect the problem will be that you set a background color on your list row. By default, the selectors are drawn behind the list items (which looks nicer, since the highlight color is behind the text). Either don't set a background color on your list items, or set it to draw the selector on top instead with ListView's drawSelectorOnTop XML attribute.

EDIT: If you really must have an opaque background for the default state and don't want to use drawSelectorOnTop, you can also try this: Set a background on your list rows, but use a StateListDrawable to use @android:drawable/list_selector_background for all but the default state (you can define an xml file in your drawables folder for this; see the StateList documentation).

You could also nest a layout inside your outer backgrounded row layout with its background set to @android:drawable/list_selector_background; that way the background would draw on top of your background, but below the content.

这篇关于如何突出Lis​​tView项上的联系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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