ListFragment does not accept my layout

前端 未结 10 1357
情歌与酒
情歌与酒 2020-12-01 02:09

According to this link: ListFragment android developers

I want to set my custom layout for list, but it makes exceptions.

Here is the code:

p         


        
10条回答
  •  时光取名叫无心
    2020-12-01 03:07

    Create the layout file list_content.xml

    
    
        
        
            
            
            
                
        
            
        
                
            
            
        
            
    
    

    Put this inside your ListFragment class:

    public ListView mList;
    boolean mListShown;
    View mProgressContainer;
    View mListContainer;
    
    public void setListShown(boolean shown, boolean animate){
        if (mListShown == shown) {
            return;
        }
        mListShown = shown;
        if (shown) {
            if (animate) {
                mProgressContainer.startAnimation(AnimationUtils.loadAnimation(
                        getActivity(), android.R.anim.fade_out));
                mListContainer.startAnimation(AnimationUtils.loadAnimation(
                        getActivity(), android.R.anim.fade_in));
            }
            mProgressContainer.setVisibility(View.GONE);
            mListContainer.setVisibility(View.VISIBLE);
        } else {
            if (animate) {
                mProgressContainer.startAnimation(AnimationUtils.loadAnimation(
                        getActivity(), android.R.anim.fade_in));
                mListContainer.startAnimation(AnimationUtils.loadAnimation(
                        getActivity(), android.R.anim.fade_out));
            }
            mProgressContainer.setVisibility(View.VISIBLE);
            mListContainer.setVisibility(View.INVISIBLE);
        }
    }
    public void setListShown(boolean shown){
        setListShown(shown, true);
    }
    public void setListShownNoAnimation(boolean shown) {
        setListShown(shown, false);
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        int INTERNAL_EMPTY_ID = 0x00ff0001;
        View root = inflater.inflate(R.layout.list_content, container, false);
        (root.findViewById(R.id.internalEmpty)).setId(INTERNAL_EMPTY_ID);
        mList = (ListView) root.findViewById(android.R.id.list);
        mListContainer =  root.findViewById(R.id.listContainer);
        mProgressContainer = root.findViewById(R.id.progressContainer);
        mListShown = true;
        return root;
    }
    

    Now you can use normally:

    setListShown(boolean shown);
    setListShown(boolean shown, boolean animate);
    setListShownNoAnimation(boolean shown);
    

    Source:

    http://source-android.frandroid.com/frameworks/base/core/java/android/app/ListFragment.java

    http://source-android.frandroid.com/frameworks/base/core/res/res/layout/list_content.xml

提交回复
热议问题