android开源控件PullToRefreshGridView的使用

1.单排单列和单排双列

先看效果图:

         

核心代码:

a、定义一个枚举类

1
2
3
public enum Type {
     LINE_ONE, LINE_TWO //LINE_ONE单排单列   LINE_TWO单排双列
}

提供枚举类type的set和get方法。

b、新建一个adapter继承baseadapter,重写getView方法

复制代码
if (Type.LINE_ONE == type)
   {
      convertView = LayoutInflater.from(mContext.getActivity()).inflate(R.layout.b5m_search_result_item, null);
     }
else if (Type.LINE_TWO == type)
     {
         convertView = LayoutInflater.from(mContext.getActivity()).inflate(R.layout.b5m_search_result_item_grid, null);
      }
复制代码

c、动态改变布局格式

mGridView.setAdapter(mAdapter);

mGridView.setNumColumns(Type.LINE_ONE == type?1:2);

在PullToRefreshGridView中就是

mPullToRefreshGridView.getRefreshableView.setNumColumns(Type.LINE_ONE == type?1:2);

注意事项:

防止GridView缓存view对布局的影响,更换布局样式先应先清掉缓存view

mPullToRefreshGridView.setAdapter(null);

然后再设置adapter

mPullToRefreshGridView.setAdapter(mAdapter);

int numColums = mPullToRefreshGridView.getRefreshableView.getNumColums();

mAdapter.setType(numColums==1?Type.LINE_TWO:Type.LINE_ONE);





2.添加headView

先看效果图:

上面整个是一个PullToRefreshGridView,第一张图(防晒大作战求别黑)就是一个headView。

核心代码:

继承于baseadapter的adapter重写下面的方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
     @Override
     public int getViewTypeCount()
     {
         // TODO Auto-generated method stub
         return 2 ;
     }
 
     @Override
     public int getItemViewType( int position)
     {
         // TODO Auto-generated method stub
         if (position == 0 )
         {
             return 0 ;
         }
         return 1 ;
     }
 
getView方法中加判断条件:
 
             if (getItemViewType(position) == 0 )
             {
                 convertView = View.inflate(activity, R.layout.banner_detail_head, null );
             }
             else
             {
                 convertView = LayoutInflater.from(activity).inflate(R.layout.mylayout, null );
             }
其实原理就是把第GridView的第一个item View自定义为headView。






3.往上滑动隐藏头部,往下滑动显示头部

先看效果图:

   

这里要说明一下的是:

a、头部的隐藏与显示是一个动画效果。

b、头部的隐藏与显示对PullToRefreshGridView的布局不会产生任何影响,主要是往上隐藏的时候PullToRefreshGridView不会突然往上移

    往下显示的时候PullToRefreshGridView不会突然往下移,不会有这些不连贯或者卡顿的感觉。

c、是否隐藏与显示是对比滑动过程中两次firstVisiblePosition的大小,也就是判断是上滑还是下滑。

核心代码:

a、布局部分:

1
2
3
4
5
6
7
8
9
10
11
12
13
<?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设置滑动监听

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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、动画部分

1
2
3
4
5
6
7
8
9
10
11
<?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" />
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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 ;
         }
     });
}

 


你可能感兴趣的:(android开源控件PullToRefreshGridView的使用)