上次有写一篇“Fragment+RadioButton组合构成的底部导航栏的实现”的博客,
本次写这篇博客:1、想用百度贴吧底部导航栏的方式实现;2、切换Fragment时用hide和add的方式进行切换,这样不会重新实例化。
直接代码贴上:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import java.util.List;
/**
* 采用LinearLayout+TextView的方式构成的底部导航栏,重复切换页面不会重新加载
*/
public class MainActivity extends FragmentActivity {
/**数组的形式*/
private Fragment[] mFragments;
/**fragment管理*/
private FragmentManager fragmentManager;
/**fragment的事务控制器*/
FragmentTransaction fragmentTransaction;
private int last_Fragment = 0;
private LinearLayout tab_main;
private LinearLayout tab_vip;
private LinearLayout tab_tba;
private LinearLayout tab_mine;
/** 红点*/
private ImageView bar_point;
private int checkedNum = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ly_main);
bar_point = (ImageView) findViewById(R.id.bar_point);
tab_main = (LinearLayout) findViewById(R.id.tab_main);
tab_tba = (LinearLayout) findViewById(R.id.tab_tba);
tab_vip = (LinearLayout) findViewById(R.id.tab_vip);
tab_mine = (LinearLayout) findViewById(R.id.tab_mine);
initmFagments();
}
/**初始化Fragment*/
private void initmFagments() {
mFragments = new Fragment[4];
fragmentManager = getSupportFragmentManager();
mFragments[0] = fragmentManager.findFragmentById(R.id.fr_main);
mFragments[1] = fragmentManager.findFragmentById(R.id.fr_tba);
mFragments[2] = fragmentManager.findFragmentById(R.id.fr_vip);
mFragments[3] = fragmentManager.findFragmentById(R.id.fr_mine);
fragmentTransaction = fragmentManager.beginTransaction().hide(mFragments[0]).hide(mFragments[1]).hide(mFragments[2]).hide(mFragments[3]);
fragmentTransaction.show(mFragments[0]).commit();
tab_main.setSelected(true);
}
public void switchContent(Fragment from, Fragment to) {
FragmentTransaction transaction = fragmentManager.beginTransaction();
if (!to.isAdded()) {
transaction.hide(from).add(R.id.fr_main, to).commit();
} else {
transaction.hide(from).show(to).commit();
}
}
/**
* 点击切换不同tab
*/
public void tab_click(View view) {
switch (view.getId()) {
case R.id.tab_main:
tab_main.setSelected(true);
tab_tba.setSelected(false);
tab_vip.setSelected(false);
tab_mine.setSelected(false);
checkedNum = 0;
break;
case R.id.tab_tba:
tab_tba.setSelected(true);
tab_main.setSelected(false);
tab_vip.setSelected(false);
tab_mine.setSelected(false);
checkedNum = 1;
break;
case R.id.tab_vip:
tab_vip.setSelected(true);
tab_tba.setSelected(false);
tab_main.setSelected(false);
tab_mine.setSelected(false);
checkedNum = 2;
break;
case R.id.tab_mine:
tab_mine.setSelected(true);
tab_vip.setSelected(false);
tab_tba.setSelected(false);
tab_main.setSelected(false);
bar_point.setVisibility(View.VISIBLE);
checkedNum = 3;
break;
}
Fragment to = mFragments[checkedNum];
Fragment last = mFragments[last_Fragment];
last_Fragment=checkedNum;
switchContent(last,to);
}
}
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:orientation="vertical">
<fragment
android:id="@+id/fr_main"
android:name="对应的fragment文件路径"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</fragment>
/**为节约篇幅,这里只放一个。。。。。。。。*/
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ffcacacc" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/tab_main"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="tab_click">
<TextView
style="@style/main_tab_radio_button_style"
android:drawableTop="@drawable/other_btn_image"
android:enabled="true"
android:text="XX" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab_tba"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="tab_click">
<TextView
style="@style/main_tab_radio_button_style"
android:drawableTop="@drawable/other_btn_image"
android:text="XX" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab_vip"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="tab_click">
<TextView
style="@style/main_tab_radio_button_style"
android:drawableTop="@drawable/other_btn_image"
android:text="XX" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab_mine"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:onClick="tab_click"
android:orientation="vertical">
<TextView
style="@style/main_tab_radio_button_style"
android:drawableTop="@drawable/other_btn_image"
android:text="XX" />
<ImageView
android:id="@+id/bar_point"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="-15dp"
android:layout_marginTop="-55dp"
android:background="@android:color/transparent"
android:src="@drawable/bar_dot"
android:visibility="gone" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>
只在最后一个按钮上添加了显示红点的提示,其他按钮上也可用相同的方式添加;默认为隐藏的状态。