Correct use of setEmtpyView in AdapterView

后端 未结 5 1085
情话喂你
情话喂你 2020-12-05 00:17

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:

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-05 00:40

    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;
    }
    

提交回复
热议问题