【问题标题】:Android ArrayAdapter not properly converting ViewsAndroid ArrayAdapter 未正确转换视图
【发布时间】:2017-01-07 08:39:36
【问题描述】:

我有一个自定义 ArrayAdapter 用于 ListView,它使用自定义行布局,在 XML 中单独定义。布局只有三个TextViews 和一个ImageView,放在一个RelativeLayout 中。为了提高性能,适配器使用像here 描述的那样的 ViewHolder 系统来转换现有视图而不是膨胀新视图。在ArrayAdapter.getView() 中,适配器应该根据布尔值将第一个 TextView 加粗或取消加粗。

当我第一次打开应用程序时,所有的 TextView 都正确地加粗或不加粗。但是,如果我滚动到 ListView 的底部,然后滚动回顶部,所有标题 TextViews 都是粗体的,即使它们不应该是粗体。我认为它必须与转换已经粗体的现有视图有关,但我无法弄清楚它是什么。我已经使用 Android Studio 调试了该应用程序,它的运行方式就像我认为的那样——当我向上滚动时,适配器在调试窗口中正确地加粗/取消加粗,但它们在应用程序上似乎都是粗体。

我注意到的一件事是,如果我将 TextView 的 textStyle 属性更改为“粗体”,则所有 TextView 的标题从一开始都是粗体,并且永远不会改变。只有当我删除 textStyle 或将其设置为“正常”时,TextView 在开始时是正常的。

这里是 ArrayAdapter 中的 getView():

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    PostShell shell = postShellList.get(getCount() - 1 - position); //I stack my ListView backwards, so index 0 is at the bottom
    ViewHolder holder;
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.row_layout, parent, false);
        holder = new ViewHolder();
        holder.firstLine = (TextView) convertView.findViewById(R.id.firstLine);
        holder.secondLine = (TextView) convertView.findViewById(R.id.secondLine);
        holder.thirdLine = (TextView) convertView.findViewById(R.id.thirdLine);
        holder.image = (ImageView) convertView.findViewById(R.id.image);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.firstLine.setText(shell.postTitle);
    if (shell.unread) {
        holder.firstLine.setTypeface(holder.firstLine.getTypeface(), Typeface.BOLD);
    } else {
        holder.firstLine.setTypeface(holder.firstLine.getTypeface(), Typeface.NORMAL);
    }
    //convert other TextViews
}

我的 ViewHolder 类只是一个带有几个 TextView 和一个 ImageView 的静态类。

这是我正在使用的行布局代码的相关部分:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="88dp">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="84dp"
    android:paddingRight="16dp"
    android:paddingTop="6dp"
    android:id="@+id/firstLine"
    android:ellipsize="end"
    android:text="First"
    android:textStyle="normal"
    android:singleLine="true" />

<!-- other views -->

</RelativeLayout>

【问题讨论】:

  • 复制粘贴的诅咒...感谢您修复我的答案:)
  • @0X0nosugar 没问题,感谢您的回答! :)

标签: android listview android-arrayadapter android-viewholder


【解决方案1】:

问题似乎是“取消加粗”文本不适用于以下语句:

holder.firstLine.setTypeface(holder.firstLine.getTypeface(), Typeface.NORMAL);

下面的 sn-p 省略了holder.firstLine.getTypeface(),只使用了更简单的setTypeface()。为我工作。

if (shell.unread) {
    holder.firstLine.setTypeface(holder.firstLine.getTypeface(), Typeface.BOLD);
} else {
    holder.firstLine.setTypeface(Typeface.DEFAULT);
}

【讨论】:

    【解决方案2】:

    您应该按正常顺序存储视图并使用android:stackFromBottomsetStackFromBottom()

    【讨论】:

      【解决方案3】:
      PostShell shell = postShellList.get(getCount() - 1 - position); //I stack my ListView backwards, so index 0 is at the bottom
      

      如果ListView 请求特定位置的元素,你最好给它那个位置的元素,否则会混淆。如果您想更改列表中项目的顺序,请更改列表的顺序,不要试图欺骗ListView 使其以不同的顺序呈现。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-09-28
        • 1970-01-01
        • 2015-07-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-07
        相关资源
        最近更新 更多