MainActivity/activity_main/bottombar

package com.example.phonefragment;

import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

TextView txt_tab_class;
TextView txt_tab_resource;
TextView txt_tab_my;

// Fragment Object
ClassFragment classFragment;
SourceFragment sourceFragment;
MyFragment myFragment;

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

classFragment = new ClassFragment();
sourceFragment = new SourceFragment();
myFragment = new MyFragment();

txt_tab_class = (TextView) findViewById(R.id.txt_tab_class);
txt_tab_resource = (TextView) findViewById(R.id.txt_tab_resource);
// txt_tab_quiz = (TextView) findViewById(R.id.txt_tab_quiz);
txt_tab_my = (TextView) findViewById(R.id.txt_tab_my);

txt_tab_class.setOnClickListener((OnClickListener) MainActivity.this);
txt_tab_resource
.setOnClickListener((OnClickListener) MainActivity.this);
// txt_tab_quiz.setOnClickListener((OnClickListener) MainActivity.this);
txt_tab_my.setOnClickListener((OnClickListener) MainActivity.this);

Intent loginintent = this.getIntent();
Bundle logingetbundle = loginintent.getExtras(); // 获取intent里面的bundle对象
String tel = logingetbundle.getString("tel"); // 获取Bundle里面的字符串

Bundle myputbundle = new Bundle();
myputbundle.putString("tel", tel);
myFragment.setArguments(myputbundle);
classFragment.setArguments(myputbundle);

// 设置默认的Fragment
setDefaultFragment();
}

@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
View v = getWindow().getDecorView();
onClick(v);
}

private void setDefaultFragment() {
FragmentManager fManager = getFragmentManager();
FragmentTransaction transaction = fManager.beginTransaction();
setSelected();
txt_tab_class.setSelected(true);
transaction.add(R.id.id_content, classFragment);
transaction.show(classFragment);
transaction.commit();
}

// 将所有文本的选中状态置为否
private void setSelected() {
txt_tab_class.setSelected(false);
txt_tab_resource.setSelected(false);
// txt_tab_quiz.setSelected(false);
txt_tab_my.setSelected(false);
}

public void onClick(View v) {
FragmentManager fManager = getFragmentManager();
FragmentTransaction fTransaction = fManager.beginTransaction();// 开启一个事务

switch (v.getId()) {
case R.id.txt_tab_class:
setSelected();
txt_tab_class.setSelected(true);

if (sourceFragment != null) {
fTransaction.hide(sourceFragment);
}
if (myFragment != null) {
fTransaction.hide(myFragment);
}

fTransaction.show(classFragment);

break;
case R.id.txt_tab_resource:
setSelected();
txt_tab_resource.setSelected(true);

if (classFragment != null) {
fTransaction.hide(classFragment);
}
if (myFragment != null) {
fTransaction.hide(myFragment);
}
if(!sourceFragment.isAdded()){
fTransaction.add(R.id.id_content, sourceFragment);
fTransaction.show(sourceFragment);
}else {
fTransaction.show(sourceFragment);
}
break;
case R.id.txt_tab_my:

setSelected();
txt_tab_my.setSelected(true);

if (classFragment != null) {
fTransaction.hide(classFragment);
}
if (sourceFragment != null) {
fTransaction.hide(sourceFragment);
}
if(!myFragment.isAdded()){
fTransaction.add(R.id.id_content, myFragment);
fTransaction.show(myFragment);
}else {
fTransaction.show(myFragment);
}
break;
}
fTransaction.commit();
}
}

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<include
android:id="@+id/ly_main_bottombar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
layout="@layout/fragment_bottombar" />

<View
android:id="@+id/div_main_tabbar"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_above="@id/ly_main_bottombar"
android:background="@color/lightgray" />

<FrameLayout
android:id="@+id/id_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@id/ly_main_bottombar" />

</RelativeLayout>

 

<?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="56dp"
android:layout_alignParentBottom="true"
android:background="@color/white"
android:orientation="horizontal" >

<TextView
android:id="@+id/txt_tab_class"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/tab_menu_bg"
android:drawablePadding="3dp"
android:drawableTop="@drawable/tab_menu_class"
android:gravity="center"
android:padding="5dp"
android:text="@string/classname"
android:textColor="@drawable/tab_menu_text"
android:textSize="16sp" />

<TextView
android:id="@+id/txt_tab_resource"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/tab_menu_bg"
android:drawablePadding="3dp"
android:drawableTop="@drawable/tab_menu_homework"
android:gravity="center"
android:padding="5dp"
android:text="@string/resource"
android:textColor="@drawable/tab_menu_text"
android:textSize="16sp" />


<TextView
android:id="@+id/txt_tab_my"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/tab_menu_bg"
android:drawablePadding="3dp"
android:drawableTop="@drawable/tab_menu_my"
android:gravity="center"
android:padding="5dp"
android:text="@string/my"
android:textColor="@drawable/tab_menu_text"
android:textSize="16sp" />

</LinearLayout>

posted @ 2017-03-06 15:23  音为  阅读(232)  评论(0编辑  收藏  举报