我构建了一个具有活动和以下布局的应用程序:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fitsSystemWindows="true"
android:orientation="vertical" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:orientation="vertical" >
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="100dp"
android:scrollbars="vertical"
android:textSize="18sp" />
<com.jjoe64.graphview.GraphView
android:id="@+id/graph"
android:layout_width="match_parent"
android:layout_height="175dip" />
<RelativeLayout
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:layout_height="60dp" >
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/send"
android:layout_marginRight="80dp"
android:layout_marginBottom="0dp"
android:hint="Comment anything important about the next measurement(s)"
android:singleLine="false" />
<Button
android:id="@+id/send"
style="@style/button_text"
android:layout_alignParentBottom="true"
android:layout_width="80dp"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:layout_marginRight="6dp"
android:layout_toLeftOf="@id/editText"
android:background="@drawable/red_button"
android:text="Send" />
</RelativeLayout>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
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="185dip"
tools:context="com.example.mapwithmarker.MapsMarkerActivity" />
</LinearLayout>
</ScrollView>
我想将布局分为3个不同的选项卡,一个选项卡具有textView和按钮,一个选项卡具有graphView和一个地图。我想我必须对ViewPager做些事情,但是我不理解android指南(https://developer.android.com/training/implementing-navigation/lateral)上的示例,因为它在Fragment对象的集合上滑动,而不是我想要的不同布局。
我应该怎么做?还有其他方法可以解决吗?
更新的问题:
我遵循BenjyTec的建议,因此在活动中添加了代码,并进行了以下3种xml布局:
fragment1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scrollbars="vertical"
android:textSize="18sp" />
<RelativeLayout
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:layout_height="60dp" >
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/send"
android:layout_marginRight="80dp"
android:layout_marginBottom="0dp"
android:hint="Comment anything important about the next measurement(s)"
android:singleLine="false" />
<Button
android:id="@+id/send"
style="@style/button_text"
android:layout_alignParentBottom="true"
android:layout_width="80dp"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:layout_marginRight="6dp"
android:layout_toLeftOf="@id/editText"
android:background="@drawable/red_button"
android:text="Send" />
</RelativeLayout>
</LinearLayout>
fragment2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<com.jjoe64.graphview.GraphView
android:id="@+id/graph"
android:layout_width="match_parent"
android:layout_height="175dip" />
</LinearLayout>
fragment3.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
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="185dip"
tools:context="com.example.mapwithmarker.MapsMarkerActivity" />
</LinearLayout>
因此,主布局(second.xml)现在是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
但是现在当我运行应用程序时,它崩溃并显示以下错误:
java.lang.RuntimeException:无法启动活动 ComponentInfo {com.redbear.chat/com.redbear.chat.Chat}: java.lang.NullPointerException:尝试调用虚拟方法'void com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' 在空对象引用上 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490) 在android.app.ActivityThread.-wrap11(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1354) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:5443) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:728) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 原因:java.lang.NullPointerException:尝试调用虚拟方法'void com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' 在空对象引用上 在com.redbear.chat.Chat.onCreate(Chat.java:185) 在android.app.Activity.performCreate(Activity.java:6245) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490) 在android.app.ActivityThread.-wrap11(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1354) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:5443) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:728) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
我的活动如下:
public class Chat extends FragmentActivity implements OnMapReadyCallback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
ViewPager viewPager = findViewById(R.id.viewPager);
viewPager.setOffscreenPageLimit(2); //number of ViewPager pages that will be kept in storage while swiping
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(viewPagerAdapter);
SupportMapFragment mapFragment = (SupportMapFragment) this.getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
graph = (GraphView) findViewById(R.id.graph);
tv = (TextView) findViewById(R.id.textView);
tv.setMovementMethod(ScrollingMovementMethod.getInstance());
et = (EditText) findViewById(R.id.editText);
btn = (Button) findViewById(R.id.send);
}
public class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new FragmentA();
case 1:
return new FragmentB();
case 2:
return new FragmentC();
}
return new FragmentA();
}
@Override
public int getCount() {
return 3; //number of Fragments inside the ViewPager
}
}
public static class FragmentA extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment1, container, false);
return rootView;
}
}
public static class FragmentB extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment2, container, false);
return rootView;
}
}
public static class FragmentC extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment3, container, false);
return rootView;
}
}
private GoogleMap mMap;
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney, Australia, and move the camera.
LatLng patras = new LatLng(38.246639, 21.734573);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(patras, 14));
updateLocationUI();
UiSettings set = mMap.getUiSettings();
set.setZoomControlsEnabled(true);
}
private void updateLocationUI() {
if (mMap == null) {
return;
}
try {
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(true);
} catch (SecurityException e) {
Log.e("Exception: %s", e.getMessage());
}
}
}
答案 0 :(得分:1)
您可以按照文档中的示例创建具有三种不同布局的三个片段,并将它们添加到ViewPager中。
假设您有三个片段FragmentA
,FragmentB
和FragmentC
,它们的布局分别是TextView和Button,布局a_fragment.xml
,GraphView和{{1} }}包含地图。
现在在您的activity_main.xml布局中:
b_fragment.xml
像这样在MainActivity.java中设置ViewPager:
c_fragment.xml
最后像这样创建一个ViewPagerAdapter类:
<androidx.constraintlayout.widget.ConstraintLayout
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">
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</androidx.constraintlayout.widget.ConstraintLayout>
现在将用于设置TextView,Button,GraphView,Map的代码移到相应的Fragments中。例如,ViewPager viewPager = findViewById(R.id.viewPager);
viewPager.setOffscreenPageLimit(2); //number of ViewPager pages that will be kept in storage while swiping
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(viewPagerAdapter);
的{{1}}内部的代码如下:
public class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new FragmentA();
case 1:
return new FragmentB();
case 2:
return new FragmentC();
}
return new FragmentA();
}
@Override
public int getCount() {
return 3; //number of Fragments inside the ViewPager
}
}
以同样的方式,将Map的所有代码移动到相应的Fragment中,在您的情况下为onCreateView
。