开发者

How to activate a marquee for a TextView inside a focused ViewGroup?

开发者 https://www.devze.com 2023-01-31 17:14 出处:网络
Here\'s a layout: <LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\" android:layout_width=\"fill_parent\"

Here's a layout:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <RelativeLayout
        android:id="@+id/first_tv_container"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:background="@android:drawable/list_selector_background">
        <TextView
            android:id="@+id/first_tv"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
            android:singleLine="true"
            android:ellipsize="marquee"
            android:duplicateParentState="true"/>
    </RelativeLayout>
    <TextView
        android:id="@+id/second_tv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
        android:singleLine="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:background="@android:drawable/list_selector_background"/>
</LinearLayout>

When second_tv gets the focus, second_tv's marquee is activated as expected. When first_tv_container gets the focus, I would like first_tv's marquee to be as well. I thought adding duplicaParentState=true to first_tv would do the trick, but it doesn't. So my question would be: is there a standard, easy way to have a TextVie开发者_运维问答w's marquee working inside a focused ViewGroup (other than a ListView, which automatically handles the case correctly) without extending and overriding a lot of stuff?

Regards


OK, I've found some kind of way. I added android:focusable="true" to first_tv, and instantiated first_tv_container as an extended version of RelativeLayout in which I declare descendantFocusability="blocksDescendants" and define the following override:

@Override
protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect)
{
    super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);

    setSelected(gainFocus);
    dispatchSetSelected(gainFocus);
}

Exactly the kind of stuff I wanted to avoid, but that will do for now.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号