В настоящее время я разрабатываю приложение о сервисах Google Maps. Я использую компоновку вкладок с 2 вкладками, которые являются фрагментами. В одной вкладке я использую google maps api, и это работает правильно. Но во второй вкладке я хочу, чтобы при нажатии кнопки «Начать» с помощью выбора места. По щелчку появляется в течение 1 секунды и скрывается после этого. Я не знаю, в чем проблема, поэтому, пожалуйста, помогите.Добавление сборщика google в вкладке фрагмент андроида
Вот код:
MainActivity:
public class MainActivity extends AppCompatActivity {
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
// FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
// fab.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
// .setAction("Action", null).show();
// }
// });
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
TabDirectionsActivity tabDirectionsActivity = new TabDirectionsActivity();
return tabDirectionsActivity;
case 1:
TabPlacesActivity tabPlacesActivity = new TabPlacesActivity();
return tabPlacesActivity;
default:
return null;
}
}
@Override
public int getCount() {
// Show 2 total pages.
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "DIRECTIONS";
case 1:
return "PLACES";
}
return null;
}
}
}
Вот TabPlaces Фрагмент:
public class TabPlacesActivity extends Fragment {
TextView placeNameText;
TextView placeAddressText;
WebView attributionText;
Button getPlaceButton;
private final static int PLACE_PICKER_REQUEST = 1;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View placesView = inflater.inflate(R.layout.activity_places, container, false);
placeNameText = (TextView) placesView.findViewById(R.id.tv_PlaceName);
placeAddressText = (TextView) placesView.findViewById(R.id.tv_PlaceAddress);
attributionText = (WebView) placesView.findViewById(R.id.wv_Attribution);
getPlaceButton = (Button) placesView.findViewById(R.id.btn_GetPlace);
getPlaceButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
try {
Intent intent = builder.build(getActivity());
startActivityForResult(intent, PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}
});
return placesView;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PLACE_PICKER_REQUEST){
if (resultCode == getActivity().RESULT_OK){
Place place = PlacePicker.getPlace(getActivity(),data);
placeNameText.setText(place.getName());
placeAddressText.setText(place.getAddress());
if (place.getAttributions() == null) {
attributionText.loadData("no attribution", "text/html; charset=utf-8", "UFT-8");
} else {
attributionText.loadData(place.getAttributions().toString(), "text/html; charset=utf-8", "UFT-8");
}
}
}
}
}
Вот activity_places.xml:
<LinearLayout 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:orientation="vertical"
tools:context="rs.fon.mapapp.TabPlacesActivity"
android:gravity="top|center">
<Button
android:id="@+id/btn_GetPlace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Place" />
<TextView
android:id="@+id/tv_PlaceName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:text="Place Name"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_PlaceAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Place Address"
android:textSize="16sp" />
<WebView
android:id="@+id/wv_Attribution"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Attribution"
android:textSize="16sp" />
</LinearLayout>