模仿手机QQ底部导航栏效果,之前自己弄过一个底部导航栏,但是每次切换界面它都会自动重新加载,自己上网查资料又自己摸索,终于解决了重新加载界面的问题,下面是一个实例的代码:
在此特别强调一点,在使用Fragment,FragmentManager,FragmentTransaction这三个类时,导包时会有2个不同的包让你选择,具体用哪一个包,代码中都有。
首先是MainActivity.java:
package cn.itcast.laboratory;
import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class Main2Activity extends AppCompatActivity {
private TextView frag1,frag2;
private Fragment fragment01;
FragmentManager FM;
FragmentTransaction FT;
private Frag01Activity f1;
private Frag02Activity f2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
frag1 = (TextView)findViewById(R.id.frag1_id);
frag2 = (TextView)findViewById(R.id.frag2_id);
frag1.setTextColor(Color.rgb(50, 205, 50)); //给各个字体设置颜色
frag2.setTextColor(Color.rgb(0,0,0));
FM = getSupportFragmentManager();
FT = FM.beginTransaction();
f1 = new Frag01Activity();
FT.add(R.id.fragment_buju, f1);
hideFragment(FT);
FT.show(f1);
FT.commit();
setview();
}
private void setview() {
frag1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
frag1.setTextColor(Color.rgb(50, 205, 50)); //给各个字体设置颜色
frag2.setTextColor(Color.rgb(0, 0, 0));
FT = FM.beginTransaction();
if(f1 == null){
f1 = new Frag01Activity();
FT.add(R.id.fragment_buju, f1);
}
hideFragment(FT); //隐藏所有界面
FT.show(f1);
FT.commit();
}
});
frag2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
frag1.setTextColor(Color.rgb(0, 0, 0)); //给各个字体设置颜色
frag2.setTextColor(Color.rgb(50, 205, 50));
FT = FM.beginTransaction();
if(f2 == null){
f2 = new Frag02Activity();
FT.add(R.id.fragment_buju, f2);
}
hideFragment(FT);
FT.show(f2);
FT.commit();
}
});
}
private void hideFragment(FragmentTransaction transaction){
if(f1 != null){
transaction.hide(f1);
}
if(f2 != null){
transaction.hide(f2);
}
}
}
下面activity_main.xml文件的代码:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:orientation="vertical"
tools:context="cn.itcast.laboratory.Main2Activity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/view_id"
/>
<View
android:id="@+id/view_id"
android:layout_height="1dp"
android:layout_width="match_parent"
android:background="#DCDBDB"
android:layout_above="@+id/linear_id"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/linear_id"
android:layout_alignParentBottom="true"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:background="#F2F2F2"
android:orientation="horizontal">
<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:drawablePadding="3dp"
android:focusable="true"
android:gravity="center"
android:id="@+id/frag1_id"
android:text="申请教室/实验室"
/>
<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:drawablePadding="3dp"
android:focusable="true"
android:gravity="center"
android:id="@+id/frag2_id"
android:text="我的"
/>
</LinearLayout>
<FrameLayout
android:id="@+id/fragment_buju"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/linear_id"
>
</FrameLayout>
</RelativeLayout>
仅仅是这些代码还不够,因为我们要在activity_main中插入两个界面,来实现两个界面之间的切换效果,所以我们再引入以下两个类:Frag01Activity.class和Frag02Activity.class,它们分别对应这两个布局文件:activity_frag01.xml和activity_frag02.xml:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class Frag01Activity extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//引用创建好的xml布局
View view = inflater.inflate(R.layout.activity_frag01,container,false);
return view;
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Button button = (Button) getActivity().findViewById(R.id.heheda1);
Toast.makeText(getActivity(), "界面1已加载...", Toast.LENGTH_SHORT).show();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 通过fragment2中的按钮事件来触发拿到fragment1中的textView
Toast.makeText(getActivity(), "呵呵哒1", Toast.LENGTH_SHORT).show();
}
});
}
}
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class Frag02Activity extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//引用创建好的xml布局
View view = inflater.inflate(R.layout.activity_frag02,container,false);
return view;
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Toast.makeText(getActivity(), "界面2已加载...", Toast.LENGTH_SHORT).show();
}
}
以下是activity_frag01.xml和activity_frag02.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:id="@+id/frag1_buju"
tools:context="cn.itcast.laboratory.Frag01Activity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="呵呵哒1"
android:id="@+id/heheda1"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:id="@+id/frag2_buju"
tools:context="cn.itcast.laboratory.Frag02Activity">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="50dp"
android:text="\n\n\n我是第二页">
</TextView>
</LinearLayout>
下面是效果图:
在这里再提醒一下,第一次进入app时,会加载第一页,第一次切换第二页时,也会加载,但是之后切换就不会加载了,加载时会有吐司提示。
谢谢!