android - 在 ListView 中设置 TextView 属性 - 奇怪的结果

标签 android

我有一个 ListView Activity ,它通过自定义 ArrayAdapter 填充。有一个对象数组,它有一个名为“isRead”的 bool 属性。根据此属性的值,我想将行中其中一个 TextView 的字体设置为“正常”或“粗体”。我还将文本的颜色设置为“灰色”或“黑色”

当 Activity 最初启动时,一切都按预期进行。如果 isRead 为真,则文本为灰色,字体为普通字体。向下滚动列表然后再次向上滚动后,isRead 为 true 的对象的文本为灰色,但字体为粗体。

我是不是做错了什么?或者这是 Android 的一些奇怪之处?

ArrayAdapter 代码如下:

    public View getView(int position, View convertView, ViewGroup parent) {
       LayoutInflater inflater = getLayoutInflater();
       View row = convertView;
       ArticleListRowViewWrapper wrapper=null;
       if (row == null) {
           row=inflater.inflate(R.layout.articlesrow, parent, false);
           wrapper = new ArticleListRowViewWrapper(row);
           row.setTag(wrapper);
       } else {
           wrapper = (ArticleListRowViewWrapper)row.getTag();
       }
       Article a = articles.get(position);
       String dateString = df.format(a.getPubDate());
       TextView titleLabel=wrapper.getTitleLabel();
       Typeface tf = titleLabel.getTypeface();
       if (a.getIsRead()) {
           titleLabel.setTextColor(Color.GRAY);
           titleLabel.setTypeface(tf, Typeface.NORMAL);
       } else {
           titleLabel.setTextColor(Color.BLACK);
           titleLabel.setTypeface(tf, Typeface.BOLD);
       }
       titleLabel.setText(a.getTitle());
       TextView dateLabel =wrapper.getDateLabel();
       dateLabel.setText(dateString);
       return row;
    }
}

最佳答案

我能够使用以下更改后的代码 fragment 来实现这一点:

       Typeface tf = titleLabel.getTypeface();
       if (a.getIsRead()) {
           titleLabel.setTextColor(Color.GRAY);
           titleLabel.setTypeface(Typeface.create(tf, Typeface.NORMAL));
       } else {
           titleLabel.setTextColor(Color.BLACK);
           titleLabel.setTypeface(Typeface.create(tf, Typeface.BOLD));
       }

在这方面的帮助归功于 CommonsWare。

关于android - 在 ListView 中设置 TextView 属性 - 奇怪的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2874394/

相关文章:

Android:中心按钮水平。和垂直。在 View 组中

安卓通知点击恢复 Activity

android - Android 4.3 纵向模式下的全屏相机预览

Android 移动应用程序到 sd

Android:找不到我自己导入的类的符号

android - 创 build 备所有者 Android Lollipop 时校验和错误

android - 是否可以检测用户(Android)何时打开屏幕?

android - 无法使用 Dagger 2 运行 Kotlin

android - 我怎样才能格式化 sdcard android 应用程序

java - 勺子 Gradle : Could not create plugin of type 'AppPlugin'