Как разделить макет на 3 вида

1

Я создал приложение, которое имеет активность и следующий макет:

<?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/advan), потому что он пролистывает коллекцию фрагмента объекты, а не разные макеты, которые я хочу.

Как я должен это делать? Есть ли другой способ справиться с этим?


Обновленный вопрос:

Я следовал предложению 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: A ttempt для вызова виртуального метода 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());
    }
}

}
Теги:
android-viewpager
layout
swipe
android-view

1 ответ

1
Лучший ответ

Вы можете создать три фрагмента с тремя различными макетами и добавить их в 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>

Настройте ваш ViewPager в MainActivity.java следующим образом:

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; 
}

Таким же образом вы перемещаете весь код для Карты в соответствующий Фрагмент, который в вашем случае FragmentC.

  • 0
    Это помогло мне, спасибо большое совпадение, но теперь есть ошибка, я думаю, с картой ... Пожалуйста, смотрите обновление в моем посте.
  • 0
    Приложение может зависать, потому что вы настраиваете Views (TextView, Button, GraphView, Map) внутри onCreate MainActivity , но компоновка MainActivity больше не содержит этих представлений! Итак, NullpointerException Таким образом, вам нужно настроить виды в соответствующих фрагментах. Смотрите мой отредактированный ответ.
Показать ещё 4 комментария

Ещё вопросы

Сообщество Overcoder
Наверх
Меню