android:getView()问题 - 它在滚动时更改项目

发布于 2024-11-14 07:47:25 字数 2012 浏览 4 评论 0原文

我制作了一个包含三个文本视图和一个复选框的列表视图,并将其放入 BaseAdapter 中。它要做的是,如果某个项目处于“未读”状态,则将其文本视图设为粗体。然而,我遇到了两个问题。

  1. 无论已读或未读,顶部项目始终为粗体
  2. 如果我滚动,那么通过滚动重新加载的项目开始变得粗体,

我对此进行了研究,但找不到任何对我有用的东西。如果有人有想法,请帮助我吗?下面是 getView()。


public View getView(final int position, View convertView, ViewGroup parent) {
            View view = convertView;
            ViewHolder viewHolder;

            if(convertView == null){ // check if convertView exists
                // get and inflate layout
                view = layoutInflater.inflate(R.layout.row, null);

                viewHolder = new ViewHolder();
                viewHolder.tv1 = (TextView)view.findViewById(R.id.list_callerIDname);
                viewHolder.tv2 = (TextView)view.findViewById(R.id.list_callerIDnumber);
                viewHolder.tv3 = (TextView)view.findViewById(R.id.list_messageSentTime);
                viewHolder.cb = (CheckBox)view.findViewById(R.id.checkBox1);

                // get each CheckBox into cb_array for future use
                this.cb_array[position] = (CheckBox)view.findViewById(R.id.checkBox1);

                // use it as a tag
                view.setTag(viewHolder);
            }
            else{
                viewHolder = (ViewHolder)view.getTag();
            }

            viewHolder.tv1.setText(this.callerIDnames[position]);
            viewHolder.tv2.setText(this.callerIDnumbers[position]);
            viewHolder.tv3.setText(this.messageSentTimes[position]);

            // if message is unread, then make texts bold
            if (messageRead_list[position] == false){
                viewHolder.tv1.setTypeface(viewHolder.tv1.getTypeface(), Typeface.BOLD);
                viewHolder.tv2.setTypeface(viewHolder.tv2.getTypeface(), Typeface.BOLD);
                viewHolder.tv3.setTypeface(viewHolder.tv3.getTypeface(), Typeface.BOLD);
            }

            return view;
        }

I made a listview that has three textviews and a checkbox and put it into a BaseAdapter. What it has to do is if an item is at the 'unread' status, then make its textviews BOLD. However, I encountered two problems.

  1. the top item is always BOLD no matter it is read or unread.
  2. If I do scrolling, then items reloaded by scrolling start to be bold

I researched about it but couldn't find any good things for me. If anyone has an idea, would you please help me? Below is getView().


public View getView(final int position, View convertView, ViewGroup parent) {
            View view = convertView;
            ViewHolder viewHolder;

            if(convertView == null){ // check if convertView exists
                // get and inflate layout
                view = layoutInflater.inflate(R.layout.row, null);

                viewHolder = new ViewHolder();
                viewHolder.tv1 = (TextView)view.findViewById(R.id.list_callerIDname);
                viewHolder.tv2 = (TextView)view.findViewById(R.id.list_callerIDnumber);
                viewHolder.tv3 = (TextView)view.findViewById(R.id.list_messageSentTime);
                viewHolder.cb = (CheckBox)view.findViewById(R.id.checkBox1);

                // get each CheckBox into cb_array for future use
                this.cb_array[position] = (CheckBox)view.findViewById(R.id.checkBox1);

                // use it as a tag
                view.setTag(viewHolder);
            }
            else{
                viewHolder = (ViewHolder)view.getTag();
            }

            viewHolder.tv1.setText(this.callerIDnames[position]);
            viewHolder.tv2.setText(this.callerIDnumbers[position]);
            viewHolder.tv3.setText(this.messageSentTimes[position]);

            // if message is unread, then make texts bold
            if (messageRead_list[position] == false){
                viewHolder.tv1.setTypeface(viewHolder.tv1.getTypeface(), Typeface.BOLD);
                viewHolder.tv2.setTypeface(viewHolder.tv2.getTypeface(), Typeface.BOLD);
                viewHolder.tv3.setTypeface(viewHolder.tv3.getTypeface(), Typeface.BOLD);
            }

            return view;
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

套路撩心 2024-11-21 07:47:25

当 ListView 重用视图时,它在滚动之前可能是粗体的,现在应该是普通的。尝试在阅读消息时将字体显式设置为 PLAIN。

When the ListView reuses a view, it may have been bold before scrolling and should be plain now. Try explicitly setting the typeface to PLAIN when the message is read.

给妤﹃绝世温柔 2024-11-21 07:47:25

您必须在 setTypeFace() 中传递 null,而不是使用 getTypeFace()

getTypeFace() 不返回 null 时,样式将无法正确设置。

// if message is unread, then make texts bold
if (messageRead_list[position] == false){
    viewHolder.tv1.setTypeface(null, Typeface.BOLD);
    viewHolder.tv2.setTypeface(null, Typeface.BOLD);
    viewHolder.tv3.setTypeface(null, Typeface.BOLD);
} else {
    viewHolder.tv1.setTypeface(null, Typeface.NORMAL);
    viewHolder.tv2.setTypeface(null, Typeface.NORMAL);
    viewHolder.tv3.setTypeface(null, Typeface.NORMAL);
}

或者您可以直接使用它,这就是传递 null 时所做的事情。

setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));

setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));

You will have to pass null in setTypeFace() instead of using getTypeFace().

When getTypeFace() doesn't return null the style wont be set correctly.

// if message is unread, then make texts bold
if (messageRead_list[position] == false){
    viewHolder.tv1.setTypeface(null, Typeface.BOLD);
    viewHolder.tv2.setTypeface(null, Typeface.BOLD);
    viewHolder.tv3.setTypeface(null, Typeface.BOLD);
} else {
    viewHolder.tv1.setTypeface(null, Typeface.NORMAL);
    viewHolder.tv2.setTypeface(null, Typeface.NORMAL);
    viewHolder.tv3.setTypeface(null, Typeface.NORMAL);
}

Or you could use this directly which is what's done when you pass null.

setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));

and

setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
岁吢 2024-11-21 07:47:25

您应该通过实现 getItemViewType(intposition) 来使用两种类型的视图计数

,如下所示:

public int getViewTypeCount () {
    return 2;
}

和:

public int getItemViewType (int position) {
    if (messageRead_list[position] == false) return 0;
    return 1;
}

然后在创建项目时检查项目类型以设置正确的字体。

不过,这是黑客,我在其他情况下将类型设置为正常或默认,因为您的评论应该是解决方案。您确定 messageRead_list 正确吗?

You should use two types of view counts by implementing getItemViewType(int position)

like this:

public int getViewTypeCount () {
    return 2;
}

and this:

public int getItemViewType (int position) {
    if (messageRead_list[position] == false) return 0;
    return 1;
}

and then checking the item type when you create them to set the proper typeface.

this is hack though, I'm setting the type to normal or default when on else as your comment should be the solution. Are you certain that messageRead_list is correct?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文