在 SwipeRefreshLayout 内滚动时隐藏和显示自定义视图
Hide and show custom view on scroll inside a SwipeRefreshLayout
我正在尝试在 SwipeRefreshLayout 中实现 hide/show SearchView on scroll 功能。
我想在用户滚动到底部时显示 SearchView,而在滚动到顶部时隐藏。
我也想保持滑动刷新行为。
尝试了很多例子,但还没有看到接近的结果。
我的xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/loading_bar"/>
<include layout="@layout/network_error"/>
<include layout="@layout/empty_materials_coinlist"/>
<RelativeLayout
android:id="@+id/coinlist_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/coinlist_item_bg">
<RelativeLayout
android:id="@+id/custom_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="exitUntilCollapsed">
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="toolbar_title"
android:textColor="@color/toolbar_text_color"
android:textSize="18sp"
android:typeface="sans"
android:layout_gravity="center" />
</RelativeLayout>
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/custom_toolbar">
<android.widget.SearchView
android:id="@+id/sv_coin"
android:layout_width="match_parent"
android:layout_height="40dp"
android:iconifiedByDefault="false"
app:layout_scrollFlags="scroll|enterAlways|snap"/>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swiperefresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/currenciesRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/coinlist_item_bg"
android:scrollbars="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.v4.widget.SwipeRefreshLayout>
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
试试这个
myRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (dy > 0) {
//check for scroll down
int itemPosition = linearLayoutManager.findFirstCompletelyVisibleItemPosition();
if(itemPosition==list.size()){// your last item your recyclerview
// hide your SearchView here
}
} else {
//check for scroll up
int itemPosition = linearLayoutManager.findLastCompletelyVisibleItemPosition();
if(itemPosition==0){
// show your SearchView here
}
}
}
});
我正在尝试在 SwipeRefreshLayout 中实现 hide/show SearchView on scroll 功能。
我想在用户滚动到底部时显示 SearchView,而在滚动到顶部时隐藏。 我也想保持滑动刷新行为。
尝试了很多例子,但还没有看到接近的结果。
我的xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/loading_bar"/>
<include layout="@layout/network_error"/>
<include layout="@layout/empty_materials_coinlist"/>
<RelativeLayout
android:id="@+id/coinlist_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/coinlist_item_bg">
<RelativeLayout
android:id="@+id/custom_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="exitUntilCollapsed">
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="toolbar_title"
android:textColor="@color/toolbar_text_color"
android:textSize="18sp"
android:typeface="sans"
android:layout_gravity="center" />
</RelativeLayout>
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/custom_toolbar">
<android.widget.SearchView
android:id="@+id/sv_coin"
android:layout_width="match_parent"
android:layout_height="40dp"
android:iconifiedByDefault="false"
app:layout_scrollFlags="scroll|enterAlways|snap"/>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swiperefresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/currenciesRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/coinlist_item_bg"
android:scrollbars="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.v4.widget.SwipeRefreshLayout>
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
试试这个
myRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (dy > 0) {
//check for scroll down
int itemPosition = linearLayoutManager.findFirstCompletelyVisibleItemPosition();
if(itemPosition==list.size()){// your last item your recyclerview
// hide your SearchView here
}
} else {
//check for scroll up
int itemPosition = linearLayoutManager.findLastCompletelyVisibleItemPosition();
if(itemPosition==0){
// show your SearchView here
}
}
}
});