滑动listview隐藏和显示顶部布局

需求:

1.listview向下滑动时,隐藏顶部布局

2.listview向上滑动到最上面,显示顶部布局

3.顶部布局的隐藏和显示有过渡效果

4.第一次加载listview时,顶部不要隐藏

布局:

注意:android:animateLayoutChanges="true"添加在需要隐藏显示布局的父布局中才能有过渡效果

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/personal_card_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:descendantFocusability="blocksDescendants"
tools:context="com.xuehu365.xuehu.ui.activity.PersonalCardActivity"> <include
android:id="@+id/title"
layout="@layout/mine_title_layout" /> <LinearLayout
android:id="@+id/personal_card_information"
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_below="@id/title"
android:gravity="center_horizontal"
android:orientation="vertical"> <com.xuehu365.xuehu.ui.widget.CircleImageView
android:id="@+id/personal_card_image"
android:layout_width="78dp"
android:layout_height="78dp"
android:layout_marginTop="23dp"
android:src="@mipmap/bg_touxiang" /> <TextView
android:id="@+id/personal_card_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="9dp"
android:textColor="@color/my_font"
android:textSize="16sp" /> <TextView
android:id="@+id/personal_card_company"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textColor="@color/my_gray_font"
android:textSize="14sp" /> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="23dp"
android:layout_marginTop="5dp"
android:orientation="horizontal"> <TextView
android:id="@+id/personal_card_department"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/my_gray_font"
android:textSize="14sp" /> <TextView
android:id="@+id/personal_card_between"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:textColor="@color/my_gray_font"
android:textSize="14sp"
android:visibility="gone" /> <TextView
android:id="@+id/personal_card_duty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/my_gray_font"
android:textSize="14sp" /> </LinearLayout>
</LinearLayout> <LinearLayout
android:id="@+id/personal_card_tabs"
android:layout_width="match_parent"
android:layout_height="43dp"
android:layout_below="@id/personal_card_information"
android:background="@color/white"
android:orientation="horizontal"> <RelativeLayout
android:id="@+id/personal_share_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"> <TextView
android:id="@+id/personal_share"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="看过的直播"
android:textColor="@color/my_gray_font"
android:textSize="18sp" /> <View
android:id="@+id/personal_share_line"
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_alignParentBottom="true"
android:background="@color/my_blue" />
</RelativeLayout> <RelativeLayout
android:id="@+id/personal_community_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"> <TextView
android:id="@+id/personal_community"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="加入的社群"
android:textColor="@color/my_gray_font"
android:textSize="18sp" /> <View
android:id="@+id/personal_community_line"
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_alignParentBottom="true"
android:background="@color/white" />
</RelativeLayout>
</LinearLayout> <android.support.v4.view.ViewPager
android:id="@+id/personal_card_vp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/personal_card_tabs"
android:descendantFocusability="blocksDescendants"></android.support.v4.view.ViewPager>
</RelativeLayout>

代码

private int oldIndex;
private int newIndex;
private boolean isFirstScroll; //列表滑动监听隐藏信息布局
personal_card_lv.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
switch (scrollState) {
case SCROLL_STATE_TOUCH_SCROLL:
oldIndex = view.getLastVisiblePosition();
break;
}
} @Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
newIndex = view.getLastVisiblePosition();
LogHelp.i("scroll", "newIndex:" + newIndex + "--oldIndex:" + oldIndex);
//往下滑
if (newIndex > oldIndex && firstVisibleItem > 0) {
isFirstScroll = true;
((PersonalCardActivity) getActivity()).personal_card_information.setVisibility(View.GONE);
} else if (firstVisibleItem == 0 && isFirstScroll == true) {
//往上滑
((PersonalCardActivity) getActivity()).personal_card_information.setVisibility(View.VISIBLE);
}
}
});
上一篇:cell左右滑动展开更多按钮-MGSwipeTableCell


下一篇:CSS3学习笔记(4)—上下滑动展开的按钮