欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android开发之ListView的head消失页面导航栏的渐变出现和隐藏

程序员文章站 2024-02-28 12:15:34
1.fragment页面xml布局:

1.fragment页面xml布局:

<relativelayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:ptr="http://schemas.android.com/apk/res-auto"
tools:context=".fragment.home.homestorefragment"
>
<com.handmark.pulltorefresh.library.pulltorefreshlistview
android:id="@+id/lv_home_store_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
ptr:ptrdrawable="@drawable/default_ptr_flip"
ptr:ptranimationstyle="flip"
/>
<!--top 搜索栏-->
<linearlayout
android:id="@+id/ll_top_search"
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:id="@+id/iv_search_icon"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/icon_navbar_search"
android:layout_marginright="5dp"
/>
<edittext
android:id="@+id/et_store_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="输入商家或商品名"
android:textcolorhint="@color/shenhui"
android:background="@null"
/>
</linearlayout>
</linearlayout>
</relativelayout>

2.主要代码:

private boolean isflingscroll;
private view headview;
private pulltorefreshlistview lvhomestore;
initview(){
lvhomestore = (pulltorefreshlistview) view.findviewbyid(r.id.lv_home_store_list);
lvhomestore.setmode(pulltorefreshbase.mode.both);
listview listview = lvhomestore.getrefreshableview();
headview = initheadview();
abslistview.layoutparams layoutparams = new abslistview.layoutparams(abslistview.layoutparams.match_parent, abslistview.layoutparams.wrap_content);//这句要加上去
headview.setlayoutparams(layoutparams);
listview.addheaderview(headview);
lvhomestore.setadapter(adapter);
lvhomestore.setonscrolllistener(this);
}
@override
public void onscrollstatechanged(abslistview view, int scrollstate) {
if (scrollstate == scroll_state_fling) {//手指离开手机界面,listview还在滑动
isflingscroll = true;
} else if (scrollstate == scroll_state_touch_scroll) {//手指在界面上滚动的情况
isflingscroll = false;
}
}
@override
public void onscroll(abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount) {
showsearchbarshow();
}
private void showsearchbarshow() {
int headbottomtoparenttop = headview.getheight() + headview.gettop();
log.d("homestore", "headview.getheight(): " + headview.getheight());
log.d("homestore", "headview.gettop(): " + headview.gettop());
log.d("homestore", "headbottomtoparenttop: " + headbottomtoparenttop);
if (!isflingscroll) {//手指在界面滑动的情况
int height = layoutsearch.getheight();
log.d("homestore", "height: " + height);
if (headbottomtoparenttop > height) {
layoutsearch.setvisibility(view.invisible);
} else if (headbottomtoparenttop <= height) {//缓慢滑动,这部分代码工作正常,快速滑动,里面的数据就跟不上节奏了。
float alpha = (height - headbottomtoparenttop) * 1f / height;
log.d("homestore", "alpha: " + alpha);
layoutsearch.setalpha(alpha);
layoutsearch.setvisibility(view.visible);
}
if (!headview.isshown()){//解决快速滑动,上部分代码不能正常工作的问题。
layoutsearch.setalpha(1);
layoutsearch.setvisibility(view.visible);
}
} else {//手指离开,listview还在滑动,一般情况是列表快速滑动,这种情况直接设置导航栏的可见性
if (!headview.isshown()) {
if (!layoutsearch.isshown()){
layoutsearch.setvisibility(view.visible);
layoutsearch.setalpha(1);
}
} else {
if (layoutsearch.isshown()){
layoutsearch.setvisibility(view.invisible);
}
}
}
}

以上所述是小编给大家介绍的android开发之listview的head消失页面导航栏的渐变出现和隐藏,希望对大家有所帮助