You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
3.8 KiB

package com.example.dxsdpdb;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<Fragment> mFragments;
private LinearLayout mIndexPage;
private LinearLayout mMissionPage;
private LinearLayout mMinePage;
// private ImageButton mImgIndexPage;
// private ImageButton mImgMissionPage;
// private ImageButton mImgMinePage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//去除标题栏
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
initFragments(); //初始化数据
initViews(); //初始化控件
initEvents(); //初始化事件
initFirstRun(0);//第一次运行初始化界面,第一个碎片
}
private void initFragments() {
mFragments = new ArrayList<>();
//将四个 Fragment 加入集合中
mFragments.add(new IndexFragment());
mFragments.add(new MissionFragment());
mFragments.add(new MineFragment());
}
private void initViews() {
mIndexPage = (LinearLayout) findViewById(R.id.id_index);
mMissionPage = (LinearLayout) findViewById(R.id.id_tab_mission);
mMinePage = (LinearLayout) findViewById(R.id.id_tab_mine);
// mImgIndexPage = (ImageButton) findViewById(R.id.id_tab_index_img);
// mImgMissionPage = (ImageButton) findViewById(R.id.id_tab_mission_img);
// mImgMinePage = (ImageButton) findViewById(R.id.id_tab_mine_img);
}
private void initEvents() {
View.OnClickListener onClickListener = new View.OnClickListener() {
public void onClick(View v) {
//先将三个 ImageButton 置为蓝色
resetBackgroud();
//根据点击的 Tab 切换不同的页面及设置对应的 ImageButton 为白色
switch (v.getId()) {
case R.id.id_index: selectTab(0); break;
case R.id.id_tab_mission: selectTab(1); break;
case R.id.id_tab_mine: selectTab(2); break;
}
}};
//设置三个 Tab 的点击事件
mIndexPage.setOnClickListener(onClickListener);
mMissionPage.setOnClickListener(onClickListener);
mMinePage.setOnClickListener(onClickListener);
}
private void resetBackgroud() {
mIndexPage.setBackgroundColor(getResources().getColor(R.color.blue));
mMissionPage.setBackgroundColor(getResources().getColor(R.color.blue));
mMinePage.setBackgroundColor(getResources().getColor(R.color.blue));
}
private void selectTab(int i) {
//根据点击的 Tab 设置对应的 tab 为白色
switch (i) {
case 0: mIndexPage.setBackgroundColor(getResources().getColor(R.color.blue0));break;
case 1: mMissionPage.setBackgroundColor(getResources().getColor(R.color.blue0));break;
case 2: mMinePage.setBackgroundColor(getResources().getColor(R.color.blue0));break;
}
//设置当前点击的 Tab 所对应的页面
setCurrentFragment(i);
}
private void setCurrentFragment(int i)
{
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction trans = fm.beginTransaction();
trans.replace(R.id.frag_layout, mFragments.get(i));
trans.commit();
}
private void initFirstRun(int i)
{
resetBackgroud(); //重置所有 Tab
selectTab(i); //显示第 i 个碎片
}
}