简体   繁体   English

Android ListView-更改活动项目的文本颜色

[英]Android ListView - Change active item's text's color

I'd like to create a custom effect on my ListView in Android. 我想在Android的ListView上创建自定义效果。 I have a list with text items - a kind a menu. 我有一个包含文本项的列表-一种菜单。 And I'd like to change the text's color (and add a shadow for it!), when the Android changes the background under the list element. 当Android更改list元素下的背景时,我想更改文本的颜色(并为其添加阴影!)。

I've a custom view (wich extends the TextView), where I overrided the onTouchEvent() function, and created a function, which changes the text's color back. 我有一个自定义视图(其中扩展了TextView),在这里我覆盖了onTouchEvent()函数,并创建了一个函数,该函数可以将文本的颜色改回原来的颜色。

@Override
public boolean onTouchEvent(MotionEvent event){
    Log.v(TAG, "" + event.getAction());
    if (event.getAction() == MotionEvent.ACTION_DOWN){
        Log.v(TAG, "Touch started.");
        setShadowLayer(6, 0, 0, getResources().getColor(R.color.selectedTextShadow));
        setTextColor(getResources().getColor(R.color.selectedTextColor));
        boolean b = super.onTouchEvent(event);
        Log.v(TAG, "Action down, super returns: " + (b ? "true" : "false"));
        //return true;
    }
    return super.onTouchEvent(event);
}

public void endSelection(){
    setShadowLayer(0, 0, 0, getResources().getColor(R.color.textColor));
    setTextColor(getResources().getColor(R.color.textColor));
}

And in the ListFragment I override the onListItemClick() function, to call the endSelection function of the view. 在ListFragment中,我重写了onListItemClick()函数,以调用视图的endSelection函数。

@Override
public void onListItemClick (ListView l, View v, int position, long id){
    Log.v(TAG, "Clicked on: " + position);
    ((ListRowText) v.findViewById(R.id.subMenuRow_menuItem)).endSelection();
}

It's almost perdect. 几乎是完美的。 But (as you can see it in this video), it does not change back the color, when I "swipe" my finger somewhere else, and the color "stucks". 但是(如您在视频中看到的那样),当我在其他地方“滑动”手指时,它并不会变回颜色,并且颜色“卡住了”。

Is there a better way, to do this? 有一个更好的方法吗? I've found the StateListDrawable s, but I cannot add a shadow for StateListDrawable. 我已经找到了StateListDrawable ,但是无法为StateListDrawable添加阴影。 Or can I? 可以吗 Thanks! 谢谢!

Clean looking UI by the way. 顺便说一下干净的用户界面。 You'll probably want to take a look at Android Asset Studios' Holo colors generator and generate a list selector to get a look at all the events that are being tracked on touch. 您可能需要看一下Android Asset Studios的Holo颜色生成器,并生成一个列表选择器,以查看触摸时正在跟踪的所有事件。 For your individual list item views you may want to make a separate state list drawable for just the text view and style it according with your list selector. 对于您的单个列表项视图,您可能希望为文本视图创建一个单独的可绘制状态列表,并根据列表选择器设置样式。 Here is a list of states that the generated selector handles: 这是生成的选择器处理的状态列表:

  • state_window_focused=false state_window_focused = false
  • state_focused=true && state_enabled=false && state_pressed=true state_focused = true && state_enabled = false && state_pressed = true
  • state_focused=true && state_enabled=false state_focused = true && state_enabled = false
  • state_focused=true && state_pressed=true state_focused = true && state_pressed = true
  • state_focused=false && state_pressed=true state_focused = false && state_pressed = true
  • state_focused=true state_focused = true

As Patty P said you can just create a state selector but in your case I think what you want is a text selector. 正如Patty P所说的,您只能创建一个状态选择器,但就您而言,我认为您想要的是一个文本选择器。

http://developer.android.com/guide/topics/resources/color-list-resource.html http://developer.android.com/guide/topics/resources/color-list-resource.html

You simply create a folder called color in your res folder and you can add selectors just like in the case of the list selector. 您只需在res文件夹中创建一个名为color的文件夹,就可以添加选择器,就像列表选择器一样。

You can then use it as text color. 然后,您可以将其用作文本颜色。

android:textColor="@color/mystatelist" android:textColor =“ @ color / mystatelist”

The states are the same 状态是相同的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM