Listview按键触发选择item改变文字

控件 专栏收录该内容
13 篇文章 0 订阅

做的是智能TV,手机上触屏那套Listview的方式当然就有问题,第一个,首次进入Listview发现条目没有焦点。第二个,使用Listview发现onKeyDown(int keycode,KeyEvent event)不起作用。解决这两个问题,网上找到方法,

第一个问题:



        Instrumentation inst = new Instrumentation();
        inst.setInTouchMode(false);//listView首次进入无焦点的解决办法。在listview生成前,加入这两句,但是我实验不起作用。后来还是用的这个:

    public void recoverFocus(){
        listview_ci_debug.requestFocus();
        listview_ci_debug.setSelection(0);       
    }

这个函数的用法对位置有特别要求,就是必须放在ui主activity中调用。放在子类中不起作用,很奇怪。


第二个问题:

public boolean dispatchKeyEvent(KeyEvent event) {
        // TODO Auto-generated method stub
        boolean bRet = true;
        currentIndex = getCurrentIndex();
        if(event.getAction()==KeyEvent.ACTION_DOWN && event.getAction() != KeyEvent.ACTION_UP){
        switch (event.getKeyCode()){
       //你的代码逻辑,比如
            case KeyEvent.KEYCODE_DPAD_RIGHT:

break;

   }
        return bRet;
    }

特别注意的是最后要return true;保证按键响应执行。


接下来就是直接贴代码了:

mainActivity.java

package mstar.tvsetting.factory.ui.advancedoption;

import mstar.factorymenu.ui.R;
import mstar.tvsetting.factory.desk.FactoryDeskImpl;
import mstar.tvsetting.factory.desk.IFactoryDesk;
import android.app.Activity;
import android.app.Instrumentation;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.LinearLayout;

public class AdvancedOptionActivity extends Activity {
    private static final String TAG = "AdvancedOptionActivity";

    private IFactoryDesk factoryDesk;

    private AdvancedOptionListViewHolder advancedOptionListViewHolder;

    private UpgradeExportViewHolder upgradeExportViewHolder;

    private EnergyAdjustViewHolder energyAdjustViewHolder;

    private SSCAdjustViewHolder sscAdjustViewHolder;

    private CISettingListViewHolder ciSettingListViewHolder;

    private VIFAdjustViewHolder vifAdjustViewHolder;

    private VDAdjustListViewHolder vdAdjustListViewHolder;

    private IPEnableMappingViewHolder ipMappingViewHolder;

    private FactoryScanningViewHolder facScanningViewHolder;

    private FactoryWritingViewHolder facWritingViewHolder;

    private final static int ADV_MAIN_PAGE = 0;

    private final static int EX_UP_ADJUST_PAGE = 1;

    private final static int ENERGY_ADJUST_PAGE = 2;

    private final static int SSC_ADJUST_PAGE = 3;

    private final static int CI_ADJUST_PAGE = 4;

    private final static int VIF_ADJUST_PAGE = 5;

    private final static int VD_ADJUST_PAGE = 6;

    private final static int IP_MAPPING_PAGE = 7;

    private final static int MAC_SCANNING_PAGE = 8;

    private final static int MAC_WRITING_PAGE = 9;

    private final static int WIFIMAC_SCANNING_PAGE = 10;

    private final static int WIFIMAC_WRITING_PAGE = 11;

    private final static int EX_UP_ADJUST_INX = 101;

    private final static int ENERGY_ADJUST_INX = 102;

    private final static int SSC_ADJUST_INX = 103;

    private final static int CI_ADJUST_INX = 104;

    private final static int VIF_ADJUST_INX = 105;

    private final static int VD_ADJUST_INX = 106;

    private final static int IP_MAPPING_INX = 107;

    private final static int MAC_SCANNING_INX = 108;

    private final static int MAC_WRITING_INX = 109;

    private final static int WIFIMAC_SCANNING_INX = 110;

    private final static int WIFIMAC_WRITING_INX = 111;

    private final static int SFU_AUTO_TEST_INX = 112;

    public int currentAdvPage = ADV_MAIN_PAGE;

    public boolean isSupportHDCP2 = false;

    public boolean isSupportCI_Plus = false;

    public static int startMacPage;

    private int currentIndex = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.advancedoptionviewflipper);
        factoryDesk = FactoryDeskImpl.getInstance(AdvancedOptionActivity.this);
        advancedOptionListViewHolder = new AdvancedOptionListViewHolder(
                AdvancedOptionActivity.this, factoryDesk);
        advancedOptionListViewHolder.findView();
        if (advancedOptionListViewHolder.getListView() != null) {
            advancedOptionListViewHolder.getListView().setOnItemClickListener(
                    new OnItemClickListener() {

                        @Override
                        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                            // TODO Auto-generated method stub
                            goToNewPage(arg2);
                        }

                    });
            advancedOptionListViewHolder.getListView().setOnItemSelectedListener(
                    new OnItemSelectedListener() {

                        @Override
                        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                                long arg3) {
                            // TODO Auto-generated method stub
                            // currentAdvPage = arg2+1;
                            currentIndex = arg2 + 101;

                        }

                        @Override
                        public void onNothingSelected(AdapterView<?> arg0) {
                            // TODO Auto-generated method stub

                        }

                    });
        }
        currentAdvPage = ADV_MAIN_PAGE;
        // advancedOptionViewHolder.advancedViewFlipper.setDisplayedChild(ADV_MAIN_PAGE);
        startMacPage = getIntent().getIntExtra("startMacPage", 0);

        if (!AdvancedOptionActivity.this.getResources().getString(R.string.designmenu_hdcp2)
                .equals("1")) {
            isSupportHDCP2 = false;
        } else {
            isSupportHDCP2 = true;
        }
        if (!AdvancedOptionActivity.this.getResources().getString(R.string.designmenu_ci)
                .equals("1")) {
            isSupportCI_Plus = false;
        } else {
            isSupportCI_Plus = true;
        }

    }

    @Override
    protected void onResume() {
        if (startMacPage > 0) {
            currentAdvPage = MAC_SCANNING_PAGE;

            advancedOptionListViewHolder.advancedViewFlipper.setDisplayedChild(MAC_SCANNING_PAGE);
            facScanningViewHolder = new FactoryScanningViewHolder(AdvancedOptionActivity.this,
                    factoryDesk);
            facScanningViewHolder.onCreate();
        }
        if (advancedOptionListViewHolder != null) {
            advancedOptionListViewHolder.setListViewData();
        }

        super.onResume();
    }

    private void goToNewPage(int Position) {
        currentIndex = Position + 101;
        switch (currentIndex) {
            case EX_UP_ADJUST_INX:
                currentAdvPage = EX_UP_ADJUST_PAGE;
                advancedOptionListViewHolder.advancedViewFlipper
                        .setDisplayedChild(EX_UP_ADJUST_PAGE);
                upgradeExportViewHolder = new UpgradeExportViewHolder(AdvancedOptionActivity.this,
                        factoryDesk);
                upgradeExportViewHolder.findView();
                upgradeExportViewHolder.onCreate();
                break;
            case ENERGY_ADJUST_INX:
                currentAdvPage = ENERGY_ADJUST_PAGE;
                advancedOptionListViewHolder.advancedViewFlipper
                        .setDisplayedChild(ENERGY_ADJUST_PAGE);
                energyAdjustViewHolder = new EnergyAdjustViewHolder(AdvancedOptionActivity.this,
                        factoryDesk);
                energyAdjustViewHolder.findView();
                energyAdjustViewHolder.onCreate();
                break;
            case SSC_ADJUST_INX:
                currentAdvPage = SSC_ADJUST_PAGE;
                advancedOptionListViewHolder.advancedViewFlipper.setDisplayedChild(SSC_ADJUST_PAGE);
                sscAdjustViewHolder = new SSCAdjustViewHolder(AdvancedOptionActivity.this,
                        factoryDesk);
                sscAdjustViewHolder.findView();
                sscAdjustViewHolder.onCreate();
                break;
            case CI_ADJUST_INX:
                currentAdvPage = CI_ADJUST_PAGE;
                advancedOptionListViewHolder.advancedViewFlipper.setDisplayedChild(CI_ADJUST_PAGE);
                ciSettingListViewHolder = new CISettingListViewHolder(AdvancedOptionActivity.this,
                        factoryDesk);
                ciSettingListViewHolder.findView();
                ciSettingListViewHolder.onCreate();
                break;
            case VIF_ADJUST_INX:
                currentAdvPage = VIF_ADJUST_PAGE;
                advancedOptionListViewHolder.advancedViewFlipper.setDisplayedChild(VIF_ADJUST_PAGE);
                vifAdjustViewHolder = new VIFAdjustViewHolder(AdvancedOptionActivity.this,
                        factoryDesk);
                vifAdjustViewHolder.findView();
                vifAdjustViewHolder.onCreate();
                break;
            case VD_ADJUST_INX:
                currentAdvPage = VD_ADJUST_PAGE;
                advancedOptionListViewHolder.advancedViewFlipper.setDisplayedChild(VD_ADJUST_PAGE);
                vdAdjustListViewHolder = new VDAdjustListViewHolder(AdvancedOptionActivity.this,
                        factoryDesk);
                vdAdjustListViewHolder.findView();
                vdAdjustListViewHolder.onCreate();
                vdAdjustListViewHolder.recoverFocus();
                break;
            case IP_MAPPING_INX:
                currentAdvPage = IP_MAPPING_PAGE;
                advancedOptionListViewHolder.advancedViewFlipper.setDisplayedChild(IP_MAPPING_PAGE);
                ipMappingViewHolder = new IPEnableMappingViewHolder(AdvancedOptionActivity.this,
                        factoryDesk);
                // ipMappingViewHolder.findView();
                ipMappingViewHolder.onCreate();
                ipMappingViewHolder.recoverFocus();
                break;
            case MAC_SCANNING_INX:
                currentAdvPage = MAC_SCANNING_PAGE;
                advancedOptionListViewHolder.advancedViewFlipper
                        .setDisplayedChild(MAC_SCANNING_PAGE);
                facScanningViewHolder = new FactoryScanningViewHolder(AdvancedOptionActivity.this,
                        factoryDesk);
                // ipMappingViewHolder.findView();
                facScanningViewHolder.facScanSetType(0);
                facScanningViewHolder.onCreate();
            case WIFIMAC_SCANNING_INX:
                currentAdvPage = WIFIMAC_SCANNING_PAGE;
                advancedOptionListViewHolder.advancedViewFlipper
                        .setDisplayedChild(MAC_SCANNING_PAGE);
                facScanningViewHolder = new FactoryScanningViewHolder(AdvancedOptionActivity.this,
                        factoryDesk);
                // ipMappingViewHolder.findView();
                facScanningViewHolder.facScanSetType(1);
                facScanningViewHolder.onCreate();
                break;
            case MAC_WRITING_INX:
                currentAdvPage = MAC_WRITING_PAGE;
                advancedOptionListViewHolder.advancedViewFlipper
                        .setDisplayedChild(MAC_WRITING_PAGE);
                facWritingViewHolder = new FactoryWritingViewHolder(AdvancedOptionActivity.this,
                        factoryDesk);
                facWritingViewHolder.facWriteSetType(0);
                facWritingViewHolder.onCreate();
                break;
            case WIFIMAC_WRITING_INX:
                currentAdvPage = WIFIMAC_WRITING_PAGE;
                advancedOptionListViewHolder.advancedViewFlipper
                        .setDisplayedChild(MAC_WRITING_PAGE);
                facWritingViewHolder = new FactoryWritingViewHolder(AdvancedOptionActivity.this,
                        factoryDesk);
                facWritingViewHolder.facWriteSetType(1);
                facWritingViewHolder.onCreate();
                break;
            case SFU_AUTO_TEST_INX:
                Intent intent = new Intent("android_serialport_api.intent.action.MainActivity");
                intent.putExtra("TEST_MODE", 2);
                startActivity(intent);
                finish();
                break;
            default:
                break;
        }
    }

    private int getCurrentAdvPage() {
        return currentAdvPage;
    }

    public boolean dispatchKeyEvent(KeyEvent event) {
        // TODO Auto-generated method stub
        boolean bRet = false;
        currentAdvPage = getCurrentAdvPage();

        switch (currentAdvPage) {

            case EX_UP_ADJUST_PAGE:

                bRet = upgradeExportViewHolder.dispatchKeyEvent(event);
                break;
            case ENERGY_ADJUST_PAGE:

                bRet = energyAdjustViewHolder.dispatchKeyEvent(event);
                break;
            case SSC_ADJUST_PAGE:

                bRet = sscAdjustViewHolder.dispatchKeyEvent(event);
                break;
            case CI_ADJUST_PAGE:

                bRet = ciSettingListViewHolder.dispatchKeyEvent(event);
                break;
            case VIF_ADJUST_PAGE:

                bRet = vifAdjustViewHolder.dispatchKeyEvent(event);
                break;
            case VD_ADJUST_PAGE:

                bRet = vdAdjustListViewHolder.dispatchKeyEvent(event);
                break;
            case IP_MAPPING_PAGE:

                bRet = ipMappingViewHolder.dispatchKeyEvent(event);
                break;
            case MAC_SCANNING_PAGE:

                bRet = facScanningViewHolder.dispatchKeyEvent(event);
                break;
            case WIFIMAC_SCANNING_PAGE:

                bRet = facScanningViewHolder.dispatchKeyEvent(event);
                break;
            case MAC_WRITING_PAGE:

                bRet = facWritingViewHolder.dispatchKeyEvent(event);
                break;
            case WIFIMAC_WRITING_PAGE:

                bRet = facWritingViewHolder.dispatchKeyEvent(event);
                break;
            default:
                break;
        }

        if (bRet) {
            return true;
        }
        if (event.getAction() == KeyEvent.ACTION_DOWN && event.getAction() != KeyEvent.ACTION_UP) {
            switch (event.getKeyCode()) {
            // 你的代码逻辑,比如
                case KeyEvent.KEYCODE_DPAD_CENTER:
                case KeyEvent.KEYCODE_ENTER:
                    switch (currentIndex) {
                        case EX_UP_ADJUST_INX:
                            currentAdvPage = EX_UP_ADJUST_PAGE;
                            advancedOptionListViewHolder.advancedViewFlipper
                                    .setDisplayedChild(EX_UP_ADJUST_PAGE);
                            upgradeExportViewHolder = new UpgradeExportViewHolder(
                                    AdvancedOptionActivity.this, factoryDesk);
                            upgradeExportViewHolder.findView();
                            upgradeExportViewHolder.onCreate();

                            break;
                        case ENERGY_ADJUST_INX:
                            currentAdvPage = ENERGY_ADJUST_PAGE;
                            advancedOptionListViewHolder.advancedViewFlipper
                                    .setDisplayedChild(ENERGY_ADJUST_PAGE);
                            energyAdjustViewHolder = new EnergyAdjustViewHolder(
                                    AdvancedOptionActivity.this, factoryDesk);
                            energyAdjustViewHolder.findView();
                            energyAdjustViewHolder.onCreate();

                            break;
                        case SSC_ADJUST_INX:
                            currentAdvPage = SSC_ADJUST_PAGE;
                            advancedOptionListViewHolder.advancedViewFlipper
                                    .setDisplayedChild(SSC_ADJUST_PAGE);
                            sscAdjustViewHolder = new SSCAdjustViewHolder(
                                    AdvancedOptionActivity.this, factoryDesk);
                            sscAdjustViewHolder.findView();
                            sscAdjustViewHolder.onCreate();

                            break;
                        case CI_ADJUST_INX:
                            currentAdvPage = CI_ADJUST_PAGE;
                            advancedOptionListViewHolder.advancedViewFlipper
                                    .setDisplayedChild(CI_ADJUST_PAGE);
                            ciSettingListViewHolder = new CISettingListViewHolder(
                                    AdvancedOptionActivity.this, factoryDesk);
                            ciSettingListViewHolder.findView();
                            ciSettingListViewHolder.onCreate();
                            ciSettingListViewHolder.recoverFocus();

                            break;
                        case VIF_ADJUST_INX:
                            currentAdvPage = VIF_ADJUST_PAGE;
                            advancedOptionListViewHolder.advancedViewFlipper
                                    .setDisplayedChild(VIF_ADJUST_PAGE);
                            vifAdjustViewHolder = new VIFAdjustViewHolder(
                                    AdvancedOptionActivity.this, factoryDesk);
                            vifAdjustViewHolder.findView();
                            vifAdjustViewHolder.onCreate();

                            break;
                        case VD_ADJUST_INX:
                            currentAdvPage = VD_ADJUST_PAGE;
                            advancedOptionListViewHolder.advancedViewFlipper
                                    .setDisplayedChild(VD_ADJUST_PAGE);
                            vdAdjustListViewHolder = new VDAdjustListViewHolder(
                                    AdvancedOptionActivity.this, factoryDesk);
                            vdAdjustListViewHolder.findView();
                            vdAdjustListViewHolder.onCreate();
                            vdAdjustListViewHolder.recoverFocus();

                            break;
                        case IP_MAPPING_INX:
                            currentAdvPage = IP_MAPPING_PAGE;
                            advancedOptionListViewHolder.advancedViewFlipper
                                    .setDisplayedChild(IP_MAPPING_PAGE);
                            ipMappingViewHolder = new IPEnableMappingViewHolder(
                                    AdvancedOptionActivity.this, factoryDesk);
                            // ipMappingViewHolder.findView();
                            ipMappingViewHolder.onCreate();
                            ipMappingViewHolder.recoverFocus();

                            break;
                        case MAC_SCANNING_INX:
                            currentAdvPage = MAC_SCANNING_PAGE;
                            advancedOptionListViewHolder.advancedViewFlipper
                                    .setDisplayedChild(MAC_SCANNING_PAGE);
                            facScanningViewHolder = new FactoryScanningViewHolder(
                                    AdvancedOptionActivity.this, factoryDesk);
                            // ipMappingViewHolder.findView();
                            facScanningViewHolder.facScanSetType(0);
                            facScanningViewHolder.onCreate();

                            break;
                        case WIFIMAC_SCANNING_INX:
                            currentAdvPage = WIFIMAC_SCANNING_PAGE;
                            advancedOptionListViewHolder.advancedViewFlipper
                                    .setDisplayedChild(MAC_SCANNING_PAGE);
                            facScanningViewHolder = new FactoryScanningViewHolder(
                                    AdvancedOptionActivity.this, factoryDesk);
                            // ipMappingViewHolder.findView();
                            facScanningViewHolder.facScanSetType(1);
                            facScanningViewHolder.onCreate();

                            break;
                        case MAC_WRITING_INX:
                            currentAdvPage = MAC_WRITING_PAGE;
                            advancedOptionListViewHolder.advancedViewFlipper
                                    .setDisplayedChild(MAC_WRITING_PAGE);
                            facWritingViewHolder = new FactoryWritingViewHolder(
                                    AdvancedOptionActivity.this, factoryDesk);
                            facWritingViewHolder.facWriteSetType(0);
                            facWritingViewHolder.onCreate();

                            break;
                        case WIFIMAC_WRITING_INX:
                            currentAdvPage = WIFIMAC_WRITING_PAGE;
                            advancedOptionListViewHolder.advancedViewFlipper
                                    .setDisplayedChild(MAC_WRITING_PAGE);
                            facWritingViewHolder = new FactoryWritingViewHolder(
                                    AdvancedOptionActivity.this, factoryDesk);
                            facWritingViewHolder.facWriteSetType(1);
                            facWritingViewHolder.onCreate();
                            bRet = facWritingViewHolder.dispatchKeyEvent(event);

                            break;
                        case SFU_AUTO_TEST_INX:
                            Intent intent = new Intent(
                                    "android_serialport_api.intent.action.MainActivity");
                            intent.putExtra("TEST_MODE", 2);
                            startActivity(intent);
                            finish();
                            break;
                        default:
                            break;

                    }
                    break;
                case KeyEvent.KEYCODE_BACK:

                case KeyEvent.KEYCODE_MENU:
                    // this.designMenuActivity.returnMainmenu(3);
                    finish();
                    return true;
                default:
                    break;
            }
        }
        return super.dispatchKeyEvent(event);
    }

    public void returnAdvancedOptionView(int pageIdx) {
        Log.d(TAG, "====================== returnAdvancedOptionView:pageIdx =" + pageIdx);
        currentAdvPage = ADV_MAIN_PAGE;
        advancedOptionListViewHolder.advancedViewFlipper.setDisplayedChild(ADV_MAIN_PAGE);
        advancedOptionListViewHolder.setListViewData();
        advancedOptionListViewHolder.getListView().setSelection(pageIdx);

    }

}


viewHolder.java
//<MStar Software>
//******************************************************************************
// MStar Software
// Copyright (c) 2010 - 2014 MStar Semiconductor, Inc. All rights reserved.
// All software, firmware and related documentation herein ("MStar Software") are
// intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
// law, including, but not limited to, copyright law and international treaties.
// Any use, modification, reproduction, retransmission, or republication of all
// or part of MStar Software is expressly prohibited, unless prior written
// permission has been granted by MStar.
//
// By accessing, browsing and/or using MStar Software, you acknowledge that you
// have read, understood, and agree, to be bound by below terms ("Terms") and to
// comply with all applicable laws and regulations:
//
// 1. MStar shall retain any and all right, ownership and interest to MStar
//    Software and any modification/derivatives thereof.
//    No right, ownership, or interest to MStar Software and any
//    modification/derivatives thereof is transferred to you under Terms.
//
// 2. You understand that MStar Software might include, incorporate or be
//    supplied together with third party's software and the use of MStar
//    Software may require additional licenses from third parties.
//    Therefore, you hereby agree it is your sole responsibility to separately
//    obtain any and all third party right and license necessary for your use of
//    such third party's software.
//
// 3. MStar Software and any modification/derivatives thereof shall be deemed as
//    MStar's confidential information and you agree to keep MStar's
//    confidential information in strictest confidence and not disclose to any
//    third party.
//
// 4. MStar Software is provided on an "AS IS" basis without warranties of any
//    kind. Any warranties are hereby expressly disclaimed by MStar, including
//    without limitation, any warranties of merchantability, non-infringement of
//    intellectual property rights, fitness for a particular purpose, error free
//    and in conformity with any international standard.  You agree to waive any
//    claim against MStar for any loss, damage, cost or expense that you may
//    incur related to your use of MStar Software.
//    In no event shall MStar be liable for any direct, indirect, incidental or
//    consequential damages, including without limitation, lost of profit or
//    revenues, lost or damage of data, and unauthorized system use.
//    You agree that this Section 4 shall still apply without being affected
//    even if MStar Software has been modified by MStar in accordance with your
//    request or instruction for your use, except otherwise agreed by both
//    parties in writing.
//
// 5. If requested, MStar may from time to time provide technical supports or
//    services in relation with MStar Software to you for your use of
//    MStar Software in conjunction with your or your customer's product
//    ("Services").
//    You understand and agree that, except otherwise agreed by both parties in
//    writing, Services are provided on an "AS IS" basis and the warranty
//    disclaimer set forth in Section 4 above shall apply.
//
// 6. Nothing contained herein shall be construed as by implication, estoppels
//    or otherwise:
//    (a) conferring any license or right to use MStar name, trademark, service
//        mark, symbol or any other identification;
//    (b) obligating MStar or any of its affiliates to furnish any person,
//        including without limitation, you and your customers, any assistance
//        of any kind whatsoever, or any information; or
//    (c) conferring any license or right under any intellectual property right.
//
// 7. These terms shall be governed by and construed in accordance with the laws
//    of Taiwan, R.O.C., excluding its conflict of law rules.
//    Any and all dispute arising out hereof or related hereto shall be finally
//    settled by arbitration referred to the Chinese Arbitration Association,
//    Taipei in accordance with the ROC Arbitration Law and the Arbitration
//    Rules of the Association by three (3) arbitrators appointed in accordance
//    with the said Rules.
//    The place of arbitration shall be in Taipei, Taiwan and the language shall
//    be English.
//    The arbitration award shall be final and binding to both parties.
//
//******************************************************************************
//<MStar Software>

package mstar.tvsetting.factory.ui.advancedoption;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import mstar.factorymenu.ui.R;
import mstar.tvsetting.factory.desk.IFactoryDesk;
import mstar.tvsetting.factory.ui.advancedoption.AdvancedOptionListViewHolder.AdvanceOptionListAdapter.ViewHolder;
import android.app.Instrumentation;
import android.content.Context;
import android.graphics.Color;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class CISettingListViewHolder {
    private AdvancedOptionActivity ciActivity;

    private IFactoryDesk factoryManager;

    protected LinearLayout linear_factory_ci_debug;
    
    protected List<Map<String, Object>> ci_debug_view_adapter_data ;
    
    protected ListView listview_ci_debug;
    
    protected int currentIndex = 0;
    
    protected static final int CI_PERFORMANCE_MONITOR_INX = 0;
    
    protected static final int CI_RESOURCE_MANAGER_INX = 1 ;
    
    protected static final int CI_APPLICATIION_INFOMATION_INX = 2 ;
    
    protected static final int CI_CONDITIONAL_ACCESS_AUPPORT_INX = 3 ;
    
    protected static final int CI_HOST_CONTROL_INX = 4 ;
    
    protected static final int CI_DATE_TIME_INX = 5 ;
    
    protected static final int CI_MAN_MACHINE_INTERFACE_INX = 6 ;
    
    protected static final int CI_LOW_SPEED_COMMUNICATION_INX = 7 ;
    
    protected static final int CI_CONTENT_CONTROL_INX = 8 ;
   
    protected static final int CI_HOSET_LANGUAGE_COUNTRY_INX = 9 ;
    
    protected static final int CI_CAM_UPGRADE_INX = 10 ;
    
    protected static final int CI_OPERATOR_PROFILE_INX = 11 ;
    
    protected static final int CI_SPECIFIC_APPLICATION_SUPPORT_INX = 12 ;
    
    protected static final int CI_APP_MAN_MACHINE_INTERFACE_INX = 13 ;
    
    protected static final int CI_PROGRAM_MAP_TABLE_INX = 14 ;
    
    protected static final int CI_HOST_SERVICE_SHUNNING_INX = 15 ;
    
    protected static final int CI_CANAL_READY_AUTHENTICATION_INX = 16 ;
    
    protected static final int CI_DEFAULT_TX_RX_INX = 17 ;

    private int monitorenableindex;

    private int[] cidebuglevel = {
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    };

    private String[] monitornable = {
            "Off", "On"
    };

    private final int CI_FUNCTION_DEBUG_COUNT = 17;
    
    private static final int[] textview_factory_ci_debug_list_name = new int[]
       {R.string.str_factory_ci_performance_monitor,R.string.str_factory_ci_resource_manager,
        R.string.str_factory_ci_application_infomation,R.string.str_factory_ci_conditional_access_support,
        R.string.str_factory_ci_host_control,R.string.str_factory_ci_date_time,
        R.string.str_factory_ci_man_machine_interface,R.string.str_factory_ci_low_speed_communication,
        R.string.str_factory_ci_content_control,R.string.str_factory_ci_host_language_country,
        R.string.str_factory_ci_cam_upgrade,R.string.str_factory_ci_operator_profile,
        R.string.str_factory_ci_specific_application_support,R.string.str_factory_ci_app_man_machine_interface,
        R.string.str_factory_ci_program_map_table,R.string.str_factory_ci_host_service_shunning,
        R.string.str_factory_ci_canal_ready_authentication,R.string.str_factory_ci_default_tx_rx};
    
    private TextView selected_item_value_tv = null;

    public CISettingListViewHolder(AdvancedOptionActivity activity, IFactoryDesk factoryManager) {
        ciActivity = activity;
        this.factoryManager = factoryManager;
    }

    public void findView() {
        Instrumentation inst = new Instrumentation();
        inst.setInTouchMode(false);//listView首次进入无焦点的解决办法。

        linear_factory_ci_debug = (LinearLayout) ciActivity
                .findViewById(R.id.linearlayout_factory_ci_debug);
        
        listview_ci_debug = (ListView)ciActivity.findViewById(R.id.listview_ci_debug);

        listview_ci_debug.setOnItemClickListener(new OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                // TODO Auto-generated method stub
                
            }
            
        });
        listview_ci_debug.setOnItemSelectedListener(new OnItemSelectedListener(){

            @Override
            public void onItemSelected(AdapterView<?> arg0, View view, int position, long arg3) {
                // TODO Auto-generated method stub
                currentIndex = position;
                selected_item_value_tv = (TextView)view.findViewById(R.id.textview_factory_ci_debug_item_value);
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
                
            }
            
        });        

    }

    public void onCreate() {
        monitorenableindex = factoryManager.getPerformanceMonitor() ? 1 : 0;
        //text_factory_ci_performance_monitor_val.setText(monitornable[monitorenableindex]);
        for (int i = 0; i < CI_FUNCTION_DEBUG_COUNT; i++) {
            cidebuglevel[i] = factoryManager.getCIDebugLevel(i);
        }
        ci_debug_view_adapter_data = getData();
        listview_ci_debug.setAdapter(new CiDebugListAdapter(ciActivity));
        
    }
    
    private int getCurrentIndex(){
        return currentIndex ;
    }

    public boolean dispatchKeyEvent(KeyEvent event) {
        // TODO Auto-generated method stub
        boolean bRet = true;
        currentIndex = getCurrentIndex();
        if(event.getAction()==KeyEvent.ACTION_DOWN && event.getAction() != KeyEvent.ACTION_UP){
        switch (event.getKeyCode()){
       //你的代码逻辑,比如
            case KeyEvent.KEYCODE_DPAD_RIGHT:
                switch (currentIndex) {
                    case CI_PERFORMANCE_MONITOR_INX:
                        if (monitorenableindex == 1) {
                            monitorenableindex = 0;
                            factoryManager.enablePerformanceMonitor(false);
                        } else {
                            monitorenableindex++;
                            factoryManager.enablePerformanceMonitor(true);
                        }
                        selected_item_value_tv
                                .setText(monitornable[monitorenableindex]);
                        break;
                    case CI_RESOURCE_MANAGER_INX:
                    case CI_APPLICATIION_INFOMATION_INX:
                    case CI_CONDITIONAL_ACCESS_AUPPORT_INX:
                    case CI_HOST_CONTROL_INX:
                    case CI_DATE_TIME_INX:
                    case CI_MAN_MACHINE_INTERFACE_INX:
                    case CI_LOW_SPEED_COMMUNICATION_INX:
                    case CI_CONTENT_CONTROL_INX:
                    case CI_HOSET_LANGUAGE_COUNTRY_INX:
                    case CI_CAM_UPGRADE_INX:
                    case CI_OPERATOR_PROFILE_INX:
                    case CI_SPECIFIC_APPLICATION_SUPPORT_INX:
                    case CI_APP_MAN_MACHINE_INTERFACE_INX:
                    case CI_PROGRAM_MAP_TABLE_INX:
                    case CI_HOST_SERVICE_SHUNNING_INX:
                    case CI_CANAL_READY_AUTHENTICATION_INX:
                    case CI_DEFAULT_TX_RX_INX:
                        int index = currentIndex -1 ;
                        if (cidebuglevel[index] == 4) {
                            cidebuglevel[index] = 0;
                        } else {
                            cidebuglevel[index]++;
                        }
                        factoryManager.setCIDebugLevel(index, cidebuglevel[index]);
                        selected_item_value_tv.setText(Integer.toString(cidebuglevel[index]));
                        break;
                }
                break;
            case KeyEvent.KEYCODE_DPAD_LEFT:
                switch (currentIndex) {
                    case CI_PERFORMANCE_MONITOR_INX:
                        if (monitorenableindex == 0) {
                            monitorenableindex = 1;
                            factoryManager.enablePerformanceMonitor(true);
                        } else {
                            monitorenableindex--;
                            factoryManager.enablePerformanceMonitor(false);
                        }
                        selected_item_value_tv
                                .setText(monitornable[monitorenableindex]);
                        break;
                    case CI_RESOURCE_MANAGER_INX:
                    case CI_APPLICATIION_INFOMATION_INX:
                    case CI_CONDITIONAL_ACCESS_AUPPORT_INX:
                    case CI_HOST_CONTROL_INX:
                    case CI_DATE_TIME_INX:
                    case CI_MAN_MACHINE_INTERFACE_INX:
                    case CI_LOW_SPEED_COMMUNICATION_INX:
                    case CI_CONTENT_CONTROL_INX:
                    case CI_HOSET_LANGUAGE_COUNTRY_INX:
                    case CI_CAM_UPGRADE_INX:
                    case CI_OPERATOR_PROFILE_INX:
                    case CI_SPECIFIC_APPLICATION_SUPPORT_INX:
                    case CI_APP_MAN_MACHINE_INTERFACE_INX:
                    case CI_PROGRAM_MAP_TABLE_INX:
                    case CI_HOST_SERVICE_SHUNNING_INX:
                    case CI_CANAL_READY_AUTHENTICATION_INX:
                    case CI_DEFAULT_TX_RX_INX:
                        int index = currentIndex -1;;
                        if (cidebuglevel[index] <= 0) {
                            cidebuglevel[index] = 4;
                        } else {
                            cidebuglevel[index]--;
                        }
                        factoryManager.setCIDebugLevel(index, cidebuglevel[index]);
                        selected_item_value_tv.setText(Integer.toString(cidebuglevel[index]));
                        break;
                }
                break;
            case KeyEvent.KEYCODE_BACK:
                this.ciActivity.returnAdvancedOptionView(3);
                break;
            case KeyEvent.KEYCODE_MENU:
                this.ciActivity.finish();
                break;
            default:
                bRet = false;
                break;
        }
        }
        return bRet;
    }
    
    
    /**
     * ListView数据填充
     * @return
     */
    private List<Map<String, Object>> getData() {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map;

        int length = textview_factory_ci_debug_list_name.length;
        
        String[] listName = new String[length];
        String[] listValue = new String[length];
        listName = getListNameFromId(textview_factory_ci_debug_list_name);
        listValue = getListDefaultValue(length);
        
        for (int i = 0; i < length; i++) {
            map = new HashMap<String, Object>();
            map.put("ci_debug_list_name", listName[i]);
            map.put("ci_debug_list_value",listValue[i]);
            list.add(map);
        }

        return list;
    }

    /**
     * @param str_id 列表名称的资源ID
     * @return
     */
    private String[] getListNameFromId(int[] str_id){
        String[] strs= new String[str_id.length];
        for(int i=0 ;i<str_id.length; i++){
            strs[i] = ciActivity.getString(str_id[i]);            
        }
        return strs;
        
    }
    /*
     * 设置初始化列表的默认值
     */
    private String[] getListDefaultValue(int list_length){
        String[] strs_value = new String[list_length];
        strs_value[0] = monitornable[monitorenableindex];
        for(int i= 1;i<list_length;i++){
            strs_value[i] =  Integer.toString(cidebuglevel[i-1]);  
        }
        return strs_value;
        
    }
    
    class CiDebugListAdapter extends BaseAdapter {
        private LayoutInflater mInflater;
        private Context mContext = null;
        
       public CiDebugListAdapter(Context context) {
           mContext = context;
           mInflater = LayoutInflater.from(mContext);
       }

       public Object getItem(int arg0) {
           // TODO Auto-generated method stub
           return arg0;
       }

       public long getItemId(int position) {
           // TODO Auto-generated method stub
           return position;
       }

       public int getCount() {
           // TODO Auto-generated method stub
           return ci_debug_view_adapter_data.size();
       }

       public View getView(int position, View convertView,    android.view.ViewGroup parent) {
           final ViewHolder viewHolder;

           if (convertView == null) {
               viewHolder = new ViewHolder();
               convertView = mInflater.inflate(R.layout.ci_adjust_item, null);
               viewHolder.nameTv= (TextView) convertView.findViewById(R.id.textview_factory_ci_debug_item_name);
               viewHolder.valueTv= (TextView) convertView.findViewById(R.id.textview_factory_ci_debug_item_value);
               convertView.setTag(viewHolder);
           } 
           else {
               viewHolder = (ViewHolder) convertView.getTag();
           }
           viewHolder.nameTv.setText((String) ci_debug_view_adapter_data.get(position).get("ci_debug_list_name"));
           viewHolder.valueTv.setText((String) ci_debug_view_adapter_data.get(position).get("ci_debug_list_value"));


           return convertView;
       }


   public class ViewHolder {
       public TextView nameTv;
       public TextView valueTv;
   }
   }
    
    public void recoverFocus(){
        listview_ci_debug.requestFocus();
        listview_ci_debug.setSelection(0);        
    }

ci_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/background_style" >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="36dp"
        android:layout_marginLeft="5dp"
        android:gravity="left"
        android:text="@string/str_factory_ci_setting"
        android:textColor="#ffffff"
        android:textSize="25sp" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:background="#ffffff"
        android:gravity="center" />

    <LinearLayout
        android:id="@+id/linearlayout_factory_ci_debug"
        android:layout_width="370dp"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:orientation="vertical" >
    <ListView
        android:id="@+id/listview_ci_debug"
        android:layout_width="370dp"
        android:layout_height="wrap_content"
        android:divider="#00000000"
        android:dividerHeight="0dip"
        android:listSelector="@drawable/button_state" >
    </ListView>
    </LinearLayout>

</LinearLayout>



ci_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minHeight="26dp"
    android:layout_gravity="center"
    android:background="@drawable/button_state"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textview_factory_ci_debug_item_name"
        android:layout_width="200dp"
        android:layout_height="fill_parent"
        android:gravity="left|center_vertical"
        android:textColor="#ffffff"
        android:textSize="13sp" />

    <TextView
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_marginLeft="20dp"
        android:background="@drawable/left"
        android:gravity="center_horizontal" />

    <TextView
        android:id="@+id/textview_factory_ci_debug_item_value"
        android:layout_width="70dp"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:textColor="#ffffff"
        android:textSize="13sp" />

    <TextView
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:background="@drawable/right"
        android:gravity="center_horizontal" />

</LinearLayout>





  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页

打赏作者

菱芯草

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值