1

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 bottom so I used google's API example, which manages fragments via the TabHost (& Manager) Almost each tab is actually a ListFragment and to each I add an header at "OnActivityCreated".

Now the weird thing is : When i move to a tab (ListFragment) the first time, I can see the header, but once I move from the tab and afterwards move back to it, the header is GONE !!!

This is the code I'm using :

private boolean initialized = false;
private TextView m_Header = null; 


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    String listTitle = "HELLO HUMAN"
    if(m_Header == null && !Helpers.isNullOrBlank(listTitle))
    {
        m_Header = (TextView)inflater.inflate(R.layout.newslist_header, null, false);
        m_Header.setText(listTitle);
    }


    return super.onCreateView(inflater, container, savedInstanceState);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if(!initialized)
    {
        ListView list = getListView();
        if(m_Header != null)
        {
            list.addHeaderView(m_Header);
        }

        this.m_adapter = new SomeAdapter();
        setListAdapter(this.m_adapter);     
        registerForContextMenu(list);
        this.initialized = true;
    }

}

I'm using this "initialized" boolean as to not call "setListAdapter"/"addHeader" each time I load the fragment (otherwise you get a nasty exception saying you can't add header after setting the adapter...)

Errr... i'm clueless @ this point...

please help :)

1 Answers1

2

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;
   }
}
Christopher Perry
  • 38,891
  • 43
  • 145
  • 187