Ashwani K
Ashwani K

Reputation: 8000

Reset TextView position in row of Listview for Android Application

I am using ListView to display some data. The data is shown in TextView. String length which is shown can vary. so the textview which is shown changes its positions depending upon the length of the string. The problem I am facing is Suppose there are 5 rows. For Row 3 if the text is long, the position of textview showing the text adjusts to show the content. However, textview positions of other rows also changes even if the text is smaller. This has something to do with ListView row caching I am not able to figure out how to reset the row.

Row Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="100dp">


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="@dimen/text_margin_bottom"
        android:orientation="horizontal">

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/id1"
            android:textSize="@dimen/textsize_content3" />

        <View
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/id2"
            android:textSize="@dimen/textsize_content3" />
    </LinearLayout>
     <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="@dimen/text_margin_bottom"
        android:orientation="horizontal">

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/id3"
            android:textSize="@dimen/textsize_content3" />

        <View
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/id4"
            android:textSize="@dimen/textsize_content3" />
    </LinearLayout>
</LinearLayout>

GetView Code

public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            ShowData metaData = getItem(position);

            if (convertView == null) {
                convertView = inflater.inflate(R.layout.row, parent, false);
                holder = new ViewHolder();

                holder.id1 = (TextView) convertView
                        .findViewById(R.id.id1);
                holder.id2 = (TextView) convertView
                        .findViewById(R.id.id2);
                holder.id3 = (TextView) convertView
                        .findViewById(R.id.id3);
                holder.id3 = (TextView) convertView
                        .findViewById(R.id.id4);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            convertView.invalidate();
            holder.id1.setText(metaData.id1);
            holder.id2.setText(metaData.id2);
            holder.id3.setText(metaData.id3);
            holder.id4.setText(metaData.id4);
            return convertView;
        }

How to reset the listview row?

Upvotes: 1

Views: 175

Answers (1)

Mike M.
Mike M.

Reputation: 39191

As you mentioned, your TextViews are sizing vertically due to the wrap_content values for their layout_heights. It seems that when Views are recycled in ListViews (and, most likely, all AdapterViews), no layout is automatically triggered, so the TextViews will retain their previous heights. Calling requestLayout() on them will trigger a layout event, during which the TextViews will adjust to their new contents.

Upvotes: 1

Related Questions