Commit 823d7c33 authored by fallenstardust's avatar fallenstardust

实现fragment切换无需重载

删除无用布局
parent 495261d1
...@@ -7,9 +7,12 @@ import android.os.Handler; ...@@ -7,9 +7,12 @@ import android.os.Handler;
import android.view.Gravity; import android.view.Gravity;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.widget.FrameLayout;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.navigation.ui.AppBarConfiguration; import androidx.navigation.ui.AppBarConfiguration;
import com.ashokvarma.bottomnavigation.BottomNavigationBar; import com.ashokvarma.bottomnavigation.BottomNavigationBar;
...@@ -45,6 +48,11 @@ public abstract class HomeActivity extends BaseActivity implements OnDuelAssista ...@@ -45,6 +48,11 @@ public abstract class HomeActivity extends BaseActivity implements OnDuelAssista
long exitLasttime = 0; long exitLasttime = 0;
private BottomNavigationBar bottomNavigationBar;
private FrameLayout frameLayout;
private FragmentTransaction transaction;
private Fragment mFragment;
private HomeFragment fragment_home; private HomeFragment fragment_home;
private CardSearchFragment fragment_search; private CardSearchFragment fragment_search;
private DeckManagerFragment fragment_deck_cards; private DeckManagerFragment fragment_deck_cards;
...@@ -93,8 +101,9 @@ public abstract class HomeActivity extends BaseActivity implements OnDuelAssista ...@@ -93,8 +101,9 @@ public abstract class HomeActivity extends BaseActivity implements OnDuelAssista
} }
private void initBottomNavigationBar() { private void initBottomNavigationBar() {
frameLayout = (FrameLayout) findViewById(R.id.fragment_content);
// 获取页面上的底部导航栏控件 // 获取页面上的底部导航栏控件
BottomNavigationBar bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar); bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);
bottomNavigationBar bottomNavigationBar
.addItem(new BottomNavigationItem(R.drawable.ic_home, R.string.mc_home)) .addItem(new BottomNavigationItem(R.drawable.ic_home, R.string.mc_home))
.addItem(new BottomNavigationItem(R.drawable.ic_search, R.string.search)) .addItem(new BottomNavigationItem(R.drawable.ic_search, R.string.search))
...@@ -104,11 +113,27 @@ public abstract class HomeActivity extends BaseActivity implements OnDuelAssista ...@@ -104,11 +113,27 @@ public abstract class HomeActivity extends BaseActivity implements OnDuelAssista
.setActiveColor(R.color.holo_blue_bright) .setActiveColor(R.color.holo_blue_bright)
.setBarBackgroundColor(R.color.transparent) .setBarBackgroundColor(R.color.transparent)
.setMode(BottomNavigationBar.MODE_FIXED) .setMode(BottomNavigationBar.MODE_FIXED)
.setFirstSelectedPosition(0)
.initialise();//所有的设置需在调用该方法前完成 .initialise();//所有的设置需在调用该方法前完成
bottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() { bottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
@Override @Override
public void onTabSelected(int position) { public void onTabSelected(int position) {
//未选中->选中 switch (position) {
case 0:
switchFragment(fragment_home);
break;
case 1:
switchFragment(fragment_search);
break;
case 2:
switchFragment(fragment_deck_cards);
break;
case 3:
switchFragment(fragment_mycard);
break;
case 4:
break;
}
} }
@Override @Override
...@@ -125,16 +150,29 @@ public abstract class HomeActivity extends BaseActivity implements OnDuelAssista ...@@ -125,16 +150,29 @@ public abstract class HomeActivity extends BaseActivity implements OnDuelAssista
fragment_search = new CardSearchFragment(); fragment_search = new CardSearchFragment();
fragment_deck_cards = new DeckManagerFragment(); fragment_deck_cards = new DeckManagerFragment();
fragment_mycard = new MycardFragment(); fragment_mycard = new MycardFragment();
mFragment = fragment_home;
getSupportFragmentManager().beginTransaction() getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_home, fragment_home) .add(R.id.fragment_content, fragment_home)
.add(R.id.fragment_search, fragment_search)
.add(R.id.fragment_deck_cards, fragment_deck_cards)
.add(R.id.fragment_mycard, fragment_mycard)
//.add(R.id.fragment_settings,new Fragment())
.commit(); .commit();
getSupportActionBar().hide(); getSupportActionBar().hide();
} }
private void switchFragment(Fragment fragment) {
//判断当前显示的Fragment是不是切换的Fragment
if (mFragment != fragment) {
//判断切换的Fragment是否已经添加过
if (!fragment.isAdded()) {
//如果没有,则先把当前的Fragment隐藏,把切换的Fragment添加上
getSupportFragmentManager().beginTransaction().hide(mFragment)
.add(R.id.fragment_content, fragment).commit();
} else {
//如果已经添加过,则先把当前的Fragment隐藏,把切换的Fragment显示出来
getSupportFragmentManager().beginTransaction().hide(mFragment).show(fragment).commit();
}
mFragment = fragment;
}
}
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
...@@ -217,12 +255,6 @@ public abstract class HomeActivity extends BaseActivity implements OnDuelAssista ...@@ -217,12 +255,6 @@ public abstract class HomeActivity extends BaseActivity implements OnDuelAssista
duelAssistantManagement.removeDuelAssistantListener(this); duelAssistantManagement.removeDuelAssistantListener(this);
} }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
......
...@@ -512,8 +512,6 @@ public class HomeFragment extends BaseFragemnt implements View.OnClickListener { ...@@ -512,8 +512,6 @@ public class HomeFragment extends BaseFragemnt implements View.OnClickListener {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
switch (v.getId()) { switch (v.getId()) {
case R.id.action_about:
break;
case R.id.action_game: case R.id.action_game:
setRandomCardDetail(); setRandomCardDetail();
if (mCardDetailRandom != null) { if (mCardDetailRandom != null) {
...@@ -527,11 +525,6 @@ public class HomeFragment extends BaseFragemnt implements View.OnClickListener { ...@@ -527,11 +525,6 @@ public class HomeFragment extends BaseFragemnt implements View.OnClickListener {
case R.id.action_bot: case R.id.action_bot:
YGOStarter.startGame(getActivity(), null, "-k", "-s"); YGOStarter.startGame(getActivity(), null, "-k", "-s");
break; break;
case R.id.action_settings: {
Intent intent = new Intent(getContext(), SettingsActivity.class);
startActivity(intent);
break;
}
case R.id.action_download_ex: case R.id.action_download_ex:
WebActivity.open(getContext(), getString(R.string.action_download_expansions), Constants.URL_YGO233_ADVANCE); WebActivity.open(getContext(), getString(R.string.action_download_expansions), Constants.URL_YGO233_ADVANCE);
break; break;
......
...@@ -7,9 +7,16 @@ ...@@ -7,9 +7,16 @@
android:fitsSystemWindows="true" android:fitsSystemWindows="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"> app:layout_behavior="@string/appbar_scrolling_view_behavior">
<FrameLayout
android:id="@+id/fragment_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
</FrameLayout>
<com.ashokvarma.bottomnavigation.BottomNavigationBar <com.ashokvarma.bottomnavigation.BottomNavigationBar
android:id="@+id/bottom_navigation_bar" android:id="@+id/bottom_navigation_bar"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="bottom"/> android:layout_gravity="bottom" />
</FrameLayout> </FrameLayout>
\ No newline at end of file
...@@ -17,10 +17,7 @@ ...@@ -17,10 +17,7 @@
android:layout_marginTop="6dp" android:layout_marginTop="6dp"
android:orientation="vertical"> android:orientation="vertical">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp" />
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
......
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/home_fragment"
android:icon="@drawable/start"
android:title="@string/mc_home"
app:showAsAction="always" />
<item
android:id="@+id/cardSearcher_fragment"
android:icon="@drawable/search"
android:title="@string/search"
app:showAsAction="always" />
<item
android:id="@+id/deckManager_fragment"
android:icon="@drawable/deck"
android:title="@string/deck_manager"
app:showAsAction="always" />
<item
android:id="@+id/mycard_fragment"
android:icon="@drawable/mycard"
android:title="@string/mycard"
app:showAsAction="always" />
<item
android:id="@+id/setting_fragment"
android:icon="@drawable/setting"
android:title="@string/settings"
app:showAsAction="always" />
</menu>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search"
android:icon="@drawable/search"
android:title="@string/search"
app:showAsAction="always" />
</menu>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_game"
android:icon="@drawable/start"
android:title="@string/action_game"
app:showAsAction="always" />
<item
android:id="@+id/action_card_search"
android:icon="@drawable/search"
android:title="@string/card_search"
app:showAsAction="always" />
<item
android:id="@+id/action_deck_manager"
android:icon="@drawable/deck"
android:title="@string/deck_manager"
app:showAsAction="always" />
</menu>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_deck_manager"
android:icon="@drawable/ic_edit"
android:title="@string/deck_manager" />
<item
android:id="@+id/action_download_ex"
android:icon="@drawable/ic_add"
android:title="@string/action_add_server" />
<item
android:id="@+id/action_join_qq_group"
android:icon="@drawable/ic_home_black"
android:title="@string/mycard" />
<item
android:id="@+id/action_reset_game_res"
android:icon="@drawable/ic_file_download_black_24dp"
android:title="@string/reset_game_res" />
<item
android:id="@+id/action_replay"
android:icon="@drawable/ic_album"
android:title="@string/replay" />
<item
android:id="@+id/action_bot"
android:icon="@drawable/ic_copy"
android:title="@string/bot_mode" />
<item android:title="@string/menu">
<menu>
<item
android:id="@+id/action_settings"
android:icon="@drawable/ic_settings"
android:title="@string/action_settings" />
<item
android:id="@+id/action_help"
android:icon="@drawable/ic_live_help"
android:title="@string/help" />
<item
android:id="@+id/action_about"
android:icon="@drawable/ic_about"
android:title="@string/action_about" />
<item
android:id="@+id/action_quit"
android:icon="@drawable/ic_close_black_24dp"
android:title="@string/quit" />
</menu>
</item>
</menu>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mobile_navigation"
app:startDestination="@+id/home_fragment">
<fragment
android:id="@+id/home_fragment"
android:name="cn.garymb.ygomobile.ui.home.HomeFragment"
tools:layout="@layout/fragment_home" />
<fragment
android:id="@+id/cardSearcher_fragment"
android:name="cn.garymb.ygomobile.ui.cards.CardSearchFragment"
tools:layout="@layout/fragment_search"/>
<fragment
android:id="@+id/deckManager_fragment"
android:name="cn.garymb.ygomobile.ui.cards.DeckManagerFragment"
tools:layout="@layout/fragment_deck_cards"/>
<fragment
android:id="@+id/mycard_fragment"
android:name="cn.garymb.ygomobile.ui.mycard.MycardFragment"
tools:layout="@layout/fragment_mycard" />
<fragment
android:id="@+id/setting_fragment"
android:name="cn.garymb.ygomobile.ui.preference.fragments.SettingFragment"
tools:layout="@layout/fragment_settings" />
</navigation>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment