android - ListView 的第一个条目对于 RTL 总是不正确

标签 android listview layout right-to-left

出于某种原因,当我使用 RTL 时,我的 ListView 中的第一个条目总是以错误的方式翻转。例如:

Elements in ListView Flipped wrong way around
附加信息:

  • ListView 位于 DialogFragment 中。
  • 我已经更新了我的 list 以支持 RTL(应用程序的其余部分工作正常)
  • ListView 有一个适配器,每个项目的布局如下所示:











    <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"/>
    


  • 虽然对话框 fragment 的布局看起来像:
    <?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>
    

    我不确定还能尝试什么。我已经摆弄了不同的布局。改变方向并不能解决问题。任何帮助将不胜感激。

    最佳答案

    在探索中,我发现问题只发生在对话框中的 ListViews 上(我试过 AlertDialog),并且在向下和向上滚动后,最上面的 ListView 行的方向性是固定的。我不确定为什么会这样,只能建议

  • 通过添加 rowView.setLayoutDirection(getContext().getResources().getConfiguration().getLayoutDirection()); 根据配置手动设置方向(根据设备语言非常可靠地更新)在 getView您的 ListAdapter
  • 不重复使用/回收您的 ListView 行/项目,尽管这可能会减慢您的应用程序并且可能是不可取的
  • 切换到 RecyclerView

  • 附言见 this question有同样的问题

    附言getView 似乎只对对话框中的 ListViews 而不是 Fragments/Activity 中的 ListViews 被调用很多且不规则,并且可能值得研究。虽然不能保证 ListView 的行为,但还是有点奇怪

    关于android - ListView 的第一个条目对于 RTL 总是不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55843642/

    相关文章:

    c++ - 打开窗口时应用程序崩溃 qt

    java - Android 截击 : Static vs Object

    android - api <21 的可绘制着色

    java - Android:如何在操作/工具栏之外创建选项菜单

    C# listView,如何将项目添加到第 2、3 和 4 列等?

    c# - C# .NET 控制台应用程序使用的控制台字体和布局

    android - TextInputLayout 作为约束布局的一部分的布局问题

    android - 如何在 Listview Android 中添加多个项目

    python - 在python中拆分列表的项目

    css - 如何在元素之间共享 CSS StyleSheet?