0

I'm trying to use the viewholder patter to improve the speed in my listviews. Everything works ok, but I have a problem with the alignment of a TextView. This is the XML of the TextView I'm having problems with:

                    <TextView
                    android:layout_width="40dp"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="-5dp"
                    android:text="200"
                    android:textSize="23sp"
                    android:id="@+id/windAverage"
                    android:layout_gravity="right"/>

In this textview there is a number, when I reuse a holder which had a number larger than 9 the problem occurs, for example:

There is a row which has 11 in this textview, the user would see "11kn" this is ok because the 11 is close to kn. When the user scrolls and I receive the holder of this row to reuse it and set the text from 11 to 5 for example, the user would see "5 kn" with a white space between the 5 and kn, if the previous value was bigger than 99 it would print 5 kn with two white spaces. It's like it is keeping the reference of the 11 to print the number. Is there a way to set the alignment to it's initial state so it is recalculated to print the new number in it's correct position?

dan87
  • 333
  • 3
  • 16

2 Answers2

0

Try setting Gravity.RIGHT on the TextView in question.

textView.setGravity(Gravity.RIGHT)

android:layout_gravity only sets the gravity of the entire view in its parent, not the gravity of the text inside the TextView.

Or, set the width of the TextView to LayoutParams.WRAP_CONTENT instead of a fixed value.

Jeffrey Mixon
  • 12,846
  • 4
  • 32
  • 55
0

Finally I found the solution, I just had to reset the layout_gravity programmatically:

LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)holder.windAverageText.getLayoutParams();
params.gravity = Gravity.RIGHT;
holder.windAverageText.setLayoutParams(params);
dan87
  • 333
  • 3
  • 16