请先看以下文章
1、酷跑项目:崩溃自动重启功能
MainActivity是启动页面Activity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
......
//停2s跳转MainFragmentActivity
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
//2s后执行
@Override
public void run() {
startActivity(new Intent(MainActivity.this, MainFragmentActivity.class));
}
},2000);
}
}
activity_main是首页布局,放上logo之类的,可以自由发挥。这里根布局是一个RelativeLayout,background是一张图片,如下
2s后跳转MainFragmentActivity
public class MainFragmentActivity extends FragmentActivity {
private Fragment sportFragment;
private Fragment discoverFragment;
private Fragment meFragment;
private Button[] buttonArray = new Button[3];
private Fragment[] fragmentArray = new Fragment[3];
private int currentIndex = 0;//当前显示的fragment的index
private int clickButtonIndex;//点击的button的index
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
setContentView(R.layout.main_fragment);
sportFragment = new SportFragment();
discoverFragment = new DiscoverFragment();
meFragment = new MeFragment();
FragmentManager manager = getSupportFragmentManager();
//事务
FragmentTransaction transaction = manager.beginTransaction();
//添加fragment
transaction.add(R.id.fragment_container, sportFragment);
//显示fragment
transaction.show(sportFragment);
transaction.commit();
buttonArray[0] = findViewById(R.id.btn_sport);
buttonArray[1] = findViewById(R.id.btn_discover);
buttonArray[2] = findViewById(R.id.btn_me);
fragmentArray[0] = sportFragment;
fragmentArray[1] = discoverFragment;
fragmentArray[2] = meFragment;
buttonArray[currentIndex].setSelected(true);
MyListener listener = new MyListener();
for (Button btn : buttonArray) {
btn.setOnClickListener(listener);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private class MyListener implements View.OnClickListener {
@Override
public void onClick(View view) {
try {
switch (view.getId()) {
case R.id.btn_sport:
clickButtonIndex = 0;
break;
case R.id.btn_discover:
clickButtonIndex = 1;
break;
case R.id.btn_me:
clickButtonIndex = 2;
break;
}
//判断单击的是不是别的
if (clickButtonIndex != currentIndex) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
//隐藏当前fragment
transaction.hide(fragmentArray[currentIndex]);
//添加新的fragment
Fragment showFragment = fragmentArray[clickButtonIndex];
//是否添加过,没有添加过才添加
if (!showFragment.isAdded()) {
transaction.add(R.id.fragment_container, showFragment);
}
//显示新的fragment
transaction.show(showFragment);
//前面几个动作,一个出错就不会commit,会走到cath
transaction.commit();
buttonArray[currentIndex].setSelected(false);
buttonArray[clickButtonIndex].setSelected(true);
currentIndex = clickButtonIndex;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
AndroidManifest中不要忘记声明每个Activity,同时修改下theme
<application
android:name=".MyApplication"
......
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<activity android:name=".MainActivity">
......
</activity>
<activity android:name=".activity.MainFragmentActivity"/>
</application>
main_fragment是跳转后的布局,底部三个Button,上面是一个LinearLayout
<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"
android:orientation="vertical">
<LinearLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"></LinearLayout>
<LinearLayout
android:id="@+id/btn_container"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:layout_alignParentBottom="true">
<Button
android:id="@+id/btn_sport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="运动"
android:background="@null"
android:drawableTop="@drawable/sport_selector"
android:layout_weight="1"/>
<Button
android:id="@+id/btn_discover"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发现"
android:background="@null"
android:drawableTop="@drawable/discover_selector"
android:layout_weight="1"/>
<Button
android:id="@+id/btn_me"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我的"
android:background="@null"
android:drawableTop="@drawable/me_selector"
android:layout_weight="1"/>
</LinearLayout>
</RelativeLayout>
其中Button的background以sport_selector为例,其他类似
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@mipmap/ic_tab_sport_normal" android:state_selected="false"/>
<item android:drawable="@mipmap/ic_tab_sport_selected" android:state_selected="true"/>
</selector>
点击3个Button对应切换以下三个新建的Fragment:SportFragment、DiscoverFragment、MeFragment,同时新建3个对应布局fragment_sport等
Bug
到此为止,开始的效果就做出来了,不过有个bug,如果旋转屏幕方向,然后再切换底部按钮,布局会重复加载,
这是因为旋转屏幕时,MainFragmentActivity中的onCreate会重复执行,可以在onCreate通过Log输出日志来验证一下
解决办法
AndroidManifest中修改
<activity android:name=".activity.MainFragmentActivity"
android:launchMode="singleTask"
android:configChanges="orientation|screenSize|keyboardHidden"
/>
MainFragmentActivity中重写onConfigurationChanged()方法
@Override
public void onConfigurationChanged(Configuration newConfig) {
int orientation = newConfig.orientation;
Log.d("Debug","orientation:"+orientation);
super.onConfigurationChanged(newConfig);
}
?关于configChanges和onConfigurationChanged