該開源項目地址: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);