如何将我的布局拆分为 3 个不同的滑动视图
How can I split my layout in 3 different swipe views
我构建了一个具有 activity 和以下布局的应用程序:
<?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 的建议,所以我在我的 activity 中添加了代码,并制作了以下 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: Unable to start activity
ComponentInfo{com.redbear.chat/com.redbear.chat.Chat}:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)'
on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void
com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)'
on a null object reference
at com.redbear.chat.Chat.onCreate(Chat.java:185)
at android.app.Activity.performCreate(Activity.java:6245)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
我的activity如下:
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());
}
}
}
您可以创建三个具有三种不同布局的 Fragment,然后按照文档中的示例将它们添加到 ViewPager。
假设您有三个 Fragments FragmentA
、FragmentB
和 FragmentC
具有相应的布局 a_fragment.xml
带有 TextView 和 Button,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 class:
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的代码移动到对应的Fragments中。例如,FragmentB
的 onCreateView
中的代码如下所示:
@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 的所有代码移动到相应的 Fragment 中,在您的情况下 FragmentC
。
我构建了一个具有 activity 和以下布局的应用程序:
<?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 的建议,所以我在我的 activity 中添加了代码,并制作了以下 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: Unable to start activity ComponentInfo{com.redbear.chat/com.redbear.chat.Chat}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5443) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference at com.redbear.chat.Chat.onCreate(Chat.java:185) at android.app.Activity.performCreate(Activity.java:6245) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5443) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
我的activity如下:
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());
}
}
}
您可以创建三个具有三种不同布局的 Fragment,然后按照文档中的示例将它们添加到 ViewPager。
假设您有三个 Fragments FragmentA
、FragmentB
和 FragmentC
具有相应的布局 a_fragment.xml
带有 TextView 和 Button,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 class:
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的代码移动到对应的Fragments中。例如,FragmentB
的 onCreateView
中的代码如下所示:
@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 的所有代码移动到相应的 Fragment 中,在您的情况下 FragmentC
。