简体   繁体   中英

How to show image in first row of Android ListView when loading ListView?

There is a ListView and list_row xml which includes LinearLayout with background image.The requirement is to show the image in first row of ListView when loading data.And subsequently show and hide the image when tap on the rows of ListView .

ViewGroup vg = (ViewGroup) ListView.getAdapter().getView(0, null, null);
TableRow selectedRow = (TableRow) vg.getChildAt(0);
LinearLayout ll = (LinearLayout) selectedRow.getChildAt(0);
ll.setVisibility(View.VISIBLE);

I used the above source code and try to do it.But this is not working and does not return any null objects.Show and hide part is working on using ItemClickListner .But I need to show the image when loading data. I try to do it in Adapter class and I was able to show the image.But I couldn't hide it when tap on other rows of ListView .How can I fix this problem? Thanks in Advance.

For this, I would suggest you to use ListView.addHeaderView(v) method. It will allow you to add a view as header of ListView (which will always be on top of other list items). Afterwards, when you've implemented ItemClickListner , simply call ListView.removeHeaderView(v) to remove that header view from list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM