Gau*_*ans 4 android android-layout android-fragments android-view google-places
活动加载完成后,我想自动单击片段。
片段定义为:
<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"
/>
Run Code Online (Sandbox Code Playgroud)
我尝试这样做
fragment = findViewById(R.id.place_autocomplete_fragment);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
fragment.performClick();
}
}, 1000);
Run Code Online (Sandbox Code Playgroud)
但是没有用。
有什么方法可以自动单击片段吗?
编辑:就我而言,该片段由
//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);
}
});
Run Code Online (Sandbox Code Playgroud)
您不能单击<fragment>。
点击事件只能View发生。片段是不是一个View。
您可以单击View片段膨胀的a。
@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());
}
Run Code Online (Sandbox Code Playgroud)
由于您使用的PlaceAutocompleteFragment是Play服务中的资源(因此您没有资源),因此您可以在活动中执行以下操作:
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.place_autocomplete_search_input)
.performClick();
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1348 次 |
| 最近记录: |