PlaceAutocompleteFragment:自动开启SearchView
PlaceAutocompleteFragment: automatically open SearchView
我想在 activity 加载完成后立即自动点击片段。
片段定义为:
<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);
但是没用。
有什么方法可以自动点击片段吗?
编辑:
在我的例子中,片段被膨胀
//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
才有。片段 不是 View
。
您可以单击您的片段膨胀的 View
。
@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());
}
更新
因为您正在使用来自 Play Services 的 PlaceAutocompleteFragment
(因此您没有来源),您可以在 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();
}
});
科特林:
对于最新的地方 api 2.4.0 (com.google.android.libraries.places:places:2.4.0)
您可以通过以下代码获取视图,然后在搜索视图上执行点击:
val root: View = autocompleteSupportFragment?.view!!
root.findViewById<View>(R.id.places_autocomplete_search_input).performClick()
我想在 activity 加载完成后立即自动点击片段。
片段定义为:
<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);
但是没用。
有什么方法可以自动点击片段吗?
编辑: 在我的例子中,片段被膨胀
//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
才有。片段 不是 View
。
您可以单击您的片段膨胀的 View
。
@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());
}
更新
因为您正在使用来自 Play Services 的 PlaceAutocompleteFragment
(因此您没有来源),您可以在 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();
}
});
科特林: 对于最新的地方 api 2.4.0 (com.google.android.libraries.places:places:2.4.0) 您可以通过以下代码获取视图,然后在搜索视图上执行点击:
val root: View = autocompleteSupportFragment?.view!!
root.findViewById<View>(R.id.places_autocomplete_search_input).performClick()