SoFunction
Updated on 2025-04-10

Android development: ListView's head disappears and the gradient appears and hides the navigation bar of the navigation bar

Page xml layout:

<RelativeLayout
xmlns:andro
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:ptr="/apk/res-auto"
tools:context="."
>
<
android:
android:layout_width="match_parent"
android:layout_height="match_parent"
ptr:ptrDrawable="@drawable/default_ptr_flip"
ptr:ptrAnimationStyle="flip"
/>
<!--top Search bar-->
<LinearLayout
android:
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@color/zuti"
android:visibility="invisible"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:background="@drawable/shape_edit_cornor"
android:gravity="center"
>
<ImageView
android:
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/icon_navbar_search"
android:layout_marginRight="5dp"
/>
<EditText
android:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter the merchant or product name"
android:textColorHint="@color/shenhui"
android:background="@null"
/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>

2. Main code:

private boolean isFlingScroll;
private View headView;
private PullToRefreshListView lvHomeStore;
initView(){
lvHomeStore = (PullToRefreshListView) (.lv_home_store_list);
();
ListView listView = ();
headView = initHeadView();
 layoutParams = new (.MATCH_PARENT, .WRAP_CONTENT);//This sentence needs to be added(layoutParams);
(headView);
(adapter);
(this);
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == SCROLL_STATE_FLING) {//The finger leaves the phone interface, and the Listview is still slidingisFlingScroll = true;
} else if (scrollState == SCROLL_STATE_TOUCH_SCROLL) {//State of finger scrolling on the interfaceisFlingScroll = false;
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
showSearchBarShow();
}
private void showSearchBarShow() {
int headBottomToParentTop = () + ();
("homeStore", "(): " + ());
("homeStore", "(): " + ());
("homeStore", "headBottomToParentTop: " + headBottomToParentTop);
if (!isFlingScroll) {//Sometimes the fingers slide on the interfaceint height = ();
("homeStore", "height: " + height);
if (headBottomToParentTop > height) {
();
} else if (headBottomToParentTop <= height) {//Swipe slowly, this part of the code works normally, swipe quickly, and the data inside can't keep up with the pace.float alpha = (height - headBottomToParentTop) * 1f / height;
("homeStore", "alpha: " + alpha);
(alpha);
();
}
if (!()){//Solve the problem that fast swipe and the above code does not work properly.(1);
();
}
} else {//The finger is leaving, the listview is still sliding. Generally, the list is swiping quickly. In this case, the visibility of the navigation bar is directly set.if (!()) {
if (!()){
();
(1);
}
} else {
if (()){
();
}
}
}
}

The above is the gradient of the navigation bar of the head disappearing page of the ListView in Android development introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!