问题
需要跑马灯的TextView和其他的View在同一个ViewGroup下, 有时候需要改动这个ViewGroup下其他View的LayoutParam,这个修改过程可能导致TextView暂时性失去焦点,表现为跑马灯效果会被重置,很难看。
解决方法
一般是采取隔离政策,把其他需要改动LayoutParam的View和跑马灯的TextView隔离开,用一个FrameLayout隔离一下。
<FrameLayout android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/song_name_container"
app:layout_constraintEnd_toStartOf="@+id/more_action"
app:layout_constraintBottom_toBottomOf="@+id/more_action"
app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="40dp"
android:layout_marginEnd="32dp">
<com.example.piaproboxanimation.Activity.FocusedTextView
android:text="你就不要想起我 - 陈奕迅简弘亦周杰伦合唱 "
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:textSize="22sp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/colorWhite"
android:id="@+id/song_name"
/>
</FrameLayout>
其中FocusedTextView继承自TextView,目的是重写isFocused()方法欺骗系统让FocusedTextView获得焦点,代码如下:
public class FocusedTextView extends android.support.v7.widget.AppCompatTextView {
public FocusedTextView(Context context) {
super(context);
}
public FocusedTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FocusedTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
/**
* 欺骗系统,让系统认为FocusedTextView得到了焦点了
*/
@Override
public boolean isFocused() {
return true;
}
}