Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView with first visible item having different layout

How can I make the first visible element of the ListView having a different layout from the others.

the first cell have a description

when is list is scrolled and 2nd cell is at first position the description of the 2nd cell is shown

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?

like image 930
null pointer Avatar asked Jan 25 '13 10:01

null pointer


2 Answers

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.

like image 156
null pointer Avatar answered Nov 15 '22 12:11

null pointer


Try this

int visibleposition = ListView.getFirstVisiblePosition();

now for this particular position change the background of the list item like this

like image 37
Swati Avatar answered Nov 15 '22 10:11

Swati