Android ListView with complex header

梦想的初衷 提交于 2019-11-29 07:15:01
Ivo van der Wijk

I've attempted to reimplement my scrolling listview from scratch, roughly as follows:

  • a main.xml with a background image and a single ListView, with a 20dip margin and some specific styling:

    <item name="android:cacheColorHint">#00000000</item>
    <item name="android:scrollbars">@null</item>
    <item name="android:background">@android:color/transparent</item>
    
  • a headerlayout.xml containing a linear layout (vertical), containing a TextView, ImageView and another TextView. The first TextView is styled with rounded topcorners, both textviews are styled transparent, and a clicklistener on the image.

  • (a similarly styled footer)
  • a custom entry for each listitem with a solid white background.

The header/footer are added roughly as follows

ListView list = (ListView) findViewById(R.id.list);
View header1 =  getLayoutInflater().inflate(R.layout.listheader, null, false);
View footer = getLayoutInflater().inflate(R.layout.listfooter, null, false);
ImageView image = (ImageView) header1.findViewById(R.id.image);

list.addHeaderView(header1, null, false);
list.addFooterView(footer, null, false);
list.setAdapter(new MenuAdapter());

None of this is actually truely special. It's mostly a matter of doing things in the right order, stripped down and properly styled.

My suggestion would be that you create a custom view for this overly complicated list view item. Apparently this is what Android devs at Google IO too suggested. If you can do it in custom view, all your problems will be solved for.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!