嗨,我想制作带有搜索框的可滚动工具栏。滚动工具栏上的应该是隐藏工具栏并显示搜索框。给我一些建议,说明如何为我的应用程序构建这种UI。
在这里我想要这样的东西。
在这里,我要发布我的xml设计,该设计正在执行该设计附近的功能,但仍然需要提供。
res / xml / collapsing_toolbar.xml
/** Very simple animation no need for start state*/
下面的代码放在Create Mehod上
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initUI(); nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() { @Override public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) { boolean isSearchViewVisible = isVisible(v); if (isSearchViewVisible){ searchView.setVisibility(View.GONE); searchViewMain.setVisibility(View.VISIBLE); } else{ searchView.setVisibility(View.VISIBLE); searchViewMain.setVisibility(View.GONE); } } }); } private boolean isVisible(View view){ Rect scrollBounds = new Rect(); nestedScrollView.getDrawingRect(scrollBounds); float top = 0f; View view1 = view; while (view1 instanceof NestedScrollView){ top += (view1).getY(); view1 = (View)view1.getParent(); } float bottom = top + view.getHeight(); return scrollBounds.top < bottom && scrollBounds.bottom >top; }
Networks.. 5
您可以使用两个搜索视图(一个在工具栏内,另一个在工具栏下方)来实现。在您的布局中:
然后在您的活动中,在屏幕下方的搜索视图滚动进出屏幕时,在工具栏中切换搜索的可见性。
public class ScrollSearchActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_scroll_search_2); final SearchView mainSearchView = findViewById(R.id.search_view); final SearchView toolbarSearchView = findViewById(R.id.search_view_2); final NestedScrollView nestedScrollView = findViewById(R.id.nestedScrollView); nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() { @Override public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) { boolean isSearchViewVisible = isSearchViewVisible(v,mainSearchView); if (isSearchViewVisible){ toolbarSearchView.setVisibility(View.GONE); mainSearchView.setVisibility(View.VISIBLE); } else{ toolbarSearchView.setVisibility(View.VISIBLE); mainSearchView.setVisibility(View.GONE); } } }); } private boolean isSearchViewVisible(NestedScrollView nestedScrollView, View view){ Rect scrollBounds = new Rect(); nestedScrollView.getDrawingRect(scrollBounds); float top = 0f; View view1 = view; while (!(view1 instanceof NestedScrollView)){ top += (view1).getY(); view1 = (View)view1.getParent(); } float bottom = top + view.getHeight(); return scrollBounds.top < bottom && scrollBounds.bottom >top; } }
您应该得到这样的内容:
编辑
用它代替isVisible()方法
private boolean isViewVisible(NestedScrollView nestedScrollView, View view){ Rect scrollBounds = new Rect(); nestedScrollView.getDrawingRect(scrollBounds); float top = 0f; View view1 = view; while (!(view1 instanceof NestedScrollView)){ top += (view1).getY(); view1 = (View)view1.getParent(); } float bottom = top + view.getHeight(); return scrollBounds.top < bottom && scrollBounds.bottom >top; }
并这样称呼它 boolean isSearchViewVisible = isSearchViewVisible(nestedScrollView,mainSearchView);
您可以使用两个搜索视图(一个在工具栏内,另一个在工具栏下方)来实现。在您的布局中:
然后在您的活动中,在屏幕下方的搜索视图滚动进出屏幕时,在工具栏中切换搜索的可见性。
public class ScrollSearchActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_scroll_search_2); final SearchView mainSearchView = findViewById(R.id.search_view); final SearchView toolbarSearchView = findViewById(R.id.search_view_2); final NestedScrollView nestedScrollView = findViewById(R.id.nestedScrollView); nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() { @Override public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) { boolean isSearchViewVisible = isSearchViewVisible(v,mainSearchView); if (isSearchViewVisible){ toolbarSearchView.setVisibility(View.GONE); mainSearchView.setVisibility(View.VISIBLE); } else{ toolbarSearchView.setVisibility(View.VISIBLE); mainSearchView.setVisibility(View.GONE); } } }); } private boolean isSearchViewVisible(NestedScrollView nestedScrollView, View view){ Rect scrollBounds = new Rect(); nestedScrollView.getDrawingRect(scrollBounds); float top = 0f; View view1 = view; while (!(view1 instanceof NestedScrollView)){ top += (view1).getY(); view1 = (View)view1.getParent(); } float bottom = top + view.getHeight(); return scrollBounds.top < bottom && scrollBounds.bottom >top; } }
您应该得到这样的内容:
编辑
用它代替isVisible()方法
private boolean isViewVisible(NestedScrollView nestedScrollView, View view){ Rect scrollBounds = new Rect(); nestedScrollView.getDrawingRect(scrollBounds); float top = 0f; View view1 = view; while (!(view1 instanceof NestedScrollView)){ top += (view1).getY(); view1 = (View)view1.getParent(); } float bottom = top + view.getHeight(); return scrollBounds.top < bottom && scrollBounds.bottom >top; }
并这样称呼它 boolean isSearchViewVisible = isSearchViewVisible(nestedScrollView,mainSearchView);