Android - 自动回收列表视图元素?
我创建了一个具有自定义 SimpleCursorAdapter 的列表视图。我想在列表中的第一个元素中放置一个标题。屏幕上一次可显示 8 个视图。当我向下滚动到第九个视图时,会出现第一个元素的标题。至少我相信这就是正在发生的事情。我删除了列表视图上方的一个按钮,允许所有元素出现在屏幕上,并且只有第一个元素具有标题。
我相信我每次都在强迫一种新的观点被夸大。我已经阅读了一些关于 ConvertView 的内容,它似乎是您必须手动实现的东西。
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final LayoutInflater inflater = LayoutInflater.from(context);
int position = cursor.getPosition();
View v;
v = inflater.inflate(R.layout.roster_lv_row_entry_with_header, parent, false);
if(position > 0)
v = inflater.inflate(R.layout.roster_lv_row_entry_no_header, parent, false);
return v;
I created a listview that has a custom SimpleCursorAdapter. I want to place a header in the first element in the list. 8 views fit on the screen at a time. When I scroll down to the ninth view, the header of the 1st element appears. At least I believe that is what is happening. I removed a button above the listview allowing all of the elements to appear on screen and only the first element had the header.
I believe I am forcing a new view to be inflated each time. I have read up a bit on convertview and it appears to be something that you have to implement manually.
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final LayoutInflater inflater = LayoutInflater.from(context);
int position = cursor.getPosition();
View v;
v = inflater.inflate(R.layout.roster_lv_row_entry_with_header, parent, false);
if(position > 0)
v = inflater.inflate(R.layout.roster_lv_row_entry_no_header, parent, false);
return v;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该重写适配器类中的
(final intposition, View ConvertView, ViewGroupparent)
方法,并将convertView
参数分配给新值(如果需要,但如果类型正确,最好使用它,并根据
yourListData.get(position)
使用正确的数据填充它,其中yourListData
是一个List
扩展。)You shoul override the
(final int position, View convertView, ViewGroup parent)
method in your adapter class, andconvertView
parameter anew value (if necessary, but better just use it if it's the right type, and fill it with the proper data based on
yourListData.get(position)
, whereyourListData
is e.g. aList<?>
extension.)