【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
第一部分就是你们现在看的这个贴,主要功能:实现fragment_+tabhost来搭建整个app的大体UI框架,底部是一个可切换的选项卡(安智市场中的底部选项卡的切换动画效果略过,没写,太困了,码完字去睡觉)。第二部分:也就是这个即将写的下一个帖子,因为今天实在太晚了,明天还要上班(Zzzzz...),主要功能:使用viewpagerindicator控件,实现左右滑动加载分页数据,还有android 4.2更新的新特性:Fragment in fragment(嵌套)。 这部分很快会更新。真的。
<br>现在来看看安智市场的app效果
<br>然后来看看现在这个帖子要实现的功能的demo效果 不会很难看吧,见笑了!呵呵!!!动画效果没做。木有时间。
<br>现在来说说代码,其实我之前在eoe写过一个类似的,不过现在理解更深刻,有改进一部分。
首先必须是布局文件啦,主要的布局文件是:activity_main.xml
- <TabHost
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@android :id/tabhost"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
-
- <LinearLayout
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
-
- <FrameLayout
- android:id="@+id/realtabcontent"
- android:layout_width="match_parent"
- android:layout_height="0dp"
- android:layout_weight="1"/>
-
- <TabWidget
- android:id="@android :id/tabs"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:tabStripEnabled="false"
- android:background="@drawable/bg_nav"
- android:layout_weight="0"/>
-
- <FrameLayout
- android:id="@android :id/tabcontent"
- android:layout_width="0dp"
- android:layout_height="0dp"
- android:layout_weight="0"/>
-
- </LinearLayout>
- </TabHost>
接下来,最重要的,当然是fragment咯,首先如果有和我一样无知的孩子,还是得先了解一下什么是fragment吧。
传送门:http://developer.android.com/reference/android/support/v4/app/Fragment.html
然后,我小小的说明一下咯,fragment是在android sdk 3.0之后出现的,为了给用户带来更好的UE,解决不同屏幕分辩率的动态和灵活UI设计。大屏幕如平板小屏幕如手机,平板电脑的设计使得其有更多的空间来放更多的UI组件,而多出来的空间存放UI使其会产生更多的交互,从而诞生了fragments。fragment有它自己的一套生命周期机制,和activity一样。不过它必须拥有一个activity宿主,以此管理这些fragments。要使用fragment,必须引入v4类库包(android-support-v4.jar),这样虽然fragment是在3.0以后出现的,也同样可以在低版本中使用。
上代码吧,我词穷。
- package org.mcdull.activity;
-
- import org.mcdull.mainfragment.FifthFragment;
- import org.mcdull.mainfragment.FourthFragment;
- import org.mcdull.mainfragment.FristFragment;
- import org.mcdull.mainfragment.SecondFragment;
- import org.mcdull.mainfragment.ThirdFragment;
- import org.mcdull.util.DummyTabContent;
-
- import android.os.Bundle;
- import android.support.v4.app.FragmentActivity;
- import android.support.v4.app.FragmentManager;
- import android.support.v4.app.FragmentTransaction;
- import android.view.LayoutInflater;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.RelativeLayout;
- import android.widget.TabHost;
- import android.widget.TabWidget;
- import android.widget.TextView;
-
- /**
- * @description:
-
- *
- * @author: hades
- *
- * @update: 2013-3-7
- *
- * @version: 1.0
- *
- * @email:mcdull525@foxmail.com
- */
- public class MainActivity extends FragmentActivity {
-
- private TabHost mTabHost;
- TabWidget tabWidget;
- private FragmentTransaction mFt;
- private RelativeLayout mTabIndicator_home, mTabIndicator_app,
- mTabIndicator_game, mTabIndicator_top, mTabIndicator_setting;
- private FristFragment mFristFragment;
- private SecondFragment mSecondFragment;
- private ThirdFragment mThirdFragment;
- private FourthFragment mFourthFragment;
- private FifthFragment mFifthFragment;
- private FragmentManager mFm = getSupportFragmentManager();
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- findTabView();
- mTabHost.setup();
-
- TabHost.OnTabChangeListener tabChangeListener = new TabHost.OnTabChangeListener() {
-
- @Override
- public void onTabChanged(String tabId) {
-
- mFt = mFm.beginTransaction();
-
- // TODO Auto-generated method stub
- if (mFristFragment != null) {
- mFt.hide(mFristFragment);
- }
- if (mSecondFragment != null) {
- mFt.hide(mSecondFragment);
- }
- if (mThirdFragment != null) {
- mFt.hide(mThirdFragment);
- }
- if (mFourthFragment != null) {
- mFt.hide(mFourthFragment);
- }
- if (mFifthFragment != null) {
- mFt.hide(mFifthFragment);
- }
-
- if (tabId.equalsIgnoreCase("home")) {
- attachTabHome();
- } else if (tabId.equalsIgnoreCase("app")) {
- attachTabApp();
- } else if (tabId.equalsIgnoreCase("game")) {
- attachTabGame();
- } else if (tabId.equalsIgnoreCase("top")) {
- attachTabTop();
- } else if (tabId.equalsIgnoreCase("setting")) {
- attachTabSetting();
- }
- // 执行Fragment事务(添加。移除,替换fragment)
- mFt.commit();
- }
- };
- mTabHost.setOnTabChangedListener(tabChangeListener);
- initTab();
- //默认跳到第几个选项卡
- mTabHost.setCurrentTab(0);
- }
-
- /**
- * 找到TabHost的相关布局,并绘制每一个选项卡
-
- */
- private void findTabView() {
-
- mTabHost = (TabHost) findViewById(android.R.id.tabhost);
- tabWidget = (TabWidget) findViewById(android.R.id.tabs);
- LinearLayout layout = (LinearLayout) mTabHost.getChildAt(0);
- TabWidget tabWidget = (TabWidget) layout.getChildAt(1);
-
- mTabIndicator_home = (RelativeLayout) LayoutInflater.from(this)
- .inflate(R.layout.tab_indicator, tabWidget, false);
- ImageView ivTab1 = (ImageView) mTabIndicator_home.getChildAt(0);
- ivTab1.setImageResource(R.drawable.selector_menu_home_btu);
- TextView tvTab1 = (TextView) mTabIndicator_home.getChildAt(1);
- tvTab1.setText(R.string.home);
-
- mTabIndicator_app = (RelativeLayout) LayoutInflater.from(this).inflate(
- R.layout.tab_indicator, tabWidget, false);
- ImageView ivTab2 = (ImageView) mTabIndicator_app.getChildAt(0);
- ivTab2.setImageResource(R.drawable.selector_menu_app_btu);
- TextView tvTab2 = (TextView) mTabIndicator_app.getChildAt(1);
- tvTab2.setText(R.string.app);
-
- mTabIndicator_game = (RelativeLayout) LayoutInflater.from(this)
- .inflate(R.layout.tab_indicator, tabWidget, false);
- ImageView ivTab3 = (ImageView) mTabIndicator_game.getChildAt(0);
- ivTab3.setImageResource(R.drawable.selector_menu_game_btu);
- TextView tvTab3 = (TextView) mTabIndicator_game.getChildAt(1);
- tvTab3.setText(R.string.game);
-
- mTabIndicator_top = (RelativeLayout) LayoutInflater.from(this).inflate(
- R.layout.tab_indicator, tabWidget, false);
- ImageView ivTab4 = (ImageView) mTabIndicator_top.getChildAt(0);
- ivTab4.setImageResource(R.drawable.selector_menu_top_btu);
- TextView tvTab4 = (TextView) mTabIndicator_top.getChildAt(1);
- tvTab4.setText(R.string.top);
-
- mTabIndicator_setting = (RelativeLayout) LayoutInflater.from(this)
- .inflate(R.layout.tab_indicator, tabWidget, false);
- ImageView ivTab5 = (ImageView) mTabIndicator_setting.getChildAt(0);
- ivTab5.setImageResource(R.drawable.selector_menu_setting_btu);
- TextView tvTab5 = (TextView) mTabIndicator_setting.getChildAt(1);
- tvTab5.setText(R.string.setting);
-
- }
-
- /**
- * 以下一系列方法都是判断fragment是否已经创建,防止重复add进FragmentTransaction
-
- * FragmentTransaction中提供了show,add,replace,hide等一系列方法,可以根据不同的需求
-
- * 使用,这里demo使用了show方法,是因为避免使用replace之后会重新创建fragment,重新加载数据
-
- */
- private void attachTabHome() {
-
- if (mFristFragment == null) {
- mFristFragment = new FristFragment();
- mFt.add(R.id.realtabcontent, mFristFragment, "home");
- } else {
- mFt.show(mFristFragment);
-
- }
- }
-
- private void attachTabApp() {
-
- if (mSecondFragment == null) {
- mSecondFragment = new SecondFragment();
- mFt.add(R.id.realtabcontent, mSecondFragment, "app");
- } else {
- mFt.show(mSecondFragment);
- }
- }
-
- private void attachTabGame() {
-
- if (mThirdFragment == null) {
- mThirdFragment = new ThirdFragment();
- mFt.add(R.id.realtabcontent, mThirdFragment, "game");
- } else {
- mFt.show(mThirdFragment);
- }
- }
-
- private void attachTabTop() {
-
- if (mFourthFragment == null) {
- mFourthFragment = new FourthFragment();
- mFt.add(R.id.realtabcontent, mFourthFragment, "top");
- } else {
- mFt.show(mFourthFragment);
- }
- }
-
- private void attachTabSetting() {
-
- if (mFifthFragment == null) {
- mFifthFragment = new FifthFragment();
- mFt.add(R.id.realtabcontent, mFifthFragment, "setting");
- } else {
- mFt.show(mFifthFragment);
- }
- }
-
- /**
- * 初始化选项卡
-
- */
- private void initTab() {
-
- TabHost.TabSpec tSpecHome = mTabHost.newTabSpec("home");
- tSpecHome.setIndicator(mTabIndicator_home);
- tSpecHome.setContent(new DummyTabContent(getBaseContext()));
- mTabHost.addTab(tSpecHome);
-
- TabHost.TabSpec tSpecApp = mTabHost.newTabSpec("app");
- tSpecApp.setIndicator(mTabIndicator_app);
- tSpecApp.setContent(new DummyTabContent(getBaseContext()));
- mTabHost.addTab(tSpecApp);
-
- TabHost.TabSpec tSpecGame = mTabHost.newTabSpec("game");
- tSpecGame.setIndicator(mTabIndicator_game);
- tSpecGame.setContent(new DummyTabContent(getBaseContext()));
- mTabHost.addTab(tSpecGame);
-
- TabHost.TabSpec tSpecTop = mTabHost.newTabSpec("top");
- tSpecTop.setIndicator(mTabIndicator_top);
- tSpecTop.setContent(new DummyTabContent(getBaseContext()));
- mTabHost.addTab(tSpecTop);
-
- TabHost.TabSpec tSpecSetting = mTabHost.newTabSpec("setting");
- tSpecSetting.setIndicator(mTabIndicator_setting);
- tSpecSetting.setContent(new DummyTabContent(getBaseContext()));
- mTabHost.addTab(tSpecSetting);
-
- }
-
- }
第二: private FragmentManager mFm = getSupportFragmentManager(); 第一点继承了fragmentActivity后,可通过 getSupportFragmentManager方法拿到FragmentManager, 这个类提供了与Activity内部的Fragment对象进行交互的接口,其他的看代码撒。
来源:oschina
链接:https://my.oschina.net/u/924450/blog/181996