Jab*_*ber 5 java android android-fragments android-navigation androidx
我在我的应用程序中遇到了一个问题,我发现自己以一种笨拙的方式在 Activity 之间共享数据 - 因此我决定尝试将应用程序的大部分内容切换到单个 Activity(以便跨屏幕使用单个 ViewModel),使用导航组件交换片段而不是更改活动。
似乎对于每个目的地,我都需要一个 xml 布局文件和一个片段类,如下所示:
class DestinationFragment: Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_destination_layout, container, false)
return view
}
}
Run Code Online (Sandbox Code Playgroud)
(我实际上使用的是 Java,但只能找到 Kotlin 示例。)
但是我不确定如何处理地图屏幕。我目前已将其作为自己的活动:
活动地图.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity">
<!-- Toolbar stuff -->
<FrameLayout android:id="@+id/container_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/my_toolbar"
xmlns:android="http://schemas.android.com/apk/res/android">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity" />
</FrameLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
地图活动.java
public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback, GoogleMap.OnInfoWindowClickListener {
private GoogleMap mMap;
//Create the activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
//Check if user has location permissions granted
if (!locationPermissionCheck()) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);
} else {
setupMap(); //custom function defined elsewhere
}
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题 - 如果当前我有一个获取地图片段的活动,为了适应导航架构,我现在需要一个获取地图片段的片段吗?即类似...
public class MapDestinationFragment extends Fragment implements OnMapReadyCallback, GoogleMap.OnInfoWindowClickListener {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.article_view, container, false);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
//Check if user has location permissions granted
if (!locationPermissionCheck()) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);
} else {
setupMap(); //custom function defined elsewhere
}
}
Run Code Online (Sandbox Code Playgroud)
如果这是解决方案,那么在片段中包含片段可以吗?或者我最好将其保留为一个单独的活动并只接受数据共享问题?
谢谢!
| 归档时间: |
|
| 查看次数: |
1026 次 |
| 最近记录: |