如何访问ListView中的Header View?

时间:2014-07-23 01:28:37

标签: android-listview

有一种方法

  

addHeaderView

可以添加几个标题,我可以反击它们,但我不知道如何才能获得视图。没有方法getHeaderView(int index),我错过了什么吗?

我的回答可能是在ListView标签中缓存视图,但也许你可以给我一些更明显的东西。

1 个答案:

答案 0 :(得分:0)

不存在本机实现,但您可以使用适配器来操作标题,如下所示:

Section header listview

Listview with section header

 public class MyBaseAdapter extends BaseAdapter {

   public class PojoHeader {}

   public class PojoView {}

   private final int TYPE_HEADER     = 0;
   private final int TYPE_VIEW       = 0;
   private final int VIEW_TYPE_COUNT = 2;

   List<Object>      mList;

   @Override
   public int getCount() {
       return this.mList.size();
   }

   @Override
   public Object getItem( final int position ) {
       return this.mList.get( position );
   }

   @Override
   public long getItemId( final int position ) {
       return position;
   }

   @Override
   public int getItemViewType( final int position ) {

       if ( this.mList.get( position ) instanceof PojoHeader ) {
           return this.TYPE_HEADER;
       }

       else if ( this.mList.get( position ) instanceof PojoHeader ) {
           return this.TYPE_VIEW;
       }

       return super.getItemViewType( position );
   }

   @Override
   public View getView( final int position, final View convertView, final ViewGroup parent ) {
       // Your view
       return null;
   }

   @Override
   public int getViewTypeCount() {
       return this.VIEW_TYPE_COUNT;
   }

   @Override
   public boolean isEnabled( final int position ) {

       if ( this.mList.get( position ) instanceof PojoHeader ) {
           return false;
       }

       else if ( this.mList.get( position ) instanceof PojoHeader ) {
           return true;
       }

       return true;
   }
}