Via Cà Matta 2 - Peschiera Borromeo (MI)
+39 02 00704272
info@synaptica.info

Evitare il reset dello scroll orizzontale (marquee ellipsize) su TextView Android

Digital Innovation Partner

Evitare il reset dello scroll orizzontale (marquee ellipsize) su TextView Android

Questo post da stack overflow è veramente veramente utile a risparmiare molto tempo e decine di righe di codice!

http://stackoverflow.com/questions/28305745/textview-scrolling-is-resetting-when-i-update-a-different-textview-from-the-same

Sostanzialmente la scroll animation su una TextView Android con scroll automatico orizzonatale (ellipsize marquee) viene resettata ogni qual volta cambia qualcosa nel layout in cui sono inserite se è impostata sulla textview una dimensione relativa al proprio contenitore (MATCH_PARENT o WRAP_CONTENT).

Specificando dimensioni fisse il reset non avviene, come fare?

TextView tv = new TextView(this);
tv.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
tv.setSingleLine(true);
tv.setHorizontallyScrolling(true);
tv.setFocusable(true);
tv.setFocusableInTouchMode(true);
tv.setMarqueeRepeatLimit(-1);
tv.setEllipsize(TextUtils.TruncateAt.MARQUEE);
tv.setSelected(true);

//the most important!!!!!
tv.post(new Runnable() {
    @Override
    public void run() {
        tv.setLayoutParams(new LinearLayout.LayoutParams(tv.getWidth(), tv.getHeight()));
    }
});

Lascia un commento