问题
For some reason, the first entry in my ListView is always flipped the wrong way around when I use RTL. For example:
Additional information:
The ListView is in a DialogFragment.
I HAVE updated my manifest to support RTL (the rest of the app works fine)
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
- 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 thegetView
of your ListAdapter - Not reusing/recycling your ListView rows/items, though this might slow down your app and is probably unadvisable
- 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