Android的 - 头在ListView中消失? [英] Android - Header in ListView disappears?

查看:169
本文介绍了Android的 - 头在ListView中消失?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经经历了非常奇怪的错误最近......,只是不知道该怎么办......

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

我有一个标签片段的活动,这意味着我需要一个tabhost在底部,所以我用谷歌的API的例子,它通过TabHost管理片段(安培;经理) 几乎每个选项卡实际上是一个ListFragment,并给每个我添加一个标题为OnActivityCreated。

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".

现在奇怪的是:当我移动到标签(ListFragment)第一次,我可以看到头,但是一旦我谨从选项卡,之后将回到它的头不见了!

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 !!!

这是在code我使用:

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;
    }

}

我用这个初始化布尔值,不叫setListAdapter/和addHeader每次我加载片段的时间(否则你会得到一个讨厌的例外,说你设置适配器之后不能添加标题... )

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 ......我无言以对@这一点......

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

请帮忙:)

推荐答案

使用视图类型系统中 BaseAdapter 。使用 addHeaderView()包装适配器,并增加了不必要的,你并不需要一个查看的复杂性。根据适配器中的位置的 getItemViewType(INT)方法,使你可以区分查看类型。在你的 getView()方法,你可以检查是否位置是头部。例如:

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;
   }
}

这篇关于Android的 - 头在ListView中消失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆