由ListFragment的适配器显示列表前添加自定义布局 [英] Add a custom layout before the list displayed by the adapter of a ListFragment

查看:439
本文介绍了由ListFragment的适配器显示列表前添加自定义布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ListFragment 显示列表。都好。我希望把一些格式良好的TextView preceding名单。结果
我试图与一个TextView创建线性布局和增加它作为一个报头添加到列表中,但它不工作。结果
以下不工作:

I have a ListFragment that displays a list. All good. I wanted to put some well formatted textview preceding the list.
I tried creating a linear layout with a textview and adding it as a header to the list but it does not work.
The following does work:

TextView tv = new TextView(getActivity());
 tv.setText("Some title with comments");
 getListView().addHeaderView(tv);
 tv.setGravity(Gravity.LEFT);
 tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);

我得到的列表前的文本,但我想实际设置的布局,而不是因为我想有2种不同的字体(它就像一个标题/字幕大字体和小说明较小的字体)。文字< BR>
我该怎么办呢?结果
我没有与定义列表视图的布局。我只有一个 ListFragment 和我的自定义listAdapter

I get the text before the list but I would like to actually set a layout instead as I would like text with 2 different fonts (it is like a title/caption with big font and a small description with smaller font).
How can I do that?
I don’t have any layout with list view defined. I only have a ListFragment and my custom listAdapter

更新:结果
我也试过,没有工作:

Update:
What I also tried and did not work:

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.headerView = inflater.inflate(R.layout.my_list_header, container, false);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

和后来设置适配器时:结果
getListView()addHeaderView(this.headerView); 结果
但我得到的例外

And later when setting the adapter:
getListView().addHeaderView(this.headerView);
But I got exceptions

推荐答案

下面是步骤在ListFragment适当添加标题:

Here are the steps for properly adding a header in a ListFragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      View list_root = inflater.inflate(R.layout.fragment_list, null);
      // Get the list header - to be added later in the lifecycle
      // during onActivityCreated()
      mheaderView = inflater.inflate(R.layout.my_list_header, null);
      return list_root;

    }


@Override
public void onActivityCreated(Bundle savedInstanceState) {
     super.onActivityCreated(savedInstanceState);
     if (mheaderView != null)  this.getListView().addHeaderView(headerView);
     // Don't forget to now call setListAdapter()
     this.setListAdapter(listAdapter);
}

@Override
public void onDestroyView()
{
    super.onDestroyView();

    // free adapter
    setListAdapter(null);
}

这篇关于由ListFragment的适配器显示列表前添加自定义布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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