AdapterView中setEmtpyView的正确使用
我在使用 setEmptyView 方法时确实遇到了麻烦。我尝试在GridView和ListView中实现它,但它们都不起作用。这里是一个示例代码块:
networkGames = (GridView) baseLayer.findViewById(R.id.all_game_grid_network);
networkGames.setBackgroundResource(R.drawable.game_border);
networkGames.setSelector(R.drawable.game_active_border);
networkGames.setOnItemClickListener(new NetworkGameListener());
networkGames.setEmptyView(View.inflate(baseLayer, R.drawable.no_network_games, null));
networkGames.setAdapter(new NetworkAdapter());
网络适配器不包含任何项目:
private class NetworkAdapter extends BaseAdapter {
/* (non-Javadoc)
* @see android.widget.Adapter#getCount()
*/
@Override
public int getCount() {
return 0;
}
/* (non-Javadoc)
* @see android.widget.Adapter#getItem(int)
*/
@Override
public Object getItem(int position) {
return null;
}
/* (non-Javadoc)
* @see android.widget.Adapter#getItemId(int)
*/
@Override
public long getItemId(int position) {
return 0;
}
/* (non-Javadoc)
* @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return null;
}
}
我还尝试调用 networkGames.setAdapter(null),但这也不起作用。我的 emtpyView 看起来像这样:
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:text="There are currently no network games available. Start a new one."
android:id="@+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
</TextView>
</LinearLayout>
我真的不知道我做错了什么。我也阅读了各种教程,但没有一个提到任何问题。
I'm really having trouble using the setEmptyView method. I tried it to implement it in GridView and ListView, but both of them didnt work. Here a sample codeblock:
networkGames = (GridView) baseLayer.findViewById(R.id.all_game_grid_network);
networkGames.setBackgroundResource(R.drawable.game_border);
networkGames.setSelector(R.drawable.game_active_border);
networkGames.setOnItemClickListener(new NetworkGameListener());
networkGames.setEmptyView(View.inflate(baseLayer, R.drawable.no_network_games, null));
networkGames.setAdapter(new NetworkAdapter());
The network adapter contains no items:
private class NetworkAdapter extends BaseAdapter {
/* (non-Javadoc)
* @see android.widget.Adapter#getCount()
*/
@Override
public int getCount() {
return 0;
}
/* (non-Javadoc)
* @see android.widget.Adapter#getItem(int)
*/
@Override
public Object getItem(int position) {
return null;
}
/* (non-Javadoc)
* @see android.widget.Adapter#getItemId(int)
*/
@Override
public long getItemId(int position) {
return 0;
}
/* (non-Javadoc)
* @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return null;
}
}
I also tried to call networkGames.setAdapter(null), but this doesnt work either. My emtpyView looks like this:
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:text="There are currently no network games available. Start a new one."
android:id="@+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
</TextView>
</LinearLayout>
I really don't know what I'm doing wrong. I also read various tutorials, but none of them metnioned any problems.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要在已添加 AdapterView 的布局中添加相同的视图。
AdapterView 仅根据适配器中的内容更改其可见性。
已编辑:
以下布局和代码工作正常:
代码:
You need to add this same view in the layout in which you have added the AdapterView.
AdapterView only changes its visibility based on the contents in the adapter.
EDITED :
Following layout and code works fine :
Code :
卡兰的回答很准确。 AdapterView 确实通过仅更改其自身的可见性与空视图的可见性来显示空视图。但是,空视图布局不一定需要与 AdapterView 位于同一个 XML 文件中。我发现将两者分开很有用,因为我对多个活动使用单个布局 XML 文件(包含 GridView),但我想为每个活动指定不同的 emptyView 布局。
关键是使用 addContentView 将空视图添加到活动中。请记住在调用 setContentView 并指定主 AdapterView 之后执行此操作。
我的代码(在 AbstractBaseActivity.onCreate 中):
在每个子活动中,我重写 getIdOfEmptyView 如下:
Karan's answer is spot-on. The AdapterView indeed displays the empty view by merely changing its own visibility vs the empty view's visibility. However, the empty view layout does not necessarily need to be in the same XML file as that of the AdapterView. I found it useful to separate the two because I use a single layout XML file (containing a GridView) for multiple activities, but I wanted to specify different emptyView layouts for each of them.
The key is to add the empty view to the activity with addContentView. Remember to do this after you have called setContentView specifying your main AdapterView.
My code (in AbstractBaseActivity.onCreate) :
In each child activity I override getIdOfEmptyView as follows:
您可以定义外部 XML 布局文件、膨胀并将其设置为空视图。
唯一要记住的是设置膨胀视图的父级。
或者
You can define external XML layout files, inflate and set it as an empty view.
Only thing to remember is to set the parent of the inflated view.
or
PacificSky 的答案几乎满足了我的需要,但是使用
addContentView
将空视图添加到活动中会导致空视图保留在活动的容器中,即使从活动中删除包含 ListView 的片段也是如此。此外,活动的滑入菜单显示在空视图下方或与空视图混合。对我来说,解决方案是将空视图添加到作为参数传递给片段的
onCreateView
的容器中:并从片段的
onDestroyView
中的容器中删除空视图:PacificSky's answer got me almost to what I needed, but adding the empty view to the activity with
addContentView
caused the empty view to remain in the activity's container even after removing the fragment containing the ListView from the activity. Additionally, the slide in menu for the activity displayed under or mixed in with the empty view.The solution for me was to add the empty view to the container passed in as a parameter to the fragment's
onCreateView
:And remove the empty view from the container in the fragment's
onDestroyView
:就我而言,
gridView.setEmptyView(emptyView)
被调用但它不显示空视图。
除非我调用了gridView.setVisibility(View.VISIBLE),所以它显示正常;
For my case,
gridView.setEmptyView(emptyView)
was called but it's not show the emptyview.
Unless I called gridView.setVisibility(View.VISIBLE), so it appear normally;