com.mstar.tv.tvplayer.philips.setting.SettingMainFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.mstar.tv.tvplayer.philips.setting.SettingMainFragment.java

Source

/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * 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.mstar.tv.tvplayer.philips.setting;

import com.mstar.tv.tvplayer.kangguan.tvapi.K_Constants;
import com.mstar.tv.tvplayer.kangguan.tvapi.K_TvCommonManager;
import com.mstar.tv.tvplayer.ui.R;

import android.app.Activity;
import android.support.v4.app.ListFragment;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnKeyListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ListView;

public class SettingMainFragment extends ListFragment {
    MainFragmentSelectedListener mCallback;
    private SettingMainViewSettingAdapter settingAdapter;
    private Context mContext;
    private ListView listView;

    public interface MainFragmentSelectedListener {
        public void onMainSelectedData(int dataPositon);

        public void onMainClick(int position);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
        settingAdapter = new SettingMainViewSettingAdapter(mContext,
                SettingKarrays.getinstance(mContext).getMainData());
        setListAdapter(settingAdapter);
    }

    @Override
    public void onStart() {
        super.onStart();
        listView = getListView();
        if (getFragmentManager().findFragmentById(R.id.first_fragment) != null) {
            listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        }
        listView.setDivider(null);
        listView.setDividerHeight(5);
        listView.setSelector(android.R.color.transparent);
        listView.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                mCallback.onMainSelectedData(position);
                if (parent.isFocused())
                    settingAdapter.setSelectViewPosition(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        listView.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    settingAdapter.setSelectViewPosition(listView.getSelectedItemPosition());
                } else {
                    settingAdapter.setSelectViewPosition(-1);
                }
            }
        });
        listView.setOnKeyListener(new OnKeyListener() {

            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (K_TvCommonManager.getInstance().K_getCurrentTvInputSource() != K_Constants.INPUT_SOURCE_ATV
                        && K_TvCommonManager.getInstance()
                                .K_getCurrentTvInputSource() != K_Constants.INPUT_SOURCE_DTV
                        || SettingKarrays.isTopActivity(mContext, "com.android.mslauncher.LauncherActivity")) {
                    if (listView.getSelectedItemPosition() == listView.getLastVisiblePosition() - 1
                            && keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
                        listView.setSelection(listView.getLastVisiblePosition() - 1);
                        return true;
                    }
                }
                return false;
            }
        });
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            getListView().setFocusable(false);
        } else {
            getListView().setFocusable(true);
            getListView().requestFocus();
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mCallback = (MainFragmentSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnHeadlineSelectedListener");
        }
    }

    @Override
    public void onListItemClick(ListView listView, View v, int position, long id) {
        mCallback.onMainClick(position);
        settingAdapter.setSelectViewPosition(-1);
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
        case KeyEvent.KEYCODE_DPAD_LEFT:
            break;
        case KeyEvent.KEYCODE_DPAD_RIGHT:
            mCallback.onMainClick(listView.getSelectedItemPosition());
            settingAdapter.setSelectViewPosition(-1);
            break;
        }
        return false;
    }

    public void updateView(int dataPostiton, boolean state) {
        settingAdapter = new SettingMainViewSettingAdapter(mContext,
                SettingKarrays.getinstance(mContext).getMainData());
        setListAdapter(settingAdapter);
        if (state) {
            settingAdapter.setSelectViewPosition(dataPostiton);
            getListView().setFocusable(true);
            getListView().requestFocus();
        } else {
            settingAdapter.setSelectViewPosition(-1);
            getListView().setFocusable(false);
        }

    }
}