无法在我的 ListAdapter 中设置自定义视图
Trouble setting custom view in my ListAdapter
我有一个包含各种元素的列表视图,并且更新了很多次。
我想在一些随机位置之后添加一个分隔线,比如在第 3 个元素、第 7 个元素、第 17 个元素、第 19 个元素等之后
所以我设计了一个包含分隔符的 xml 文件,并使用自定义 SimpleAdapter
在这些位置上对其进行扩充。但是,当我 运行 我的代码时,分隔符被添加到奇怪的位置,使我的列表看起来太难看,有时会给出 Unfortunately, yourApp has stopped
错误
我以前从未使用过自定义适配器,也不知道我做错了什么。
这是我的简单代码,我添加了注释以进行解释,
public class SpecialAdapter extends SimpleAdapter {
Context context;
private LayoutInflater mInflater;
public SpecialAdapter(Context context, ArrayList<HashMap<String, Object>> newsList, int resource,String[] from, int[] to) {
super(context, newsList, resource, from, to);
this.context = context;
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
// TestApp.endPos holds the positions (it keep changing) after which i want to add
// separator
if (position == TestApp.endPos){
/* my Arraylist is of type HashMap<String, Object>
* I have added an empty element and add the seperator view in
* it.
*/
TestApp.arrayList.add(new HashMap<String, Object>());
// heads is an xml file of my separator
view = mInflater.inflate(R.layout.heads, null);
}
return view;
}
}
任何人都可以帮助我如何简化我的问题以及我做错了什么?
在您的 getView
方法中,您应该确定是否需要带有 seperator
的视图,然后为该项目扩充该视图:
if (view == null) {
if (showSeperatorItem)
view = _activity.getLayoutInflater().inflate(R.layout.item__row_layout_with_seperator, parent, false);
else
view = _activity.getLayoutInflater().inflate(R.layout.item__row_layout, parent, false);
}
super.getView
膨胀尽可能多的视图在一次可见。稍后它会重新使用它们。
由于您稍后会扩充自己的布局,因此它会缓存在顶部。最后 super.getView
开始在随机位置返回您的自定义视图。
为了克服这个问题,您需要告诉适配器有两种类型的视图,以便它分别缓存它们。
以下是示例方法(未经测试但应该有效):
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (position == TestApp.endPos) {
// Handle separator views manually
TestApp.arrayList.add(new HashMap<String, Object>());
// If separator is not yet cached, create the view
if (convertView == null) {
convertView = mInflater.inflate(R.layout.heads, null);
}
} else {
// Normal views are handled by the adapter
convertView = super.getView(position, convertView, parent);
}
return convertView;
}
@Override
public int getViewTypeCount() {
// There are 2 types: separator and normal
return 2;
}
@Override
public int getItemViewType(int position) {
if (position == TestApp.endPos) {
// Separator view
return 0;
} else {
// Normal view
return 1;
}
}
注意:有 2 种类型,意味着将缓存和重新使用 2 种类型的视图。
我有一个包含各种元素的列表视图,并且更新了很多次。 我想在一些随机位置之后添加一个分隔线,比如在第 3 个元素、第 7 个元素、第 17 个元素、第 19 个元素等之后
所以我设计了一个包含分隔符的 xml 文件,并使用自定义 SimpleAdapter
在这些位置上对其进行扩充。但是,当我 运行 我的代码时,分隔符被添加到奇怪的位置,使我的列表看起来太难看,有时会给出 Unfortunately, yourApp has stopped
错误
我以前从未使用过自定义适配器,也不知道我做错了什么。
这是我的简单代码,我添加了注释以进行解释,
public class SpecialAdapter extends SimpleAdapter {
Context context;
private LayoutInflater mInflater;
public SpecialAdapter(Context context, ArrayList<HashMap<String, Object>> newsList, int resource,String[] from, int[] to) {
super(context, newsList, resource, from, to);
this.context = context;
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
// TestApp.endPos holds the positions (it keep changing) after which i want to add
// separator
if (position == TestApp.endPos){
/* my Arraylist is of type HashMap<String, Object>
* I have added an empty element and add the seperator view in
* it.
*/
TestApp.arrayList.add(new HashMap<String, Object>());
// heads is an xml file of my separator
view = mInflater.inflate(R.layout.heads, null);
}
return view;
}
}
任何人都可以帮助我如何简化我的问题以及我做错了什么?
在您的 getView
方法中,您应该确定是否需要带有 seperator
的视图,然后为该项目扩充该视图:
if (view == null) {
if (showSeperatorItem)
view = _activity.getLayoutInflater().inflate(R.layout.item__row_layout_with_seperator, parent, false);
else
view = _activity.getLayoutInflater().inflate(R.layout.item__row_layout, parent, false);
}
super.getView
膨胀尽可能多的视图在一次可见。稍后它会重新使用它们。
由于您稍后会扩充自己的布局,因此它会缓存在顶部。最后 super.getView
开始在随机位置返回您的自定义视图。
为了克服这个问题,您需要告诉适配器有两种类型的视图,以便它分别缓存它们。
以下是示例方法(未经测试但应该有效):
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (position == TestApp.endPos) {
// Handle separator views manually
TestApp.arrayList.add(new HashMap<String, Object>());
// If separator is not yet cached, create the view
if (convertView == null) {
convertView = mInflater.inflate(R.layout.heads, null);
}
} else {
// Normal views are handled by the adapter
convertView = super.getView(position, convertView, parent);
}
return convertView;
}
@Override
public int getViewTypeCount() {
// There are 2 types: separator and normal
return 2;
}
@Override
public int getItemViewType(int position) {
if (position == TestApp.endPos) {
// Separator view
return 0;
} else {
// Normal view
return 1;
}
}
注意:有 2 种类型,意味着将缓存和重新使用 2 种类型的视图。