android - SupportPlaceAutocompleteFragment 的 OnPlaceSelectedListener 未在 ViewPager 内触发

标签 android android-viewpager google-places-api

我的应用程序有一个 viewPager 和 4 个 fragment 。 在一个 fragment 中,我添加了一个 google SupportPlaceAutocompleteFragment

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.area_swipe_fragment, container, false);



    SupportPlaceAutocompleteFragment autocompleteFragment = (SupportPlaceAutocompleteFragment)
            getChildFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);

    autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(Place place) {
            Toast.makeText(ctx, "do something", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(Status status) {
        }
    });






    return view;
}

我的问题是 onPlaceSelectedListener 从未触发,我无法获得所选地点的结果。取而代之的是被放置在第二个 fragment 中的 onActivityResults() 触发。 我尝试在第一个 fragment 中使用 onActivityResults() 但它没有被触发,总是在第二个 fragment 中执行。

有什么想法吗?

最佳答案

来自 the docs :

If your fragment is nested within another fragment, your app must also forward onActivityResult() calls to the containing fragment, as shown in the following snippet:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    autocompleteFragment.onActivityResult(requestCode, resultCode, data);
}

也许这就是解决方案。

关于android - SupportPlaceAutocompleteFragment 的 OnPlaceSelectedListener 未在 ViewPager 内触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34507708/

相关文章:

android - 使用 Google Places API Android 使用 Sqlite 数据库获取最近的地点

android - 如何将一个 ViewPager 与另一个 ViewPager 叠加

java - 无法将 dex 与 google 的新地方 compat sdk 合并

ios - Cocoapods 错误 - 安装 GooglePlacesAutocomplete 时出现 49 个错误

java - android map api 无法从 Fragment 转换为 MapFragment

java - 在 pc 上初始化蓝牙连接 android(client) 到 python(server)

android - 指标元素之间的空间

android - 如何在 ViewPager 中添加 PreferenceFragment?

css - Bootstrap 3 - 修复后的导航栏问题

android - 为什么 Android O 失败并显示 “does not belong to this FragmentManager!”