該開源項目地址:https://github.com/chrisbanes/Android-PullToRefresh
3.往上滑動隱藏頭部,往下滑動顯示頭部
先看效果圖:

這里要說明一下的是:
a、頭部的隱藏與顯示是一個動畫效果。
b、頭部的隱藏與顯示對PullToRefreshGridView的布局不會產生任何影響,主要是往上隱藏的時候PullToRefreshGridView不會突然往上移
往下顯示的時候PullToRefreshGridView不會突然往下移,不會有這些不連貫或者卡頓的感覺。
c、是否隱藏與顯示是對比滑動過程中兩次firstVisiblePosition的大小,也就是判斷是上滑還是下滑。
核心代碼:
a、布局部分:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/gray"
android:orientation="vertical" >
<!--PullToRefreshGridView-->
<include layout="@layout/pull_gridview_refresh" />
<!--頭部-->
<include layout="@layout/pull_gridview_refresh_head" />
</RelativeLayout>
注意上面提到的頭部不是mPullToRefreshGridView的頭部。
mPullToRefreshGridView必須設置一個頭部,怎么設置頭部,請參考開源控件PullToRefreshGridView的使用(二),mPullToRefreshGridView的頭部的高度必須與布局里面的頭部高度一致,可以在布局
中設定,也可以通過代碼動態設定。
b、mPullToRefreshGridView設置滑動監聽
mPullToRefreshGridView.setOnScrollListener(new OnScrollListener()
{
@Override
public void onScrollStateChanged(AbsListView view , int scrollState)
{
// TODO Auto-generated method stub
if (OnScrollListener.SCROLL_STATE_IDLE == scrollState)
{
if (mAdapter.type == Type.LINE_ONE)
{
firstVisiblePosition = view.getFirstVisiblePosition() + 1;
}
else
{
firstVisiblePosition = view.getFirstVisiblePosition() + 2;
}
}
}
@Override
public void onScroll(AbsListView view , int firstVisibleItem , int visibleItemCount , int totalItemCount)
{
// TODO Auto-generated method stub
if (ll_animation == null || toUp == null)
{
return;
}
int temp;
if (mAdapter.type == Type.LINE_ONE)
{
temp = view.getFirstVisiblePosition() + 1;
}
else
{
temp = view.getFirstVisiblePosition() + 2;
}
if (mAdapter.type == Type.LINE_ONE)
{
if (temp == 1 && !ll_animation.isShown() && !isDownStarted)
{
ll_animation.startAnimation(toDown);
}
}
else
{
if (temp == 2 && !ll_animation.isShown() && !isDownStarted)
{
ll_animation.startAnimation(toDown);
}
}
if ((temp > firstVisiblePosition) && firstVisibleItem != 0)//向上滑
{
if (!isUpStarted && ll_animation.isShown())
{
ll_animation.startAnimation(toUp);
}
// ll_animation.setVisibility(View.GONE);
}
else if (temp < firstVisiblePosition)
{
if (!isDownStarted && !ll_animation.isShown())
{
ll_animation.startAnimation(toDown);
}
// ll_animation.setVisibility(View.VISIBLE);
}
// firstVisiblePosition = temp;
}
});
c、動畫部分
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fromYDelta="0%p"
android:toYDelta="-100%p" />
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fromYDelta="-100%p"
android:toYDelta="0%p" />
private void initAnimation()
{
// TODO Auto-generated method stub
toUp = AnimationUtils.loadAnimation(mActivity, R.anim.searchresult_toup_animation);
toDown = AnimationUtils.loadAnimation(mActivity, R.anim.searchresult_todown_animation);
toUp.setAnimationListener(new AnimationListener()
{
@Override
public void onAnimationStart(Animation animation)
{
// TODO Auto-generated method stub
isUpStarted = true;
}
@Override
public void onAnimationRepeat(Animation animation)
{
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation)
{
// TODO Auto-generated method stub
ll_animation.setVisibility(View.GONE);
isUpStarted = false;
}
});
toDown.setAnimationListener(new AnimationListener()
{
@Override
public void onAnimationStart(Animation animation)
{
// TODO Auto-generated method stub
isDownStarted = true;
}
@Override
public void onAnimationRepeat(Animation animation)
{
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation)
{
// TODO Auto-generated method stub
ll_animation.setVisibility(View.VISIBLE);
isDownStarted = false;
}
});
}
ps:里面有些變量需參考前一章節
