Changing View of items in an Adapter linked to a ListView during app execution
I'm trying to give the users of my app the option to change how they want their results displayed.
I've created a different layout item for each view and extended from BaseAdapter
like so:
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;
}
Calling notifyDataSetChanged()
is (observable through debug) refreshing the view because the new view is being inflated and returned from getView()
method.
However the view on screen is not changing...
Is there something I'm missing ?
The problem here is that ListView may cache already inflated View
s that are of old "format". You need to somehow reset this View "cache". For this you can use next trick:
mListView.setAdapter(mListView.getAdapter());
Instead of calling notifyDataSetChanged()
.
精彩评论