Android's TextView with marquee in a ListView won't animate for the fist time it is shown

岁酱吖の 提交于 2019-12-10 10:19:24

问题


I've got a ListView with a custom adapter. Every item in it contains a TextView.

<TextView
    android:id="@+id/chat_name"
    android:layout_height="48dp"
    android:layout_width="match_parent"
    android:textColor="#FFFFFF"
    android:gravity="center_vertical"
    android:ellipsize="marquee"
    android:marqueeRepeatLimit="marquee_forever"
    android:singleLine="true"
    android:focusable="false"
    android:focusableInTouchMode="false"/>

I need this TextView to not be focusable, because I rely on the onListItemClick() method. As I learned, onListItemClick() won't fire if you set focusable="true".

In the adapter I set:

TextView chatName = (TextView) v.findViewById(R.id.chat_name);
chatName.setText(chat.getChatName());
chatName.setSelected(true);

And it works every time but first one. When an item is added to the adapter it shows on the listview, but it isn't marqueed. When item is clicked, another activity is displayed. After the user returns to the ListView, the item is suddenly marqueed. I want it marqueed all the time.

I have already tried subclassing the TextView with a method that overwrites isFocused() to always return true, but that didn't help. Behavior remained the same. I also experimented with various values of focusable and focusableInTouchMode attributes, but with no success.


回答1:


Please use requestFocus() function as follows.

chatName.setSelected(true);
chatName.requestFocus();

I will work.




回答2:


In your custom adapter, add this into getView:

chatName.addOnLayoutChangeListener(new OnLayoutChangeListener() 
{
    @Override
    public void onLayoutChange(View v, ....) 
    {
        chatName.setSelected(true);
    }
});


来源:https://stackoverflow.com/questions/11311085/androids-textview-with-marquee-in-a-listview-wont-animate-for-the-fist-time-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!