其他分享
首页 > 其他分享> > android-在ViewPager内部未触发SupportPlaceAutocompleteFragment的OnPlaceSelectedListener

android-在ViewPager内部未触发SupportPlaceAutocompleteFragment的OnPlaceSelectedListener

作者:互联网

我的应用程序有一个带有viewPager和4个片段的活动.
在一个片段中,我添加了一个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 one rror(Status status) {
        }
    });






    return view;
}

我的问题是,永远不会触发onPlaceSelectedListener,并且无法获得所选位置的结果.而是触发第二个片段中的onActivityResults().
我尝试在第一个片段中使用onActivityResults(),但不会被触发,请始终在第二个片段中执行.

有任何想法吗?

解决方法:

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

也许这就是解决方案.

标签:google-places-api,android-viewpager,android
来源: https://codeday.me/bug/20191119/2034634.html