Android 组件导航如何防止地图页面在每次点击时刷新
Posted
技术标签:
【中文标题】Android 组件导航如何防止地图页面在每次点击时刷新【英文标题】:Android Component Navigation how do i prevent the map page from refreshing on every click 【发布时间】:2021-12-03 18:42:40 【问题描述】:this is my design
我正在计划一个 3 页的地图应用程序。我的第一页上有一张地图。每次点击底部导航都会重新加载我的地图。我怎样才能防止这种情况发生?
我的导航图
由于子页面太多,我无法对事务执行此操作。
<?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/nav_graph"
app:startDestination="@id/home">
<include app:graph="@navigation/dashboard" />
<include app:graph="@navigation/home" />
<include app:graph="@navigation/menu" />
</navigation>
我的地图导航图
<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/home"
app:startDestination="@id/homeFragment">
<fragment
android:id="@+id/homeFragment"
android:name="com.example.bottomnavwithnavigationcomponent.homeScreen.HomeFragment"
android:label="Home"
tools:layout="@layout/fragment_home" />
<fragment
android:id="@+id/homeFragment2"
android:name="com.example.bottomnavwithnavigationcomponent.homeScreen.HomeFragment2"
android:label="Home"
tools:layout="@layout/fragment_home2" />
MainActivity
public class MainActivity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigationView);
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupWithNavController(bottomNavigationView, navController);
NavigationUI.setupActionBarWithNavController(this, navController);
@Override
public boolean onSupportNavigateUp()
onBackPressed();
return super.onSupportNavigateUp();
地图片段
此页面上有一个试用片段转换。 我希望这个页面永远不会被刷新。
public class HomeFragment extends Fragment
private OnMapReadyCallback callback = new OnMapReadyCallback()
@Override
public void onMapReady(GoogleMap googleMap)
LatLng sydney = new LatLng(-34, 151);
googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState)
return inflater.inflate(R.layout.fragment_home, container, false);
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)
super.onViewCreated(view, savedInstanceState);
SupportMapFragment mapFragment =
(SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
if (mapFragment != null)
mapFragment.getMapAsync(callback);
我是通过事务处理的,但它非常不正常。
binding.navigationBottom.setOnItemSelectedListener(new BottomNavigationView.OnItemSelectedListener()
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem)
if(menuItem.getItemId() == R.id.people)
removeFragment(tempFragment);
if(menuItem.getItemId() == R.id.group)
if (tempFragment ==null)
getFragment(groupFragment);
else
removeFragment(tempFragment);
getFragment(groupFragment);
tempFragment = groupFragment;
if(menuItem.getItemId() == R.id.places)
if (tempFragment ==null)
getFragment(addFragment);
else
removeFragment(tempFragment);
getFragment(addFragment);
tempFragment = addFragment;
if(menuItem.getItemId() == R.id.blank)
if (tempFragment ==null)
getFragment(blankFragment);
else
removeFragment(tempFragment);
getFragment(blankFragment);
tempFragment = blankFragment;
if(menuItem.getItemId() == R.id.settings)
if (tempFragment ==null)
getFragment(settingsFragment);
else
removeFragment(tempFragment);
getFragment(settingsFragment);
tempFragment = settingsFragment;
return true;
);
binding.navigationBottom.setOnItemReselectedListener(new NavigationBarView.OnItemReselectedListener()
@Override
public void onNavigationItemReselected(@NonNull MenuItem item)
);
public void getFragment(Fragment fragment)
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.setCustomAnimations(R.anim.slide_up, 0);
transaction.add(R.id.nav_host_fragment, fragment).addToBackStack(null).commit();
public void removeFragment(Fragment fragment)
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.remove(fragment).addToBackStack(null).commit();
【问题讨论】:
【参考方案1】:地图正在“刷新”,因为当您删除一个片段时,被删除的片段被破坏,然后重新创建并
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)
super.onViewCreated(view, savedInstanceState);
SupportMapFragment mapFragment =
(SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
if (mapFragment != null)
mapFragment.getMapAsync(callback);
再次调用mapFragment.getMapAsync(callback);
。您需要创建一次“地图片段”并使用来自FragmentTransaction
的hide()
和show()
方法,而不是add()/replace()
。
【讨论】:
【参考方案2】:只需更新您的导航组件
/*Navigation Component dependencies*/
def nav_version = "2.4.0-alpha06"
implementation "androidx.navigation:navigation-fragment:$nav_version"
implementation "androidx.navigation:navigation-ui:$nav_version"
【讨论】:
以上是关于Android 组件导航如何防止地图页面在每次点击时刷新的主要内容,如果未能解决你的问题,请参考以下文章