为什么在视图上调用setBackgroundColor打破其长按颜色变化,并强调? [英] Why does calling setBackgroundColor on a view break its long click color change and highlighting?

查看:144
本文介绍了为什么在视图上调用setBackgroundColor打破其长按颜色变化,并强调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android中,默认情况下,当你长按上一个列表项,它会从高亮颜色为白色,表明用户已举行了下来,一个上下文项目将被显示出来。此外,您还可以使用轨迹球(或箭头在某些手机上的按钮),选择列表中的项目而不是用你的手指。

不过,我有一个ListView的项目的意见,我呼吁setBackgroundColor,都与预期的行为不再起作用。任何人都知道这是为什么,以及如何解决它?

注:设置在XML的背景颜色是不是一种选择,因为我需要能够动态设置/改变颜色

在code为我NewView的功能如下:

  @覆盖
公共查看NewView的(上下文CTX,光标光标的ViewGroup父)
{
    查看查看=新景(mCtx);
    最后LayoutInflater李=(LayoutInflater)mCtx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    鉴于= li.inflate(R.layout.task_list_item,NULL);    返回视图。
}


解决方案

默认情况下,的ListView 有一个选择这将起到 TransitionDrawable 当你长preSS列表项。但是,如果你的列表项视图有一个坚实的背景,那么你将无法看到选择的长期preSS动画(或任何其他国家),因为它涵盖了由列表项的背景。

如果你想看到选择的长期preSS动画/选择/ pressed状态,则列表项的必须有项目时所选的透明背景/ pressed /长pressed。您可以通过使用做一个<一个href=\"http://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html\">StateListDrawable为您的项目的背景,而不是纯色。

下面是<一个样本href=\"http://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html\">StateListDrawable为此目的:

 公共类ColorfulListItemDrawable扩展StateListDrawable {
    私人最终PaintDrawable mColor;    公共ColorfulListItemDrawable(INT颜色){
        mColor =新PaintDrawable(彩色);
        初始化();
    }
    私人无效初始化(){
        可绘制彩色= mColor;
        可绘制选择=新ColorDrawable(Color.TRANSPARENT);
        addState(新INT [] {android.R.attr.state_ pressed,android.R.attr.state_enabled,
                android.R.attr.state_window_focused},选择);
        addState(新INT [] {android.R.attr.state_ pressed,android.R.attr.state_enabled,
                android.R.attr.state_window_focused,android.R.attr.state_selected},选择);
        addState(新INT [] {android.R.attr.state_enabled,android.R.attr.state_window_focused,android.R.attr.state_selected},选择);
        addState(新INT [] {},颜色);
    }
    公共无效的setColor(INT颜色){
        。mColor.getPaint()的setColor(彩色);
        mColor.invalidateSelf();
    }
}

In Android, by default when you long click on a list item, it goes from the highlight color to white indicating that the user has held it down and a context item will be displayed. Also, you can use the trackball (or arrow buttons on some phones) to select list items rather then using your fingers.

However, I have a ListView whose item's views I am calling setBackgroundColor on, and both of those expected behaviors no longer work. Anybody know why this is and how to fix it?

Notes: Setting the background color in the xml is not an option because I need to be able to set/change the color dynamically.

The code for my newView function is as follows:

@Override
public View newView(Context ctx, Cursor cursor, ViewGroup parent)
{
    View view = new View(mCtx);
    final LayoutInflater li = (LayoutInflater) mCtx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = li.inflate(R.layout.task_list_item, null);

    return view;
}

解决方案

By default, ListView has a Selector that will play a TransitionDrawable when you longpress a list item. However, if your list item view has a solid background, then you won't be able to see the selector's longpress animation (or on any other states) , because it's covered up by the list item's background.

If you want to see the selector's longpress animation/selected/pressed state, then the list item's has to have a transparent background when the item is selected/pressed/longpressed. You can do it by using a StateListDrawable as your item's background instead of a solid color.

Here is a sample of StateListDrawable for that purpose:

public class ColorfulListItemDrawable extends StateListDrawable {
    private final PaintDrawable mColor;

    public ColorfulListItemDrawable(int color) {
        mColor = new PaintDrawable(color);
        initialize();
    }
    private void initialize() {
        Drawable color = mColor;
        Drawable selected = new ColorDrawable(Color.TRANSPARENT);
        addState(new int[] {android.R.attr.state_pressed, android.R.attr.state_enabled, 
                android.R.attr.state_window_focused}, selected);
        addState(new int[] {android.R.attr.state_pressed, android.R.attr.state_enabled, 
                android.R.attr.state_window_focused,android.R.attr.state_selected}, selected);
        addState(new int[] {android.R.attr.state_enabled,android.R.attr.state_window_focused, android.R.attr.state_selected}, selected);
        addState(new int[] {}, color);
    }
    public void setColor(int color) {
        mColor.getPaint().setColor(color);
        mColor.invalidateSelf();
    }   
}

这篇关于为什么在视图上调用setBackgroundColor打破其长按颜色变化,并强调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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