gpt4 book ai didi

android - 向上滚动时,折叠工具栏中的 SearchView 被隐藏

转载 作者:行者123 更新时间:2023-12-03 17:39:54 25 4
gpt4 key购买 nike

我在 MainActivity 中有一个折叠工具栏,工具栏中有一个 SearchView。单击 searchview 图标时,searchview 会在工具栏中折叠。折叠工具栏时它工作正常。但是,如果我单击搜索图标并向上滚动,折叠的搜索 View 将隐藏。我希望折叠的搜索 View 在向上滚动时可见。我正在关注
这个链接MaterialSearchView

这是折叠工具栏折叠时的屏幕
enter image description here

现在,如果我点击搜索图标,屏幕看起来像这样
enter image description here

到目前为止,搜索 View 工作正常。现在如果我向上滚动屏幕看起来像这样
enter image description here

现在,如果我单击搜索图标,搜索 View 会折叠但它会被隐藏。因此,即使折叠工具栏没有折叠,我也希望搜索 View 在工具栏中折叠。
这是工具栏未折叠并单击搜索图标时我想要的屏幕截图

enter image description here

这是我的 activity_main.xml

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:expandedTitleTextAppearance="@android:color/transparent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<com.daimajia.slider.library.SliderLayout
android:id="@+id/slider"
android:layout_width="match_parent"
android:layout_height="150dp"
/>

</RelativeLayout>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="pin" />
<com.miguelcatalan.materialsearchview.MaterialSearchView
android:id="@+id/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</android.support.design.widget.CollapsingToolbarLayout>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
app:tabIndicatorColor="@color/pink"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</android.support.design.widget.AppBarLayout>



<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:layout_margin="16dp"
android:src="@drawable/ic_launcher_white"
android:layout_gravity="end|bottom"
app:rippleColor="@android:color/white"
app:backgroundTint="@color/primary"
app:elevation="10dp"
/>

这是我的 MainActivity.java
    public class MainActivity extends AppCompatActivity {

private MaterialSearchView searchView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);




final CollapsingToolbarLayout collapsingToolbar =
(CollapsingToolbarLayout)

findViewById(R.id.collapsing_toolbar);

AppBarLayout appBarLayout = (AppBarLayout)

findViewById(R.id.appbar);
collapsingToolbar.setTitle(" ");
appBarLayout.setExpanded(true);

// hiding & showing the title when toolbar expanded & collapsed
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
boolean isShow = false;
int scrollRange = -1;

@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
if (scrollRange == -1) {
scrollRange = appBarLayout.getTotalScrollRange();
toolbar.setBackgroundColor(getResources().getColor(R.color.black_tint));

}
if (scrollRange + verticalOffset == 0) {
collapsingToolbar.setTitle(getString(R.string.app_name));
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu);
isShow = true;
toolbar.setBackgroundColor(getResources().getColor(R.color.primary));

} else if (isShow) {
collapsingToolbar.setTitle(" ");
isShow = false;
toolbar.setBackgroundColor(getResources().getColor(R.color.black_tint));

}
}
});

final ActionBar ab = getSupportActionBar();
ab.setHomeAsUpIndicator(R.drawable.ic_menu);
ab.setDisplayHomeAsUpEnabled(true);


searchView = (MaterialSearchView) findViewById(R.id.search_view);
searchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
//Do some magic
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
//Do some magic
return false;
}
});

searchView.setOnSearchViewListener(new MaterialSearchView.SearchViewListener() {
@Override
public void onSearchViewShown() {
//Do some magic
}

@Override
public void onSearchViewClosed() {
//Do some magic
}
});
}
}

最佳答案

我用 edittext 解决了这个问题和 textchangedlistenerxml

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.coordinatorlayout.widget.CoordinatorLayout

android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorTransparent"
android:fitsSystemWindows="true">

<com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:background="@color/colorBlack_50"
android:theme="@style/AppTheme.AppBarOverlay">

<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="250dp"
android:fitsSystemWindows="true"
android:gravity="bottom"
app:contentScrim="@color/colorBlack_50"
app:expandedTitleMargin="10dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">

//some views

</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
activity
editTextSearch.setVisibility(View.VISIBLE);
editTextSearch.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
searchAudio(editTextSearch.getText().toString());
}

@Override
public void beforeTextChanged(CharSequence cs, int s, int b, int c) {}

@Override
public void onTextChanged(CharSequence query, int s, int b, int c) {}
});

关于android - 向上滚动时,折叠工具栏中的 SearchView 被隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39054134/

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