android - 如何将我的布局拆分为 3 个不同的滑动 View

标签 android layout android-viewpager android-view swipe

我构建了一个具有 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,一个包含 map 。我想我必须对 ViewPager 做一些事情,但我不理解 android 指南 ( https://developer.android.com/training/implementing-navigation/lateral ) 上的示例,因为它会扫过 Fragment 对象的集合,而不是我想要的不同布局。

我应该怎么做?有没有其他方法可以解决这个问题?


更新的问题:

我遵循了 BenjyTec 的建议,所以我在我的 Activity 中添加了代码并制作了以下 3 个 xml 布局:

fragment 1.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>

fragment 2.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>

fragment 3.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 FragmentAFragmentBFragmentC,具有相应的布局 a_fragment.xml 以及 TextView 和按钮、带有 GraphView 的 b_fragment.xml 和包含 map 的 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的代码移动到相应的Fragments中。例如,FragmentBonCreateView 中的代码如下:

@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

关于android - 如何将我的布局拆分为 3 个不同的滑动 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54638989/

相关文章:

xml - Magento 布局 module_default

html - 使用 CSS 修复了容器左侧的 div、容器(但不是页面)的全高

html - CSS 页面布局,将文本保留在自己的 div 列中

android - 同步ViewPager中的所有ListView

android - ViewPager InstantiateItem() 返回错误的位置值

Android ViewPager 点指示器不动

java - Android:未处理的异常类型 IOException 错误

java - Java 和 Android 之间的不一致行为

android - 应用不稳定

java - 即使 android.bat 存在也无法执行