Add a custom layout before the list displayed by t

2019-07-18 17:10发布

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

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

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

4条回答
Emotional °昔
2楼-- · 2019-07-18 17:43

Write an ArrayAdapter extending ArrayAdapter then do this inside getView method...

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    Contact contact = getItem(position);
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.contact_list_item, parent, false);
    }
//Do whatever u need to do with that View...
    return convertView
}
查看更多
姐就是有狂的资本
3楼-- · 2019-07-18 17:47

You can create a custom view with the specifics you need, and then then, for the method "addHeaderView" you can assign you view.

eg:

View header = getLayoutInflater().inflate(R.layout.header, null); 
ListView listView = getListView();  
listView.addHeaderView(header);  
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice,android.R.id.text1, names));
查看更多
欢心
4楼-- · 2019-07-18 17:54

In your adapter, inside your getView(), you can load your header layout while position is 0.

Check out my code.

public View getView(int position, View convertView, ViewGroup parent) {
if(position==0)
{
 //view 1
}
else
{
Rest of the list elements
}
查看更多
【Aperson】
5楼-- · 2019-07-18 18:06

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);
}
查看更多
登录 后发表回答