I intend to create a listview with different layout for each visible row. I also set setStackFromBottom(true)
.
I know I can use int getItemViewType(int position)
in View getView(int position, View convertView, ViewGroup parent)
but it uses the fix position and not based on visible positions.
I'd like to use the listview as the following concept shows:
If I scroll down, the next item should replace the previous one and use its large layout. I tried it with the following code, but it uses fix position so always the last item in the list is the largest, not the last visible item.
@Override
public int getItemViewType(int position)
{
if(position==values.length-1)
return 1;
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = null;
type=getItemViewType(position);
if (type == 0)
rowView = inflater.inflate(R.layout.list_item, parent, false);
else
rowView = inflater.inflate(R.layout.list_item2, parent, false);
return rowView;
}
I reckon, I should get the position of the first and last visible item inside the Adapter, but I don't know how to do it.
Can I somehow create a listview that follows the aforementioned concept? Can I get the correct positions?
You can get a lot of info by setting a scroll listener on the ListView:
mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
// firstVisibleItem + visibleItemCount = last item
}
}
However, I can't think of how you could use this info effectively. Even if you change getView() to return a larger View when its position is the same as the last item, you can't easily resize the views as they move up, because getView() won't be called again for items already displayed, unless you call notifyDataSetChanged() on the adapter repeatedly, which would kill performance.
My though is to add another View below the ListView that will display the larger View for the item below the last one in the ListView. Make it look like the last row in the ListView. You could change that view in the onScroll() method.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments