0

Mon problème actuel est le suivant: J'ai un RecyclerView et chaque élément de ce RecyclerView a un TextView. Je veux que le TextView défile automatiquement. Et si essayé jusqu'à présent:Défilement automatique de TextView dans un RecyclerView

Personnaliser 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; 
} 

} 

et mon fichier .xml ressemble à ceci:

<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" 

     /> 

Et RecyclerView dans le constructeur pour le ViewHolder je mis

textView.setSelected(true); 

Mais cela ne fonctionne pas. Est-ce que quelqu'un a une idée pour résoudre cela?

Merci.

Répondre

0

Voici donc la réponse:

Je mets mon TextView dans un HorizontalScrollView et a ajouté le code suivant à mon adaptateur pour le 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(); 
} 

Et commence l'animation avec:

animateTextView(txtView); 

Remarque: il n'y a plus besoin de TextView personnalisé, il suffit d'utiliser un TextView normal.