Code example for Fragment

Methods: getView, onViewCreated

0
    /** 
     * Attach to list view once the view hierarchy has been created. 
     */ 
    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ensureList(); 
    } 
 
    /** Detach from list view. */ 
    @Override 
    public void onDestroyView() { 
        mHandler.removeCallbacks(mRequestFocus);
        mList = null;
        super.onDestroyView(); 
    } 
 
    /** 
     * This method will be called when an item in the list is selected. 
     * Subclasses should override. Subclasses can call 
     * getListView().getItemAtPosition(position) if they need to access the 
     * data associated with the selected item. 
     * @param l        The ListView where the click happened 
     * @param v        The view that was clicked within the ListView 
     * @param position The position of the view in the list 
     * @param id       The row id of the item that was clicked 
     */ 
    public void onListItemClick(ListView l, View v, int position, long id) {
    } 
 
    /** Provide the cursor for the list view. */ 
    public void setListAdapter(ExpandableListAdapter adapter) {
        boolean hadAdapter = mAdapter != null;
        mAdapter = adapter;
        if (mList != null) {
            mList.setAdapter(adapter);
            if (!mListShown && !hadAdapter) {
                setListShown(true, getView().getWindowToken() != null);
            } 
        } 
    } 
 
    public ExpandableListView getExpandableListView() {
        ensureList(); 
        return mList;
    } 
 
    /** 
     * Control whether the list is being displayed.  You can make it not 
     * displayed if you are waiting for the initial data to show in it.  During 
     * this time an indeterminant progress indicator will be shown instead. 
     * @param shown   If true, the list view is shown; if false, the progress 
     *                indicator.  The initial value is true. 
     * @param animate If true, an animation will be used to transition to the 
     *                new state. 
     */ 
    private void setListShown(boolean shown, boolean animate) {
        ensureList(); 
        if (mListShown == shown) {
            return; 
        } 
        mListShown = shown;
        if (mListContainer != null) {
            if (shown) {
                if (animate) {
                    mListContainer.startAnimation(AnimationUtils.loadAnimation(
                            getActivity(), android.R.anim.fade_in));
                } 
                mListContainer.setVisibility(View.VISIBLE);
            } else { 
                if (animate) {
                    mListContainer.startAnimation(AnimationUtils.loadAnimation(
                            getActivity(), android.R.anim.fade_out));
                } 
                mListContainer.setVisibility(View.GONE);
            } 
        } 
    } 
 
    /** Get the ListAdapter associated with this activity's ListView. */ 
    public ExpandableListAdapter getExpandableListAdapter() {
        return mAdapter;
    } 
 
    private void ensureList() { 
        if (mList != null) {
            return; 
        } 
        View root = getView();
        if (root == null) {
            throw new IllegalStateException("Content view not yet created");
        } 
        if (root instanceof ExpandableListView) {
            mList = (ExpandableListView) root;