Android AdapterView 中 setEmtpyView 的正确使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2807253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Correct use of setEmtpyView in AdapterView
提问by RoflcoptrException
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:
我在使用 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());
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:
我也尝试调用 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 really don't know what I'm doing wrong. I also read various tutorials, but none of them metnioned any problems.
我真的不知道我做错了什么。我也阅读了各种教程,但没有一个提到任何问题。
回答by Karan
You need to add this same view in the layout in which you have added the AdapterView.
您需要在已添加 AdapterView 的布局中添加相同的视图。
AdapterView only changes its visibility based on the contents in the adapter.
AdapterView 仅根据适配器中的内容更改其可见性。
EDITED :
Following layout and code works fine :
编辑:
以下布局和代码工作正常:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_width="fill_parent"
android:layout_height="300dip"
android:id="@+id/list_view" />
<TextView
android:id="@+id/empty_list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List view is empty"
android:visibility="gone" />
</LinearLayout>
Code :
代码 :
ListView listView = (ListView) findViewById( R.id.list_view );
listView.setEmptyView( findViewById( R.id.empty_list_view ) );
listView.setAdapter( new ArrayAdapter( this, R.layout.selected_spinner_view, new ArrayList() ) );
回答by PacificSky
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.
卡兰的回答是准确的。AdapterView 确实通过仅更改其自己的可见性与空视图的可见性来显示空视图。但是,空视图布局不一定需要与 AdapterView 位于同一个 XML 文件中。我发现将两者分开很有用,因为我对多个活动使用单个布局 XML 文件(包含一个 GridView),但我想为每个活动指定不同的 emptyView 布局。
The key is to add the empty view to the activity with addContentView. Remember to do this afteryou have called setContentView specifying your main AdapterView.
关键是用 addContentView 将空视图添加到活动中。请记住在调用 setContentView 指定主 AdapterView后执行此操作。
My code (in AbstractBaseActivity.onCreate) :
我的代码(在 AbstractBaseActivity.onCreate 中):
View mainView = inflater.inflate(R.layout.imagegrid, null);
GridView gridView = (GridView)mainView.findViewById(R.id.gridView);
View emptyView = inflater.inflate(getIdOfEmptyView(), null, false);
gridView.setEmptyView(emptyView);
// Any additional processing on mainView
setContentView(mainView);
addContentView(emptyView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
In each child activity I override getIdOfEmptyView as follows:
在每个子活动中,我按如下方式覆盖 getIdOfEmptyView:
@Override
protected int getIdOfEmptyView()
{
return R.layout.emptyView_Favorites;
}
回答by skpal202
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.
您可以定义外部 XML 布局文件,对其进行扩充并将其设置为空视图。唯一要记住的是设置膨胀视图的父级。
View emptyView = inflater.inflate(R.layout.id_of_your_layoout_file, parent, false);
gridView.setEmptyView(emptyView);
or
或者
listView.setEmptyView(emptyView);
回答by chipwilson
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.
PacificSky 的回答几乎让我达到了我所需要的,但是addContentView
即使在从活动中删除包含 ListView 的片段之后,将空视图添加到活动也会导致空视图保留在活动的容器中。此外,活动的滑入菜单显示在空视图下或与空视图混合。
The solution for me was to add the empty view to the container passed in as a parameter to the fragment's onCreateView
:
我的解决方案是将空视图添加到作为参数传入片段的容器中onCreateView
:
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
mFragmentContainer = container;
mListView = (ListView) mLayout.findViewById(R.id.list_view);
mEmptyListView = inflater.inflate(R.layout.empty_list, null, false);
container.addView(mEmptyListView);
mListView.setEmptyView(mEmptyListView);
mListAdapter = new MyListAdapter(getActivity(), mList);
mListView.setAdapter(mListAdapter);
And remove the empty view from the container in the fragment's onDestroyView
:
并从片段的容器中删除空视图onDestroyView
:
@Override
public void onDestroyView(){
super.onDestroyView();
mFragmentContainer.removeView(mEmptyListView);
}
回答by ratanak pek
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;
对于我的情况,调用了 gridView.setEmptyView(emptyView) 但它没有显示 emptyview。除非我调用gridView.setVisibility(View.VISIBLE),所以它正常显示;