如何在MapFragment中创建Tab和PageView?

时间:2018-03-09 18:18:30

标签: android google-maps android-fragments

我的代码显示以下错误:

  

setSupportActionBar(toolbar);

     

ERRO:无法解析方法setSupportActionBar(android.support.v7.widget.Toolbar);

     

getSupportActionBar();

     

错误:无法解析方法' getSupportActionBar();

我的代码如下:

  

MapsActivity.java

package com.thiagosaad.filadeatendimento;


import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.thiagosaad.filadeatendimento.tab.TabViewPagerAdapter;
import com.thiagosaad.filadeatendimento.tab.fragments.InfoMapFragment;
import com.thiagosaad.filadeatendimento.tab.fragments.UserAccountFragment;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    // GOOGLE MAPS API CONFIG
    private GoogleMap mMap;

    // TABLAYOUT CONFIG
    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        // TAB LAYOUT CONFIG
        toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        viewPager = findViewById(R.id.tabViewPager);
        setupViewPager(viewPager);

        tabLayout = findViewById(R.id.tabMenu);
        tabLayout.setupWithViewPager(viewPager);
    }


    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }

    private void setupViewPager(ViewPager viewPager) {
        TabViewPagerAdapter adapter = new TabViewPagerAdapter(getSupportFragmentManager());
        adapter.addFrag(new UserAccountFragment(), "ONE");
        adapter.addFrag(new InfoMapFragment(), "TWO");
        viewPager.setAdapter(adapter);
    }
}
  

activity_maps.xml

<FrameLayout 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"
    tools:context=".MapsActivity" >

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/map"
        tools:context=".MapsActivity"
        android:name="com.google.android.gms.maps.SupportMapFragment" />


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fieldList"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_gravity="top|left"
            android:layout_marginStart="11dp"
            android:adjustViewBounds="true"
            android:clickable="true"
            android:fadeScrollbars="false"
            android:src="@android:drawable/ic_menu_sort_by_size"
            android:visibility="visible"
            app:backgroundTint="@android:color/holo_orange_light"
            app:fabSize="normal" />

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/updateMap"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignStart="@+id/fieldList"
            android:layout_gravity="top|left"
            android:layout_marginStart="65dp"
            android:adjustViewBounds="true"
            android:clickable="true"
            android:fadeScrollbars="false"
            android:src="@android:drawable/ic_popup_sync"
            android:visibility="visible"
            app:backgroundTint="@android:color/holo_orange_light"
            app:fabSize="normal" />

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/myLocation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentTop="true"
            android:layout_gravity="top|right"
            android:layout_marginEnd="11dp"
            android:adjustViewBounds="true"
            android:clickable="true"
            android:fadeScrollbars="false"
            android:foregroundGravity="top|right"
            android:src="@android:drawable/ic_menu_mylocation"
            android:visibility="visible"
            app:backgroundTint="@color/colorPrimaryDark"
            app:fabSize="normal" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabMenu"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentStart="true"
            android:background="@android:color/holo_orange_light"
            android:foregroundGravity="bottom|center_vertical|center_horizontal|center"
            app:tabIndicatorColor="@color/colorPrimary">

            <android.support.design.widget.TabItem
                android:id="@+id/userAccount"
                android:foregroundGravity="bottom|center_vertical|center_horizontal|center"
                android:icon="@android:drawable/ic_menu_myplaces"
                android:visibility="visible" />

            <android.support.design.widget.TabItem
                android:id="@+id/infoMap"
                android:foregroundGravity="bottom|center_vertical|center_horizontal|center"
                android:icon="@android:drawable/ic_dialog_info"
                android:visibility="visible" />

            <android.support.v4.view.ViewPager
                android:id="@+id/tabViewPager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"  />

        </android.support.design.widget.TabLayout>


    </RelativeLayout>


</FrameLayout>
  

TabViewPagerAdapter.java

package com.thiagosaad.filadeatendimento.tab;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;

public class TabViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public TabViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFrag(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}
  

UserAccountFragment.java

package com.thiagosaad.filadeatendimento.tab.fragments;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.thiagosaad.filadeatendimento.R;

/**
 * Created by thiago.saad on 09/03/2018.
 */

public class UserAccountFragment extends Fragment {

    public UserAccountFragment(){
        // OBRIGATORIO TER O CONSTRUTOR VAZIO
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_user_account, container, false);
    }
}

1 个答案:

答案 0 :(得分:0)

setSupportActionBar()AppCompatActivity上的一种方法。

更改

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback