gpt4 book ai didi

android - PlaceAutocompleteFragment : automatically open SearchView

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:44:42 27 4
gpt4 key购买 nike

我想在 Activity 加载完成后立即自动点击 fragment 。

fragment 定义为:

 <fragment
android:id="@+id/place_autocomplete_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
/>

我试过这样做

fragment = findViewById(R.id.place_autocomplete_fragment);

new Handler().postDelayed(new Runnable() {
@Override
public void run() {
fragment.performClick();
}
}, 1000);

但是没用。

有什么方法可以自动点击 fragment 吗?

编辑:在我的例子中, fragment 被膨胀了

//PlaceAutoComplete Search Implementation
PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);


autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
Log.i(String.valueOf(this), "Place: " + place.getName() + "\nID: " + place.getId());
String placeId = place.getId();
try {
Intent intent = new Intent(PlaceSearch.this, PlaceDetailsFromSearch.class);
Bundle extras = new Bundle();
extras.putString("placeID", placeId);
intent.putExtras(extras);
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public void onError(Status status) {
Log.i(String.valueOf(this), "An error occurred: " + status);
}
});

最佳答案

您不能点击 <fragment> .

点击事件只有View可以有。 fragment 不是 View .

你可以点击一个View你的 fragment 膨胀。

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

// some `View` from your fragment
View searchView = view.findViewById(R.id.searchView);
// Dispatch a click event to `searchView` as soon as that view is laid out
searchView.post(() -> searchView.performClick());
}

更新

因为您正在使用 PlaceAutocompleteFragment来自 Play Services(因此你没有来源),你可以在你的 Activity 中做这样的事情:

final PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment) getFragmentManager()
.findFragmentById(R.id.place_autocomplete_fragment);

final View root = autocompleteFragment.getView();
root.post(new Runnable() {
@Override
public void run() {
root.findViewById(R.id.places_autocomplete_search_input)
.performClick();
}
});

关于android - PlaceAutocompleteFragment : automatically open SearchView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43501582/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com