Android TV 使用Fragment搭建界面框架(一)

使用FragmentActivity+FragmentTabHost+Fragement模式

//定义FragmentTabHost对象
private FragmentTabHost mTabHost;

//定义数组来存放Fragment界面
private static Class<?>[] fragmentArray;

//定义Tab选项卡文字、图片
private static final int[] aiTabWidgetIcon;
private static final int[] astrTabWidgetTitle;

//静态代码块实例化Fragment数组、Tab选项卡文字、图片
    static
    {
        aiTabWidgetIcon = new int[]{R.drawable.icon_eth, R.drawable.icon_wifi, R.drawable.icon_3g, R.drawable.icon_vpn, R.drawable.icon_bluetooth};
        astrTabWidgetTitle = new int[]{R.string.STR_LAN, R.string.STR_WIRELESS, R.string.STR_3G, R.string.STR_VPN, R.string.STR_BLUETOOTH};
        fragmentArray = new Class<?>[]{EthernetFragment.class, WifiFragment.class, Net3gFragment.class, VpnFragment.class, BluetoothFragment.class};
    }

//实例化TabHost对象,得到TabHost
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

//得到fragment的个数
int count = fragmentArray.length;

//为每一个Tab按钮设置图标、文字和内容
for (int i = 0; i < count; i++)
      {
           View view = LayoutInflater.from(this).inflate(R.layout.tabwidget_style, null);
           view.setLayoutParams(new LayoutParams(getWindowManager().getDefaultDisplay().getWidth() /aiTabWidgetIcon.length, 60));
           TextView textview = (TextView) view.findViewById(R.id.tabwidget_name);
            ImageView icon = (ImageView) view.findViewById(R.id.tabwidget_icon);
            textview.setText(astrTabWidgetTitle[i]);
            icon.setBackgroundResource(aiTabWidgetIcon[i]);

            //设置每个Tab按钮背景,其中背景根据按钮不同状态相应变化
            view.setBackgroundResource(R.drawable.tabwidget_selector);

            //创建TabHost.TabSpec用来管理Tag、Content、Indicator
            TabSpec tabSpec = mTabHost.newTabSpec(getString(astrTabWidgetTitle[i])).setIndicator(view);
 
            //将Tab按钮添加进Tab选项卡中
            mTabHost.addTab(tabSpec, fragmentArray[i], null);
        }

TabHost是整个Tab的容器,包括两部分---TabWidget和FrameLayout;TabWidget就是每个tab的标签,FrameLayout则是tab内容。

//设置TabHost的背景
mTabHost.getTabWidget().setBackgroundResource(R.drawable.tabwidget_bg);

//按键事件
 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        //获取当前的Tab索引
        int iCurTab = mTabHost.getCurrentTab();

        //根据当前Tab的tag得到当前Fragment
        Fragment curFg = getSupportFragmentManager().findFragmentByTag(mTabHost.getCurrentTabTag());
        if (curFg instanceof OnkeyDownListener)
        {
            if (((OnkeyDownListener) curFg).onKeyDown(keyCode, event))
                return true;
        }
        switch (keyCode)
        {
            case KEY_LEFT://向左切换
                Log.d(TAG, " KEY_LEFT iCurTab = " + iCurTab);
                if (!moreThanLastKeyTime(300))
                {
                    return true;
                }
                if (iCurTab > 0)
                {//根据index设置当前显示哪个Tab(也可以根据tag设置当前显示哪个Tab,调用setCurrentTabByTag方法)
                    mTabHost.setCurrentTab(iCurTab - 1);
                }
                else if (iCurTab == 0)
                {
                    mTabHost.setCurrentTab(fragmentArray.length - 1);
                }
                return true;

            case KEY_RIGHT://向右切换
                Log.d(TAG, " KEY_RIGHT iCurTab = " + iCurTab);
                if (moreThanLastKeyTime(300))
                {
                    return true;
                }
                if (iCurTab < (fragmentArray.length - 1))
                {
                    mTabHost.setCurrentTab(iCurTab + 1);
                }
                else if (iCurTab == (fragmentArray.length - 1))
                {
                    mTabHost.setCurrentTab(0);
                }
                return true;
        }
        Log.d(TAG, "exec onKeyDown " + keyCode);
        return super.onKeyDown(keyCode, event);
    }

相关xml布局

activity_network_setting.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/settings_background" >
    <com.john.alien.uistyle.TextImageView
        android:id="@+id/network_header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/menu_beyond_top_item"
        android:layout_marginLeft="@dimen/menu_title_margin_left"
        android:layout_marginTop="@dimen/menu_title_margin_top"
        android:focusable="false"
        android:gravity="center_vertical" />
    <android.support.v4.app.FragmentTabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/network_header"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:focusable="false" >
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0"
            android:focusable="false" />
    </android.support.v4.app.FragmentTabHost>
    <FrameLayout
        android:id="@+id/realtabcontent"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1" />
    <TextView
        android:id="@+id/server_status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="115dp"
        android:textSize="@dimen/text_size_normal"
        android:visibility="invisible" />
</RelativeLayout>

tabwidget_style.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="60dp"
    android:gravity="center"
    android:orientation="horizontal"
    android:paddingLeft="5dp"
    android:paddingRight="5dp" >
    <ImageView
        android:id="@+id/tabwidget_icon"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp"
        android:gravity="center"/>
    <TextView
        android:id="@+id/tabwidget_name"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:gravity="center_vertical|left"
        android:textSize="@dimen/text_size_normal" />
</LinearLayout>

CustomFragmentImpl.java

public class CustomFragmentImpl
{
    public static interface OnkeyDownListener
    {
        public boolean onKeyDown(int keyCode, KeyEvent event);
    }
    
    public static interface WindowFocusChangeListener
    {
        public void onWindowFocusChanged(boolean hasFocus);
    }
}

最后,每个Fragment需要实现CustomFragmentImpl.OnkeyDownListener接口

Android使用Fragment搭建界面框架(二)

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哈哈,柳暗花明

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

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

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

打赏作者

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

抵扣说明:

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

余额充值