Android 在应用程序执行期间更改链接到ListView的适配器中项目的视图
我正试图给我的应用程序的用户一个选项,让他们改变他们希望结果显示的方式 我为每个视图创建了不同的布局项,并从Android 在应用程序执行期间更改链接到ListView的适配器中项目的视图,android,listview,baseadapter,Android,Listview,Baseadapter,我正试图给我的应用程序的用户一个选项,让他们改变他们希望结果显示的方式 我为每个视图创建了不同的布局项,并从BaseAdapter进行了扩展,如下所示: public View getView(int index, View recycledCompatibleView, ViewGroup parent) { // Just in case the view can be reused View toReturn = recycledCompatibleView; i
BaseAdapter
进行了扩展,如下所示:
public View getView(int index, View recycledCompatibleView, ViewGroup parent)
{
// Just in case the view can be reused
View toReturn = recycledCompatibleView;
if(toReturn == null)
{
LayoutInflater inflator = LayoutInflater.from(parent.getContext());
toReturn = inflator.inflate(layoutId, null);
}
...
}
public void setResultsListStyle(int layoutId)
{
this.layoutId = layoutId;
}
调用notifyDataSetChanged()
是(通过调试可以观察到)刷新视图,因为新视图正在膨胀并从getView()
方法返回
但是屏幕上的视图没有改变
有什么我遗漏的吗?这里的问题是ListView可能会缓存已经膨胀的
视图
的旧“格式”。您需要以某种方式重置此视图“缓存”。为此,您可以使用下一个技巧:
mListView.setAdapter(mListView.getAdapter());
而不是调用notifyDataSetChanged()