gpt4 book ai didi

java - 如何保持 Material 搜索 View 默认打开

转载 作者:搜寻专家 更新时间:2023-11-01 08:29:55 24 4
gpt4 key购买 nike

我正在使用 https://github.com/MiguelCatalan/MaterialSearchView用于搜索 View 。我想在进入 materialsearchactivity 时保持搜索 View 打开。

search view

我现在的代码是

public class search extends AppCompatActivity {
MaterialSearchView searchView;
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
searchView = (MaterialSearchView)findViewById(R.id.search_view);
toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Search");
searchView.showSearch();
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search, menu);

MenuItem item = menu.findItem(R.id.action_search);
searchView.setMenuItem(item);

return true;
}
}

根据 Nayan Srivastava 的回答更新为以下代码后

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
searchView = (MaterialSearchView)findViewById(R.id.search_view);
searchView.performClick();
toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Search");
searchView.showSearch();
}

我收到以下错误..

FATAL EXCEPTION: main
Process: com.buckydroid.anonchat, PID: 7850
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.buckydroid.anonchat/com.buckydroid.anonchat.search}: java.lang.IllegalStateException: Cannot start this animator on a detached view!
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6126)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.IllegalStateException: Cannot start this animator on a detached view!
at android.view.RenderNode.addAnimator(RenderNode.java:786)
at android.view.RenderNodeAnimator.setTarget(RenderNodeAnimator.java:300)
at android.view.RenderNodeAnimator.setTarget(RenderNodeAnimator.java:282)
at android.animation.RevealAnimator.<init>(RevealAnimator.java:37)
at android.view.ViewAnimationUtils.createCircularReveal(ViewAnimationUtils.java:71)
at com.miguelcatalan.materialsearchview.utils.AnimationUtil.reveal(AnimationUtil.java:86)
at com.miguelcatalan.materialsearchview.MaterialSearchView.setVisibleWithAnimation(MaterialSearchView.java:553)
at com.miguelcatalan.materialsearchview.MaterialSearchView.showSearch(MaterialSearchView.java:519)
at com.miguelcatalan.materialsearchview.MaterialSearchView.showSearch(MaterialSearchView.java:501)
at com.buckydroid.anonchat.search.onCreate(search.java:25)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6126) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

最佳答案

错误似乎是由于动画重叠造成的。要禁用动画,只需使用:

searchView.showSearch(false);

要回答评论中提出的其他问题(如何将其发送回之前的 Activity ),请使用:

searchView.setOnSearchViewListener(new MaterialSearchView.SearchViewListener() {
@Override
public void onSearchViewShown() {

}

@Override
public void onSearchViewClosed() {
//To close the current activity
search.this.finish();
}
});

关于java - 如何保持 Material 搜索 View 默认打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41738488/

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