您的位置:首页 > 移动开发 > Android开发

Android通过动态加载Fragment实现TabActivity效果

2015-10-31 21:41 537 查看
Fragment算是android学习中级的知识点了,一直很神往,今天也学习浅显的知识,并用它实现简单的TabActivity效果。

根据我的理解,Fragment可以算是简易版的Activity,更方便调用,将可能重复使用到的部分做成fragment,可以在不同的activity里调用到,并且同一个activity可以调用多个fragment,代码可以显得干净整洁,可读性强。



Fragment和Activity有很相似的生命周期,也会有个布局文件,对布局文件的操作一般放在函数onCreateView里,今天做个小例子,布局控件都很简单,忽略不急。这里一共创建了3个fragment,作为TabActivity切换显示使用。TabActivity里最下方添加了三个按钮,按下每个按钮时,分别在页面显示一个fragment,并隐藏另外两个,原理很简单。具体实现如下:

package com.huiyu.hornsey.fragmentdemo;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

public class MainActivity extends Activity implements View.OnClickListener {
private static final String TAG = "MainActivity";
private FragmentTransaction ft;
private Fragment homeFragment, messageFragment, meFragment, currentFragment;
private LinearLayout homeLayout, messageLayout, meLayout;

@Override
 protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

initView();
initTab();

}

private void initTab() {
LinearLayout layout;

homeLayout.setBackgroundColor(getResources().getColor(R.color.white));
messageLayout.setBackgroundColor(getResources().getColor(R.color.white));
meLayout.setBackgroundColor(getResources().getColor(R.color.white));

if (!homeFragment.isAdded()) {
ft.add(R.id.container, homeFragment).commit();
currentFragment = homeFragment;

homeLayout.setBackgroundColor(getResources().getColor(R.color.beike_blue_light));
}

}

private void initView() {
ft = getFragmentManager().beginTransaction();
homeFragment = new HomeFragment();
meFragment = new MeFragment();
messageFragment = new MessageFragment();

homeLayout = (LinearLayout) findViewById(R.id.tab_home);
messageLayout = (LinearLayout) findViewById(R.id.tab_message);
meLayout = (LinearLayout) findViewById(R.id.tab_me);
homeLayout.setOnClickListener(this);
messageLayout.setOnClickListener(this);
meLayout.setOnClickListener(this);

}

@Override
 public void onClick(View v) {
switch (v.getId()) {
case R.id.tab_home:
clickLayoutHome();
break;
case R.id.tab_message:
clickLayoutMessage();
break;
case R.id.tab_me:
clickLayoutMe();
break;
default:
break;
}
}

private void clickLayoutHome() {
homeLayout.setBackgroundColor(getResources().getColor(R.color.white));
messageLayout.setBackgroundColor(getResources().getColor(R.color.white));
meLayout.setBackgroundColor(getResources().getColor(R.color.white));

homeLayout.setBackgroundColor(getResources().getColor(R.color.beike_blue_light));

addOrShowFragment(homeFragment);
}

private void clickLayoutMessage() {
homeLayout.setBackgroundColor(getResources().getColor(R.color.white));
messageLayout.setBackgroundColor(getResources().getColor(R.color.white));
meLayout.setBackgroundColor(getResources().getColor(R.color.white));

messageLayout.setBackgroundColor(getResources().getColor(R.color.beike_blue_light));

addOrShowFragment(messageFragment);
}

private void clickLayoutMe() {
homeLayout.setBackgroundColor(getResources().getColor(R.color.white));
messageLayout.setBackgroundColor(getResources().getColor(R.color.white));
meLayout.setBackgroundColor(getResources().getColor(R.color.white));

meLayout.setBackgroundColor(getResources().getColor(R.color.beike_blue_light));

addOrShowFragment(meFragment);
}

private void addOrShowFragment(Fragment fragment) {
if (currentFragment != fragment) {
if (!fragment.isAdded()) {
getFragmentManager().beginTransaction().hide(currentFragment).add(R.id.container, fragment).commit();
} else {
getFragmentManager().beginTransaction().hide(currentFragment).show(fragment).commit();
}
currentFragment = fragment;
}

}
}


理解浅陋,不足之处,多多指正。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: