com.cnm.cnmrc.fragment.vodtvch.VodTvch.java Source code

Java tutorial

Introduction

Here is the source code for com.cnm.cnmrc.fragment.vodtvch.VodTvch.java

Source

/*
 * Copyright (C) 2011 Chris Gao <chris@exina.net>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cnm.cnmrc.fragment.vodtvch;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentManager.BackStackEntry;
import android.support.v4.app.FragmentManager.OnBackStackChangedListener;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ListView;

import com.cnm.cnmrc.MainActivity;
import com.cnm.cnmrc.R;
import com.cnm.cnmrc.fragment.rc.RcBottomMenu;
import com.cnm.cnmrc.slidingmenu.SlidingMenu;

public class VodTvch extends Fragment implements View.OnClickListener, SlidingMenu.Listener {

    public static VodTvch newInstance(String type) {
        VodTvch f = new VodTvch();
        Bundle args = new Bundle();
        args.putString("type", type); // vod or tvch
        f.setArguments(args);
        return f;
    }

    public SlidingMenu mSlidingMenu;

    FrameLayout mCategoryCover, mLoadingData;
    ListView mCategory;

    String[] mCategoryArray = null;
    String[] mClassTypeArray = null;
    int selectedCategory = 0;

    public int mBackStackEntry = 100;

    public int currentDepth = 1;

    public HashMap<Integer, String> mMapTitle = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View layout = inflater.inflate(R.layout.vod_tvch, container, false);

        mSlidingMenu = (SlidingMenu) layout.findViewById(R.id.animation_layout);
        mSlidingMenu.setListener(this);

        mLoadingData = (FrameLayout) layout.findViewById(R.id.loading_data_panel);

        mCategoryCover = (FrameLayout) layout.findViewById(R.id.category_cover);
        mCategoryCover.setOnClickListener(this);
        mCategoryCover.setVisibility(View.GONE);

        mCategory = (ListView) layout.findViewById(R.id.category_list);

        //        FragmentManager fm = getActivity().getSupportFragmentManager();
        //        fm.addOnBackStackChangedListener(mAddOnBackStackChangedListener);
        //        fm.removeOnBackStackChangedListener(mRemoveOnBackStackChangedListener);

        // --------------------------
        // type check (vod or tvch)
        // --------------------------
        String type = getArguments().getString("type");
        if (type.equals("vod")) {
            mCategoryArray = getActivity().getResources().getStringArray(R.array.vod_category);
            mClassTypeArray = getActivity().getResources().getStringArray(R.array.vod_class_type);
        }
        if (type.equals("tvch")) {
            mCategoryArray = getActivity().getResources().getStringArray(R.array.tvch_category);
            mClassTypeArray = getActivity().getResources().getStringArray(R.array.tvch_class_type);
        }

        ArrayList<String> arrayList = new ArrayList<String>(mCategoryArray.length);
        for (String item : mCategoryArray) {
            arrayList.add(item);
        }

        // -------------------------
        // set title : category
        // -------------------------
        // ??  , ? back key    . depth   .
        mMapTitle = new HashMap<Integer, String>();

        setTitle(0);

        // -------------------
        // sidebar category
        // -------------------
        VodTvchAdapter adapter = new VodTvchAdapter(getActivity(), R.layout.list_item_vod_category, arrayList);
        mCategory.setAdapter(adapter);
        mCategory.setDivider(null);
        mCategory.setDividerHeight(0);
        mCategory.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // sidebar  return...   ? sidebar  ?? ??...
                if (!mSlidingMenu.isOpening())
                    return;

                // sidebar ?  item??  ...
                mCategoryCover.setVisibility(View.VISIBLE);

                // change category text color
                view.setSelected(true);
                selectedCategory = position;

                // change title
                setTitle(position);

                // close sidebar
                mSlidingMenu.toggleSidebar();

                Log.i("hwang", "category is selected");

                /*View v;
                int count = parent.getChildCount();
                v = parent.getChildAt(position);*/
            }

        });

        // -----------------------
        // ? bottom menu 
        // -----------------------
        Fragment f = getActivity().getSupportFragmentManager().findFragmentById(R.id.fragment_rc_bottom_menu);
        if (f != null)
            ((RcBottomMenu) f).setVodTvchMode();

        // --------------------------------------------------------------------------------------------------------
        // ? ? ?  fragment VodTvch ?? loadingDataForSidrebar()? no addToBackStack ??.
        // --------------------------------------------------------------------------------------------------------
        loadingDataForSidrebar();

        return layout;
    }

    /**
     * ? ? ?  fragment VodTvch ?? loadingDataForSidrebar()? no addToBackStack ??.
     * ? MainActivity onBackPressed()? back key ?.
     * ? VodTvch fragment addToBackStack ?? ?,  ?? Base back key . 
     * back key?   ??... Base? destoryView()?  ? ?.
     * no addToBackStack(null)
     * ft.replace(R.id.loading_data_panel, base)
     */
    private void loadingDataForSidrebar() {
        FragmentManager fm = getActivity().getSupportFragmentManager();
        Log.i("hwang", "before vodTvch fragment count (no add to stack) --> "
                + Integer.toString(fm.getBackStackEntryCount()));

        FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
        Class<?> classObject;
        try {
            String packageName = this.getClass().getPackage().getName() + ".";
            classObject = Class.forName(packageName + mClassTypeArray[selectedCategory]); // CommonList, VodSemiDetail, VodDetail, TvchSemiDetail, TvchDetail
            Object obj = classObject.newInstance();

            Base base = ((Base) obj).newInstance(mCategoryArray[selectedCategory], true); // true : 1 depth (vod:VodSemiDetail, VodSemiDetail, VodSemiDetail, VodList)
            // true : 1 depth (tvch: TvchSemiDetail, TvchList, TvchSemiDetail, TvchSemiDetail)

            //ft.addToBackStack(null);   // addTBackStack  onBackPressed() ? ?... remove fragment onDestroyView() ?.
            ft.replace(R.id.loading_data_panel, base);
            ft.commit();
            //fm.executePendingTransactions();   // fragment? ? pending recursive error,  ?? pending .
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (java.lang.InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        Log.i("hwang", "after vodTvch fragment count (no add to stack) --> "
                + Integer.toString(fm.getBackStackEntryCount()));

        // ------------------------------------
        // ? bottom menu? depth level 
        // ------------------------------------
        Fragment f = getActivity().getSupportFragmentManager().findFragmentById(R.id.fragment_rc_bottom_menu);
        if (f != null)
            ((RcBottomMenu) f).setDepthLevelClear(); // set 1 depth
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

    }

    @Override
    public void onDetach() {
        super.onDetach();

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

        if (((MainActivity) getActivity()).noVodTvchDestroy)
            return;

        // ----------------------------------------------------------------------------------------------
        // vod? ?? vodtopmenu fragment vod destory?  ??? vodtopmenu? ? destory .
        // ?  ???? ? fragment ? vod? ?  ? ? Activity?   .
        // ----------------------------------------------------------------------------------------------
        {
            Fragment f = getActivity().getSupportFragmentManager()
                    .findFragmentById(R.id.fragment_vod_tvch_top_menu);
            if (f != null) {
                getActivity().getSupportFragmentManager().beginTransaction().remove(f).commit();
            }
        }

        {
            Fragment f = getActivity().getSupportFragmentManager().findFragmentById(R.id.loading_data_panel);
            if (f != null) {
                // fragment   Base? onDestroyView callBack?.
                // ?  back stack? count ? minus? destoryView?   0??.
                // 0?  Base? onDestroyView()? .

                // ? ? onDestroyView()? ?  R.id.loading_data_panel?  fragment ? backstack  ? minus?.
                // ? ? ??. ? before after ? minus? count?.
                //  remove ? ?. rc  ?.  Base? onDestroyView() callBack .
                //  ? Base? onDestroyView()?   ??  ? remove   ? ? ? .
                // ?? remove Base? onDestroyView()? callBack ?.
                FragmentManager fm = getActivity().getSupportFragmentManager();
                Log.i("hwang", "before at vodTvch : remove(R.id.loading_data_panel).commit fragment count --> "
                        + Integer.toString(fm.getBackStackEntryCount()));
                getActivity().getSupportFragmentManager().beginTransaction().remove(f).commit();
                Log.i("hwang", "after at vodTvch : remove(R.id.loading_data_panel).commit fragment count --> "
                        + Integer.toString(fm.getBackStackEntryCount()));

                //   base? remove ?? TAG_FRAGMENT_BASE  ? ?.
                //   mainActity? backpress?  base? destoryView callback?.
                // .
                // ??  ? ? ?.
                // getActivity().getSupportFragmentManager().executePendingTransactions();   
            }
        }

        // -----------------------
        // ? bottom menu 
        // -----------------------
        {
            Fragment f = ((MainActivity) getActivity()).getFragmentRcBottomMenu();
            if (f != null)
                ((RcBottomMenu) f).setRemoconMode();
        }
    }

    /*
     * back key? ?? ??. 
     */
    public int allowBackPressed() {
        if (mSlidingMenu.isOpening())
            return 1; //  ? sidebar ? ?.
        else
            return 0;
    }

    private void clearSelectedAll() {
        for (int i = 0; i < mCategoryArray.length; i++) {
            mCategory.getChildAt(i).setSelected(false);
        }
    }

    public String[] getCategoryArray() {
        return mCategoryArray;
    }

    //  ?? VodTvch Base ? ? ?. (1)
    private void setTitle(int position) {
        Fragment f = getActivity().getSupportFragmentManager().findFragmentById(R.id.fragment_vod_tvch_top_menu);
        if (f != null)
            ((VodTvchTopMenu) f).setTitle(mCategoryArray[position]);

        saveTitle(currentDepth, mCategoryArray[position]);
    }

    public void setTitle(String title) {
        Fragment f = getActivity().getSupportFragmentManager().findFragmentById(R.id.fragment_vod_tvch_top_menu);
        if (f != null)
            ((VodTvchTopMenu) f).setTitle(title);

        saveTitle(currentDepth, title);
    }

    public void setTitle() {
        String title = getTitle(--currentDepth);
        Fragment f = getActivity().getSupportFragmentManager().findFragmentById(R.id.fragment_vod_tvch_top_menu);
        if (f != null)
            ((VodTvchTopMenu) f).setTitle(title);
    }

    public void saveTitle(int key, String title) {
        mMapTitle.put(key, title);
    }

    public String getTitle(int key) {
        return mMapTitle.get(key);
    }

    @Override
    public void onSidebarOpenedStart() {
        //  ?? category clear...
        clearSelectedAll();

        // listview? getChildAt()? ?   view .
        // ?  null? .
        View selectedItem = mCategory.getChildAt(selectedCategory);
        if (selectedItem != null)
            selectedItem.setSelected(true);

        //mCategory.performItemClick(mCategory.getChildAt(selectedCategory), selectedCategory, mCategory.getAdapter().getItemId(selectedCategory));
    }

    @Override
    public void onSidebarOpenedEnd() {
        // TODO Auto-generated method stub
    }

    @Override
    public void onSidebarClosedStart() {
        {
            Fragment f = getActivity().getSupportFragmentManager().findFragmentById(R.id.loading_data_panel);
            if (f != null)
                getActivity().getSupportFragmentManager().beginTransaction().remove(f).commit();
        }

        // ------------------------------------
        // ? bottom menu? depth level 
        // onSidebarClosedEnd() > loadingDataForSidrebar()?? ? ...
        // ------------------------------------
        Fragment f = getActivity().getSupportFragmentManager().findFragmentById(R.id.fragment_rc_bottom_menu);
        if (f != null)
            ((RcBottomMenu) f).setDepthLevelClear(); // set 1 depth
    }

    @Override
    public void onSidebarClosedEnd() {
        mCategoryCover.setVisibility(View.GONE);

        // ---------------
        // data loading
        // ---------------
        loadingDataForSidrebar();
    }

    @Override
    public boolean onContentTouchedWhenOpening() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

    OnBackStackChangedListener mRemoveOnBackStackChangedListener = new OnBackStackChangedListener() {
        @Override
        public void onBackStackChanged() {
            int i = getActivity().getSupportFragmentManager().getBackStackEntryCount();
            BackStackEntry tt = getActivity().getSupportFragmentManager().getBackStackEntryAt(i - 1);
            int checkedId = tt.getBreadCrumbTitleRes();
        }
    };

    OnBackStackChangedListener mAddOnBackStackChangedListener = new OnBackStackChangedListener() {
        @Override
        public void onBackStackChanged() {
            //int i = getActivity().getSupportFragmentManager().getBackStackEntryCount();
            //BackStackEntry tt = getSupportFragmentManager().getBackStackEntryAt(i - 1);
            //int checkedId = tt.getBreadCrumbTitleRes();
        }
    };

}