Android - Header in ListView disappears?

后端 未结 1 1939
长发绾君心
长发绾君心 2021-01-25 06:14

I\'ve been experiencing a very weird bug lately... and simply don\'t know what to do...

I have a \"Tabbed-Fragment-Activity\", which means I needed a tabhost on the bott

1条回答
  •  北荒
    北荒 (楼主)
    2021-01-25 06:58

    Use the view typing system in BaseAdapter. Using addHeaderView() wraps your adapter and adds unnecessary complexity that you don't need for a single View. The getItemViewType(int) method let's you differentiate View types based on position within the adapter. In your getView() method you can check to see if the position is for the header. For example:

    public class YourAdapter extends BaseAdapter {
       private static final int HEADER = 0;
       private static final int CELL   = 1;
    
       @Override public int getItemViewType(int position) {
          if (position == 0) {
             return HEADER;
          }
          return CELL;
       }
    
       @Override public int getViewTypeCount() {
          return 2;
       }
    
       @Override
       public View getView(int position, View convertView, ViewGroup parent) {
          if (getItemViewType(position) == HEADER) {
             // do header stuff...
             return yourHeaderView;
          }
    
          // do non header stuff...
          return yourNonHeaderView;
       }
    }
    

    0 讨论(0)
提交回复
热议问题