null pointer
null pointer

Reputation: 5914

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?

Upvotes: 10

Views: 6060

Answers (4)

null pointer
null pointer

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

Swati
Swati

Reputation: 1179

Try this

int visibleposition = ListView.getFirstVisiblePosition();

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

Upvotes: 2

Krit
Krit

Reputation: 620

You can try some method such as

getFirstVisiblePosition

Or this might answer your problem

Upvotes: 0

Vineet
Vineet

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

Related Questions