Correct use of setEmtpyView in AdapterView

后端 未结 5 1070
情话喂你
情话喂你 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条回答
  •  暖寄归人
    2020-12-05 00:42

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

提交回复
热议问题