开源控件PullToRefreshGridView的使用(一)

该开源项目地址:https://github.com/chrisbanes/Android-PullToRefresh

1.单排单列和单排双列

先看效果图:

         

核心代码:

a、定义一个枚举类

    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);

原文地址:https://www.cnblogs.com/yushilong/p/3891726.html