Как разделить макет на 3 вида - PullRequest
0 голосов
/ 11 февраля 2019

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

<?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:

фрагмент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>

фрагмент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>

фрагмент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: невозможно запустить действие 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) на андроид.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.Приложение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) at 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());
    }
}

}

1 Ответ

0 голосов
/ 12 февраля 2019

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

...