我已经构建了一个具有活动和以下布局的应用程序:
<?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。在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)上的空对象引用上的SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' 2490)在Android.app.A活动上的android.app.ActivityThread.-wrap11(ActivityThread.java)$ H.handleMessage(ActivityThread.java:1354)android.os.Handler.dispatchMessage(Handler.java:102)at android位于com.android.internal.os的java.lang.reflect.Method.invoke(Native Method)的android.app.ActivityThread.main(ActivityThread.java:5443)上的.os.Looper.loop(Looper.java:148) .ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:728)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)引起:java.lang.NullPointerException:尝试在com.redbear.chat.Chat.onCreate上的空对象引用上调用虚拟方法'void com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)'( Chat.java:185)在android.app.Anstrumentation.callActivityOnCreate(Instrumentation.java:1130)android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)的android.app.Activity.performCreate(Activity.java:6245) )在android.app.A活动中的android.app.A活动时,在android.app.A活动时,在Android.app.A活动(AndroidT.:::: android.app.Looper.loop(Looper.java:148)的os.Handler.dispatchMessage(Handler.java:102)位于android.app.ActivityThread.main(ActivityThread.java:5443)的java.lang.reflect.Method .invoke(Native Method)位于com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:728)com.android.internal.os.ZygoteInit.main(ZygoteInit.java:61 8)
我的活动如下:
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());
}
}
}
您可以使用三种不同的布局创建三个片段,并按照文档中的示例将它们添加到ViewPager。假设你有三个片段FragmentA
,FragmentB
和FragmentC
与相应的布局a_fragment.xml
与TextView和按钮,b_fragment.xml
与GraphView和c_fragment.xml
包含地图。
现在在您的activity_main.xml布局中:
<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>
像这样在MainActivity.java中设置ViewPager:
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);
最后创建一个这样的ViewPagerAdapter类:
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
}
}
现在将用于设置TextView,Button,GraphView,Map的代码移动到相应的片段中。例如,onCreateView
的FragmentB
中的代码将是这样的:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment2, container, false);
GraphView graphView = rootView.findViewById(R.id.graph);
return rootView;
}
以同样的方式,您将Map的所有代码移动到相应的片段,在您的情况下FragmentC
。