Reputation: 5914
How can I make the first visible element of the ListView
having a different layout
from the others.
I have created a custom adapter with 2 different layouts. Now, how can I make the layout
change when the item comes in the first position
?
Upvotes: 10
Views: 6060
Reputation: 5914
to get the the first item of the listview having a different layout the following code can be used. But the listview don't move when the items in the list are less than the no of visible items.
list.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
if(firstItem != firstVisibleItem)
{
firstItem = firstVisibleItem;
adapter.notifyDataSetChanged();
}
}
});
and inside the adapter class check if the postion
is same as firstItem
if its equal then show the detals layout.
Upvotes: 4
Reputation: 1179
Try this
int visibleposition = ListView.getFirstVisiblePosition();
now for this particular position change the background of the list item like this
Upvotes: 2
Reputation: 620
You can try some method such as
getFirstVisiblePosition
Or this might answer your problem
Upvotes: 0
Reputation: 151
In getView(), you might inflate a different layout for different position. do something like
if(position==0){
inflate(first_layout);
else{
inflate(other_layout);
Upvotes: 0