android:getView()问题 - 它在滚动时更改项目
我制作了一个包含三个文本视图和一个复选框的列表视图,并将其放入 BaseAdapter 中。它要做的是,如果某个项目处于“未读”状态,则将其文本视图设为粗体。然而,我遇到了两个问题。
- 无论已读或未读,顶部项目始终为粗体。
- 如果我滚动,那么通过滚动重新加载的项目开始变得粗体,
我对此进行了研究,但找不到任何对我有用的东西。如果有人有想法,请帮助我吗?下面是 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.
- the top item is always BOLD no matter it is read or unread.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当 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.
您必须在
setTypeFace()
中传递null
,而不是使用getTypeFace()
。当
getTypeFace()
不返回null
时,样式将无法正确设置。或者您可以直接使用它,这就是传递
null
时所做的事情。和
You will have to pass
null
insetTypeFace()
instead of usinggetTypeFace()
.When
getTypeFace()
doesn't returnnull
the style wont be set correctly.Or you could use this directly which is what's done when you pass
null
.and
您应该通过实现 getItemViewType(intposition) 来使用两种类型的视图计数
,如下所示:
和:
然后在创建项目时检查项目类型以设置正确的字体。
不过,这是黑客,我在其他情况下将类型设置为正常或默认,因为您的评论应该是解决方案。您确定 messageRead_list 正确吗?
You should use two types of view counts by implementing getItemViewType(int position)
like this:
and this:
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?