首页实现页面切换
This commit is contained in:
@@ -1,19 +1,40 @@
|
||||
package com.example.myapplication.activity;
|
||||
|
||||
import android.os.Bundle;
|
||||
|
||||
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TableLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.fragment.app.FragmentPagerAdapter;
|
||||
|
||||
import com.example.myapplication.base.BaseActivity;
|
||||
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
||||
import com.google.android.material.snackbar.Snackbar;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.appcompat.widget.Toolbar;
|
||||
|
||||
import android.view.View;
|
||||
|
||||
import com.example.myapplication.R;
|
||||
import com.example.myapplication.fragment.FindFragment;
|
||||
import com.example.myapplication.fragment.MineFragment;
|
||||
import com.example.myapplication.fragment.RecordFragment;
|
||||
import com.example.myapplication.fragment.TreasureFragment;
|
||||
import com.example.myapplication.util.NoSlideViewPager;
|
||||
import com.google.android.material.tabs.TabLayout;
|
||||
import com.gyf.immersionbar.ImmersionBar;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* 创建首页布局展示
|
||||
*/
|
||||
public class HomeActivity extends BaseActivity {
|
||||
private NoSlideViewPager mViewPager;
|
||||
private TabLayout mTab;
|
||||
private final String[] name = {"寻宝", "记录", "发现", "我的"};
|
||||
private final int[] pic = {R.mipmap.map_select,R.mipmap.task_select,R.mipmap.article_select,R.mipmap.mine_select};
|
||||
private final int[] pic1 = {R.mipmap.map,R.mipmap.task,R.mipmap.article,R.mipmap.mine};
|
||||
|
||||
@Override
|
||||
protected int getLayout() {
|
||||
return R.layout.activity_home;
|
||||
@@ -22,8 +43,62 @@ public class HomeActivity extends BaseActivity {
|
||||
@Override
|
||||
protected void initView() {
|
||||
super.initView();
|
||||
}
|
||||
ImmersionBar.with(this)
|
||||
.statusBarDarkFont(true, 0.2f)
|
||||
//原理:如果当前设备支持状态栏字体变色,会设置状态栏字体为黑色,如果当前设备不支持状态栏字体变色,会使当前状态栏加上透明度,否则不执行透明度
|
||||
.init();
|
||||
mTab=findViewById(R.id.tab_layout);
|
||||
mViewPager=findViewById(R.id.no_slide_view_pager);
|
||||
ArrayList<Fragment> fragments = new ArrayList<>();
|
||||
fragments.add(new TreasureFragment());
|
||||
fragments.add(new RecordFragment());
|
||||
fragments.add(new FindFragment());
|
||||
fragments.add(new MineFragment());
|
||||
|
||||
mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
|
||||
@Override
|
||||
public Fragment getItem(int i) {
|
||||
return fragments.get(i);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
return fragments.size();
|
||||
}
|
||||
});
|
||||
mTab.setupWithViewPager(mViewPager);
|
||||
mTab.getTabAt(0).setText(name[0]).setIcon(pic[0]);
|
||||
mTab.getTabAt(1).setText(name[1]).setIcon(pic1[1]);
|
||||
mTab.getTabAt(2).setText(name[2]).setIcon(pic1[2]);
|
||||
mTab.getTabAt(3).setText(name[3]).setIcon(pic1[3]);
|
||||
mTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
|
||||
//禁止table layout效果
|
||||
@Override
|
||||
public void onTabSelected(TabLayout.Tab tab) {
|
||||
mTab.getTabAt(tab.getPosition()).setIcon(pic[tab.getPosition()]);
|
||||
}
|
||||
|
||||
//点击table layout效果
|
||||
@Override
|
||||
public void onTabUnselected(TabLayout.Tab tab) {
|
||||
mTab.getTabAt(tab.getPosition()).setIcon(pic1[tab.getPosition()]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTabReselected(TabLayout.Tab tab) {
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
public View getTabView(String name,int image) {
|
||||
View view = LayoutInflater.from(this).inflate(R.layout.item_tab, null);
|
||||
TextView txt_title = (TextView) view.findViewById(R.id.txt_title);
|
||||
txt_title.setText(name);
|
||||
ImageView img_title = (ImageView) view.findViewById(R.id.img_title);
|
||||
img_title.setImageResource(image);
|
||||
return view;
|
||||
}
|
||||
@Override
|
||||
protected void initData() {
|
||||
super.initData();
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.example.myapplication.activity;
|
||||
|
||||
import com.example.myapplication.R;
|
||||
import com.example.myapplication.base.BaseActivity;
|
||||
|
||||
/**
|
||||
* 登录页
|
||||
*/
|
||||
public class LoginActivity extends BaseActivity {
|
||||
@Override
|
||||
protected int getLayout() {
|
||||
return R.layout.login_activity;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initData() {
|
||||
super.initData();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initView() {
|
||||
super.initView();
|
||||
}
|
||||
}
|
||||
@@ -2,11 +2,21 @@ package com.example.myapplication.activity;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.graphics.Color;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.CountDownTimer;
|
||||
import android.view.View;
|
||||
import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
|
||||
import com.example.myapplication.R;
|
||||
import com.example.myapplication.base.BaseActivity;
|
||||
|
||||
/**
|
||||
* 启动页
|
||||
*/
|
||||
public class MainActivity extends BaseActivity {
|
||||
|
||||
@Override
|
||||
@@ -17,6 +27,19 @@ public class MainActivity extends BaseActivity {
|
||||
@Override
|
||||
protected void initData() {
|
||||
super.initData();
|
||||
new CountDownTimer(3000, 1000) {
|
||||
@Override
|
||||
public void onTick(long l) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFinish() {
|
||||
Intent intent = new Intent(MainActivity.this, HomeActivity.class);
|
||||
startActivity(intent);
|
||||
finish();
|
||||
}
|
||||
}.start();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
package com.example.myapplication.activity;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.view.KeyEvent;
|
||||
|
||||
import com.example.myapplication.R;
|
||||
import com.example.myapplication.base.BaseActivity;
|
||||
import com.example.myapplication.fragment.FindFragment;
|
||||
import com.example.myapplication.fragment.MineFragment;
|
||||
import com.example.myapplication.fragment.RecordFragment;
|
||||
import com.example.myapplication.fragment.TreasureFragment;
|
||||
import com.gyf.immersionbar.ImmersionBar;
|
||||
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
import androidx.fragment.app.FragmentTransaction;
|
||||
|
||||
/**
|
||||
* fragment 管理作用:
|
||||
*/
|
||||
|
||||
public class ManagementFragment extends BaseActivity {
|
||||
private FragmentManager fragmentManager;
|
||||
private int fourthly_tag;
|
||||
private MineFragment mineFragment;//我的fragment
|
||||
private FindFragment findFragment;//发现的fragment
|
||||
private RecordFragment recordFragment;//纪录的fragment
|
||||
private TreasureFragment treasureFragment;//寻宝的fragment
|
||||
|
||||
|
||||
@Override
|
||||
protected int getLayout() {
|
||||
return R.layout.management_fragment;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initView() {
|
||||
super.initView();
|
||||
fourthly_tag = getIntent().getIntExtra("tag", 0);
|
||||
fragmentManager = getSupportFragmentManager();
|
||||
if (fourthly_tag != 0) {
|
||||
selectorFragment(fourthly_tag);
|
||||
}
|
||||
setZtlTextColor();
|
||||
}
|
||||
|
||||
//隐藏所有Fragment
|
||||
private void hideAllFragment(FragmentTransaction fragmentTransaction) {
|
||||
if (mineFragment != null)
|
||||
fragmentTransaction.hide(mineFragment);
|
||||
if (findFragment != null)
|
||||
fragmentTransaction.hide(findFragment);
|
||||
if (recordFragment != null)
|
||||
fragmentTransaction.hide(recordFragment);
|
||||
if (treasureFragment != null)
|
||||
fragmentTransaction.hide(treasureFragment);
|
||||
}
|
||||
|
||||
public void selectorFragment(int i) {
|
||||
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
|
||||
hideAllFragment(fragmentTransaction);
|
||||
switch (i) {
|
||||
case 1:
|
||||
if (mineFragment == null) {
|
||||
mineFragment = new MineFragment();
|
||||
fragmentTransaction.add(R.id.frame_layout, mineFragment);
|
||||
} else {
|
||||
fragmentTransaction.show(mineFragment);
|
||||
}
|
||||
/* Bundle bundle1 = new Bundle();
|
||||
bundle1.putString("grad", getIntent().getStringExtra("grad"));
|
||||
mineFragment.setArguments(bundle1);*/
|
||||
break;
|
||||
case 2:
|
||||
if (findFragment == null) {
|
||||
findFragment = new FindFragment();
|
||||
fragmentTransaction.add(R.id.frame_layout, findFragment);
|
||||
} else {
|
||||
fragmentTransaction.show(findFragment);
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
if (recordFragment == null) {
|
||||
recordFragment = new RecordFragment();
|
||||
fragmentTransaction.add(R.id.frame_layout, recordFragment);
|
||||
} else {
|
||||
fragmentTransaction.show(recordFragment);
|
||||
}
|
||||
break;
|
||||
case 4:
|
||||
if (treasureFragment == null) {
|
||||
treasureFragment = new TreasureFragment();
|
||||
fragmentTransaction.add(R.id.frame_layout, treasureFragment);
|
||||
} else {
|
||||
fragmentTransaction.show(treasureFragment);
|
||||
}
|
||||
break;
|
||||
}
|
||||
fragmentTransaction.commit();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
switch (fourthly_tag) {
|
||||
case 1:
|
||||
mineFragment.onActivityResult(requestCode, resultCode, data);
|
||||
break;
|
||||
case 2:
|
||||
findFragment.onActivityResult(requestCode, resultCode, data);
|
||||
break;
|
||||
case 3:
|
||||
recordFragment.onActivityResult(requestCode, resultCode, data);
|
||||
break;
|
||||
case 4:
|
||||
treasureFragment.onActivityResult(requestCode, resultCode, data);
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
private void setZtlTextColor() {
|
||||
ImmersionBar.with(this)
|
||||
.statusBarDarkFont(true, 0.2f) //原理:如果当前设备支持状态栏字体变色,会设置状态栏字体为黑色,如果当前设备不支持状态栏字体变色,会使当前状态栏加上透明度,否则不执行透明度
|
||||
.init();
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user