在 RecyclerView 中自动滚动 TextView

Automatically scrolling TextView inside a RecyclerView

我目前的问题如下:我有一个 RecyclerView,这个 RecyclerView 的每个元素都有一个 TextView。我希望 TextView 自动滚动。如果到目前为止尝试过会怎样:

自定义 TextView:

public class ScrollCustomTextView extends AppCompatTextView {
public ScrollCustomTextView(Context context) {
    super(context);
}

public ScrollCustomTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context,attrs,defStyle);
}

public ScrollCustomTextView(Context context, AttributeSet attrs) {
    super(context,attrs);
}
@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
    if (focused)
    super.onFocusChanged(focused, direction, previouslyFocusedRect);
}


@Override
public void onWindowFocusChanged(boolean focused) {
    if(focused)
        super.onWindowFocusChanged(focused);
}


@Override
public boolean isFocused() {
    return true;
}

}

我的 .xml 文件如下所示:

<package.ScrollCustomTextView
        android:id="@+id/main_idea_name"
        android:scrollHorizontally="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/darkGreen"
        android:textSize="27dp"
        android:layout_marginTop="10dp"
        android:maxLines="1"
        android:textAppearance="@android:style/TextAppearance.Medium"

        />

然后在我设置的 ViewHolder 的构造函数中的 RecyclerView 中

textView.setSelected(true);

但是没用。有没有人有解决这个问题的想法?

谢谢。

所以这里是答案:

我将我的 TextView 放在 Horizo​​ntalScrollView 中,并将以下代码添加到我的 RecyclerView 适配器中:

private void animateTextView(TextView mTextView) {
    int textWidth = getTextViewWidth(mTextView);
    int displayWidth = getDisplayWidth(mContext);

/* Start animation only when text is longer than dislay width. */
    if(displayWidth<=textWidth) {
        Animation mAnimation = new TranslateAnimation(
                0, -textWidth,
                0, 0);
        mAnimation.setDuration(10000);    // Set custom duration.
        mAnimation.setStartOffset(1000);    // Set custom offset.
        mAnimation.setRepeatMode(Animation.RESTART);    // This will animate text back ater it reaches end.
        mAnimation.setRepeatCount(Animation.INFINITE);    // Infinite animation.

        mTextView.startAnimation(mAnimation);
    }
}

private int getDisplayWidth(Context context) {
    int displayWidth;

    WindowManager windowManager = (WindowManager)context.getSystemService(
            Context.WINDOW_SERVICE);
    Display display = windowManager.getDefaultDisplay();
    Point screenSize = new Point();

    if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB_MR2) {
        display.getSize(screenSize);
        displayWidth = screenSize.x;
    } else {
        displayWidth = display.getWidth();
    }

    return displayWidth;
}

private int getTextViewWidth(TextView textView) {
    textView.measure(0, 0);    // Need to set measure to (0, 0).
    return textView.getMeasuredWidth();
}

并开始动画:

animateTextView(txtView);

注意:不需要自定义TextView,直接使用普通TextView即可。