重置 ListView 中的视图
我的ListView
是一个简单的ListView,但是他有问题。
我正在使用一个名为 adapter
的 SimpleCursorAdapter
,我希望 TextView 变为粗体,并且在单击某个项目时背景发生变化。我还希望之前受影响的其他 ListView 项目能够自行重置。
以下是 onListItemClick
事件的代码。此代码重置可见列表项。并正确地将所选项目加粗。然而,当用户滚动随机 TextViews
时,我认为它是粗体的,因为视图正在被回收。
因此,在 onClick 我想重置所有先前单击的 textView 并将单击的项目加粗。
我的代码没有按预期工作:
@Override
public void onListItemClick(ListView listView, View view, int position, long id)
{
super.onListItemClick(listView, view, position, id);
//Tablet
for (int i = 0; i < adapter.getCount(); i++)
{
View mView = listView.getChildAt(i);
if (mView != null)
{
//Reset Bold text to default
TextView mtv = (TextView)mView.findViewById(R.id.Name);
mtv.setTypeface(null, Typeface.BOLD);
LinearLayout ll = (LinearLayout)mView.findViewById(R.id.itemlayout);
ll.setBackgroundDrawable(null);
}
}
TextView tv = (TextView)view.findViewById(R.id.Name);
tv.setTypeface(null, Typeface.NORMAL);
LinearLayout ll = (LinearLayout)view.findViewById(R.id.itemlayout);
ll.setBackgroundResource(R.drawable.dashboard_list_selector);
}
}
My ListView
is a simple ListView, but he has problems.
I am using a SimpleCursorAdapter
called adapter
and I want the TextView to bold and the background to change when an item is clicked. I also want the other previously affected ListView item to reset itself.
Below is the code for the onListItemClick
event. This code resets visible list items. and correctly bolds the selected item. However, when the user scrolls random TextViews
are bolded, I presume because views are being recycled.
So, onClick I want to reset any previously clicked textViews and bold the clicked item.
My not working as expected code:
@Override
public void onListItemClick(ListView listView, View view, int position, long id)
{
super.onListItemClick(listView, view, position, id);
//Tablet
for (int i = 0; i < adapter.getCount(); i++)
{
View mView = listView.getChildAt(i);
if (mView != null)
{
//Reset Bold text to default
TextView mtv = (TextView)mView.findViewById(R.id.Name);
mtv.setTypeface(null, Typeface.BOLD);
LinearLayout ll = (LinearLayout)mView.findViewById(R.id.itemlayout);
ll.setBackgroundDrawable(null);
}
}
TextView tv = (TextView)view.findViewById(R.id.Name);
tv.setTypeface(null, Typeface.NORMAL);
LinearLayout ll = (LinearLayout)view.findViewById(R.id.itemlayout);
ll.setBackgroundResource(R.drawable.dashboard_list_selector);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
发生的情况是正常的,正如您所说,回收器使用屏幕上的旧视图而不是创建新视图。您需要做的是更改适配器,您需要的逻辑也必须在 getView 方法中实现。此外,您需要适配器支持不同类型的项目才能使用不同的背景。
看一下这个示例,该示例有不同的目标,但它应用了您需要的解决方案。
抱歉,没有快速的解决方案。如果有帮助请告诉我。
What happens is normal, as you said the recycler uses old views off the screen instead of creating new ones. What you need to do is to change your adapter, the logic you need has to be implemented also in your getView method. Moreover you need your adapter to support different type of items in order to use different backgrounds.
Take a look to this example, the example has a different goal but it applies the solution you need.
Sorry but there is not a quick solution. Let me know if it helps.
正如之前指出的,您遇到的问题是基于 Android 重用视图来加速列表视图的滚动。我过去解决这个问题的方法是维护一个并行数组,用于跟踪该项目是否应加粗。在 getView 代码中,检查 items 位置中的标志是否为 true 将其加粗,否则将其取消加粗(您必须将其设置回默认状态)。然后,当用户单击某个项目时,切换标志并调用
notifyDataSetChanged
。这将强制重绘并将所选项目加粗。As was previously pointed out the problem you are having is based on Android reusing views to speed the scrolling of listview. The I have fixed this in the past is to maintain a parallel array that tracks if the item should be bolded. In the getView code, check the flag in the items position if it is true bold it, otherwise unbold it (you have to set it back to its default state). Then when a user clicks an item switch the flag and call
notifyDataSetChanged
. This will force a redraw and bold the selected item.