开发者

Is there a way to specify a Layout to use for a ListView when the adapter is empty?

开发者 https://www.devze.com 2023-03-02 16:20 出处:网络
I have a complex empty view in a layout, with an icon, text, button, etc. It is easy to select a view within my layout.xml to use when the listview is empty, similar to

I have a complex empty view in a layout, with an icon, text, button, etc.

It is easy to select a view within my layout.xml to use when the listview is empty, similar to

getListView().setEmptyView(findViewById(R.id.empty));

This code sets the empty view works just fine when it resides in the layout.xml file.

Now I want to refactor this view into its own empty.xml layout file, and have coded it similar to the following:

// Setup the empty layout.xml
LayoutInflater vi = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vlEmpty = vi.inflate(R.layout.empty, null);

// Find the empty layout view
vEmpty = vlEmpty.findViewById(R.id.llEmpty);
vEmpty.setOnClickListener(ocl);

// Find the ListView
vListView = (ListView) findViewById(R.id.lvWords);
vListView.setEmptyView(vEmpty);

The problem is that the details within llEmpty never show up; The exact same layout and view works withing the main layout, just not refactored into its own xm开发者_运维百科l file.

Has anyone got something like this to work?


You might need to pass the proper context to the inflater:

vListView = (ListView) findViewById(R.id.lvWords);
View vlEmpty = vi.inflate(R.layout.empty, (ViewGroup)vListView.getParent());

which (should) make them both live in the same root view. It may be sufficient to just pass the root view of the parent activity.

Let me know if that works.


I doubt that setEmptyView() automatically makes the supplied View a child of any container in your activity.

Personally, I'd just use the <include> element rather than inflating it separately. But, if you really want to inflate it separately, the answer that Femi posted while I was writing this may work, depending on what the ListView's parent is.

0

精彩评论

暂无评论...
验证码 换一张
取 消