Próbowałem wielu kodów, aby dać mi mniej lub więcej tego, czego szukasz. To alternatywne rozwiązanie, ale trzeba zmienić AppBarLayout do prostego widoku ... Spójrz to:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/annonce.main.coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="RtlHardcoded">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<!-- here you put your search bar, can be any view !-->
<android.support.v7.widget.CardView
android:id="@+id/searchbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="4dp"
app:cardUseCompatPadding="true"
app:layout_behavior="[package].RecyclerSearchBehavior">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="Pesquise aqui"
android:minHeight="58dp"/>
</android.support.v7.widget.CardView>
Uwaga: to jest wewnątrz CoordinatorLayout. Tworzenie Zachowanie Pogląd ten rodzaj paska wyszukiwania używasz:
public class RecyclerSearchBehavior extends CoordinatorLayout.Behavior<CardView> {
//Initial height and location
private int mViewHeight;
private int[] mViewStartLocation;
public RecyclerSearchBehavior(Context context, AttributeSet attrs) {
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, final CardView child, View dependency) {
//the first function called. The initial variables settings.
//getting height
mViewHeight = child.getHeight();
//getting location on screen
mViewStartLocation = new int[2];
child.getLocationOnScreen(mViewStartLocation);
//if the dependecy is your recycler
if (dependency instanceof RecyclerView)
//add scroll listener
((RecyclerView) dependency).addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
//here the magic happens
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
//animate function
animate(child, dy);
}
});
return dependency instanceof RecyclerView;
}
private void animate(CardView child, int dy) {
//the initial position of your search bar is 0, because it is on top, so...
if (child.getY() <= 0)
//dy is an integer that shows you if user scrolls up or down
if (dy >= 0) {
//move to top is a negative value, so *-1
//if that is < than height, scrools up
if ((child.getY() * -1) <= (mViewHeight))
child.setTranslationY(child.getY() - dy * 0.1f);
} else {
//if the position of search bar is < than zero, scrolls down
if (child.getY() < 0)
child.setTranslationY(child.getY() - dy * 0.1f);
//else, put the view on 0y
else child.setY(0);
}
//else, put the view on 0y again. this function is called after any user
//touches... so everything happens fast and with numbers different than
//1
else child.setY(0);
}
}
i zrobione. Twój "pasek wyszukiwania" jest tutaj.
Jedynym problemem jest konieczność dodania dopełnienia do pierwszego elementu w Recyklerze lub przezroczystego widoku jako pierwszego elementu.
Czy chcesz, aby NestedScrollView przewijał się pod AppbarLayout? – jayeshsolanki93
Chcę, aby zawartość NestedScrollView ściśle obejmowała obszar wyszukiwania, ale w tej chwili nad obszarem wyszukiwania nie widać żadnej zawartości, a poniżej znajduje się linia szara, oddzielająca obszar wyszukiwania od treści. Edytuj: Tak, właśnie tego chcę. – joharei
Spróbuj dodać 'android: fitsSystemWindows =" true "' do 'AppbarLayout' i' NestedScrollView' – jayeshsolanki93