AdapterView中setEmtpyView的正确使用

发布于 2024-09-01 04:57:33 字数 2044 浏览 2 评论 0原文

我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

星星的轨迹 2024-09-08 04:57:33

您需要在已添加 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>

代码:

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

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 :


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

戴着白色围巾的女孩 2024-09-08 04:57:33

卡兰的回答很准确。 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));

在每个子活动中,我重写 getIdOfEmptyView 如下:

@Override
protected int getIdOfEmptyView()
{
    return R.layout.emptyView_Favorites;
}

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

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:

@Override
protected int getIdOfEmptyView()
{
    return R.layout.emptyView_Favorites;
}
忆伤 2024-09-08 04:57:33

您可以定义外部 XML 布局文件、膨胀并将其设置为空视图。
唯一要记住的是设置膨胀视图的父级。

View emptyView = inflater.inflate(R.layout.id_of_your_layoout_file, parent, false);
gridView.setEmptyView(emptyView);

或者

listView.setEmptyView(emptyView);

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.

View emptyView = inflater.inflate(R.layout.id_of_your_layoout_file, parent, false);
gridView.setEmptyView(emptyView);

or

listView.setEmptyView(emptyView);
葵雨 2024-09-08 04:57:33

PacificSky 的答案几乎满足了我的需要,但是使用 addContentView 将空视图添加到活动中会导致空视图保留在活动的容器中,即使从活动中删除包含 ListView 的片段也是如此。此外,活动的滑入菜单显示在空视图下方或与空视图混合。

对我来说,解决方案是将空视图添加到作为参数传递给片段的 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);

并从片段的 onDestroyView 中的容器中删除空视图:

@Override
public void onDestroyView(){
    super.onDestroyView();
    mFragmentContainer.removeView(mEmptyListView);
}

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:

@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:

@Override
public void onDestroyView(){
    super.onDestroyView();
    mFragmentContainer.removeView(mEmptyListView);
}
素染倾城色 2024-09-08 04:57:33

就我而言,
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;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文