ListView's first entry always incorrect for RTL

混江龙づ霸主 提交于 2020-08-06 21:38:04

问题


For some reason, the first entry in my ListView is always flipped the wrong way around when I use RTL. For example:

Additional information:

  1. The ListView is in a DialogFragment.

  2. I HAVE updated my manifest to support RTL (the rest of the app works fine)

  3. The ListView has an Adapter, the layout per item looks like:

    <ImageView
        android:id="@+id/dropdown_icon"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:src="@drawable/photo_button_disabled"
        android:layout_gravity="center_horizontal"/>
    
    <ImageView
        android:id="@+id/count_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:scaleType="fitCenter"
        android:layout_alignEnd="@+id/dropdown_icon"
        android:layout_alignRight="@+id/dropdown_icon"
        android:layout_alignStart="@+id/dropdown_icon"
        android:layout_alignLeft="@+id/dropdown_icon"
        android:layout_alignTop="@+id/dropdown_icon"
        android:layout_alignBottom="@+id/dropdown_icon"
        android:adjustViewBounds="true"/>
    

While the layout for the Dialog Fragment looks like:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:background="@drawable/rounded_bg">

<TextView
    android:id="@+id/title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="@android:color/white"
    android:textStyle="bold"
    android:textSize="@dimen/dialog_title"
    android:padding="10dp"/>

<LinearLayout
    android:layout_below="@+id/title"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <!-- This table should be populated with all the different options -->
        <ListView
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:id="@+id/entry_list"
            android:divider="?android:attr/dividerHorizontal"
            android:showDividers="middle"
            android:background="@color/overlay"
            android:descendantFocusability="afterDescendants"
            android:animateLayoutChanges="true">
        </ListView>


    <!-- The save and clear buttons-->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingLeft="@dimen/general_padding"
        android:paddingTop="@dimen/general_padding"
        android:paddingRight="@dimen/general_padding">

        <TextView
            android:id="@+id/warning_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="@string/recon_value_required"
            android:visibility="gone"
            android:textColor="@color/warningColour"
            android:layout_marginBottom="@dimen/list_vertical_margin"/>

        <LinearLayout
            android:id="@+id/button_container"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clipChildren="false"
            android:clipToPadding="false"
            android:layout_below="@id/warning_label"
            android:paddingBottom="@dimen/general_padding">
            <Button
                android:id="@+id/clear_button"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:background="@drawable/rounded_button"
                android:text="@string/clear"
                android:textColor="@color/buttonTextColour"
                android:layout_weight="1"
                android:foreground="?attr/selectableItemBackground"
                android:layout_marginRight="8dp"
                android:layout_marginEnd="8dp"/>

            <Button
                android:id="@+id/save_button"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:background="@drawable/rounded_button"
                android:text="@string/save"
                android:textColor="@color/buttonTextColour"
                android:layout_weight="1"
                android:foreground="?attr/selectableItemBackground"
                android:layout_marginLeft="8dp"
                android:layout_marginStart="8dp"/>
        </LinearLayout>

    </RelativeLayout>

</LinearLayout>
</RelativeLayout>

I'm not sure what else to try. I have fiddled with the different layouts. Changing orientation doesn't fix the issue. Any help would be greatly appreciated.


回答1:


On exploring, I've found that the problem only happens on ListViews in Dialogs (I tried AlertDialog), and the directionality of the topmost ListView row is fixed after scrolling down and back up. I'm not sure why it happens, and can only suggest

  1. manually setting the direction based on configuration (that is quite reliably updated based on device language) by adding rowView.setLayoutDirection(getContext().getResources().getConfiguration().getLayoutDirection()); in the getView of your ListAdapter
  2. Not reusing/recycling your ListView rows/items, though this might slow down your app and is probably unadvisable
  3. Switching to RecyclerView

P.S. See this question with the same issue

P.P.S. getView seems to be called a lot and irregularly only for ListViews in Dialogs and not those in Fragments/Activities, and might be worth looking into. Although ListView behaviour is not guaranteed, it's still a little strange



来源:https://stackoverflow.com/questions/55843642/listviews-first-entry-always-incorrect-for-rtl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!