Rof*_*ion 38 android android-adapterview
我在使用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());
Run Code Online (Sandbox Code Playgroud)
网络适配器不包含任何项目:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
我也尝试调用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>
Run Code Online (Sandbox Code Playgroud)
我真的不知道我做错了什么.我也阅读了各种教程,但没有一个能够解决任何问题.
Kar*_*ran 93
您需要在添加了AdapterView的布局中添加相同的视图.
AdapterView仅根据适配器中的内容更改其可见性.
编辑:
以下布局和代码工作正常:
<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>Run Code Online (Sandbox Code Playgroud)
代码:
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() ) );Run Code Online (Sandbox Code Playgroud)
Pac*_*Sky 20
Karan的回答是现场的.AdapterView确实仅通过更改自己的可见性与空视图的可见性来显示空视图.但是,空视图布局不一定需要与AdapterView位于同一XML文件中.我发现将两者分开是有用的,因为我使用单个布局XML文件(包含GridView)进行多个活动,但我想为每个活动指定不同的emptyView布局.
关键是使用addContentView将空视图添加到活动.请记住在调用setContentView指定主AdapterView 之后执行此操作.
我的代码(在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));
Run Code Online (Sandbox Code Playgroud)
在每个子活动中,我重写getIdOfEmptyView,如下所示:
@Override
protected int getIdOfEmptyView()
{
return R.layout.emptyView_Favorites;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
26329 次 |
| 最近记录: |