关于瀑布流,我相信有很多朋友经常会用到.但是呢我们经常也会遇到这个问题,比如瀑布流不能下拉刷新,或者上拉更多,有的是一个简单的touch事件监听上拉加载更多,但是很显然,这样的功能以及用户体验得不到提高.所以,今天抽空把那个可以下拉上拉的瀑布流开发出来,其中借鉴了一位大仙的下拉上拉控件.我们现在开始看下效果图
![](https://img6.php1.cn/3cdc5/963f/5a0/843e314a2850906a.jpeg)
![](https://img6.php1.cn/3cdc5/963f/5a0/9a22321ec28b35aa.jpeg)
![](https://img6.php1.cn/3cdc5/963f/5a0/31cd3360cbad99e9.jpeg)
![](https://img6.php1.cn/3cdc5/963f/5a0/2cb50d0b057eea1f.jpeg)
由于模拟器屏幕太小,所以我先设置他一排显示两张图片,可以很明显的看到下拉刷新和上拉加载更多.
现在来看下我们的MainActivity.java
package com.fay.pullwaterfall;import java.util.ArrayList;
import java.util.List;import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Display;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.android.volley.toolbox.Volley;
import com.fay.pullwaterfall.PullToRefreshView.OnFooterRefreshListener;
import com.fay.pullwaterfall.PullToRefreshView.OnHeaderRefreshListener;
/*** waterfall with refreshing and loading more* @since 2014/05/26* @author Fay* {@link 1940125001@qq.com}*/
public class MainActivity extends Activity implements OnHeaderRefreshListener,OnFooterRefreshListener {private String TAG = "MainActivity";private Display display = null;/*** the width of item*/private int itemWidth = 0;/*** the count of every column*/private int column_count = 2;/*** refresh for message.what*/private final int MSG_WHAT_REFRESH = 1;/*** load more for message.what*/private final int MSG_WHAT_MORE = 2;/*** refresh or load more view*/private PullToRefreshView mPullToRefreshView = null;/*** the list of children containers*/private ArrayList containerList = null;/*** the whole container for waterfall*/private LinearLayout waterfallContainer = null;/*** open source dealing with photos*/private ImageLoader mImageLoader = null;/*** the current list of image's URLs*/private List currentDownloadList = new ArrayList();/*** the total list of images's URLs*/private List imageUrlsList = new ArrayList();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main1);RequestQueue mQueue = Volley.newRequestQueue(getApplicationContext());mImageLoader = new ImageLoader(mQueue, BitmapCache.getInstance());initData();initView();}/*** initialize the view*/private void initView() {mPullToRefreshView = (PullToRefreshView) findViewById(R.id.main_pull_refresh_view);mPullToRefreshView.setOnHeaderRefreshListener(this);mPullToRefreshView.setOnFooterRefreshListener(this);display = this.getWindowManager().getDefaultDisplay();itemWidth = display.getWidth() / column_count;waterfallContainer = (LinearLayout) this.findViewById(R.id.waterfall_container);containerList = new ArrayList();for (int i = 0; i ) {LinearLayout itemLayout = new LinearLayout(this);LinearLayout.LayoutParams itemParam = new LinearLayout.LayoutParams(itemWidth, LayoutParams.WRAP_CONTENT);itemLayout.setPadding(2, 2, 2, 2);itemLayout.setOrientation(LinearLayout.VERTICAL);itemLayout.setLayoutParams(itemParam);containerList.add(itemLayout);waterfallContainer.addView(itemLayout);}addItemsToContainer(currentDownloadList);}/*** to refresh waterfall * @param List imgNameList*/public void refreshContainer(List imgNameList) {int columnNumber = 0;imageUrlsList.clear();imageUrlsList.addAll(imgNameList);//remove all children imagesfor (int index = 0; index ) {containerList.get(index).removeAllViews();}for (int index = 0; index ) {//check the current column index by the position of the listcolumnNumber = index % column_count; addImage(imageUrlsList.get(index), columnNumber);}}/*** add items to waterfall* @param imgNameList*/public void addItemsToContainer(List imgNameList) {int columnNumber = 0;int usualListLength = imageUrlsList.size();imageUrlsList.addAll(imgNameList);for (int index = usualListLength; index ) {//check the current column index by the position of the listcolumnNumber = index % column_count; addImage(imageUrlsList.get(index), columnNumber);}}/*** add ImageView into container* @param String url* @param int columnIndex*/private void addImage(String url, int columnIndex) {ImageView itemImage = (ImageView) LayoutInflater.from(this).inflate(R.layout.waterfallitem, null);ImageListener listener = ImageLoader.getImageListener(itemImage, R.drawable.default_photo, R.drawable.default_photo);mImageLoader.get(url, listener);containerList.get(columnIndex).addView(itemImage);}@Overridepublic void onFooterRefresh(PullToRefreshView view) {new Thread(){public void run() {try {//to do network taskThread.sleep(1000);mHandler.sendEmptyMessage(MSG_WHAT_MORE);} catch (Exception e) {e.printStackTrace();}}}.start();}@Overridepublic void onHeaderRefresh(PullToRefreshView view) {new Thread() {public void run() {try {//to do network taskThread.sleep(1000);mHandler.sendEmptyMessage(MSG_WHAT_REFRESH);} catch (Exception e) {e.printStackTrace();}}}.start();}private Handler mHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {switch (msg.what) {case MSG_WHAT_MORE:mPullToRefreshView.onFooterRefreshComplete();addItemsToContainer(currentDownloadList);break;case MSG_WHAT_REFRESH:mPullToRefreshView.onHeaderRefreshComplete();refreshContainer(currentDownloadList);break;}}};/*** initialize the static URLs just for test.*/private void initData() {currentDownloadList.add("http://g.hiphotos.baidu.com/album/w%3D230/sign=a94d197c8435e5dd902ca2dc46c7a7f5/838ba61ea8d3fd1fa7bb5b14314e251f95ca5f6a.jpg");currentDownloadList.add("http://e.hiphotos.baidu.com/album/w%3D230/sign=af9a677a024f78f0800b9df049300a83/4d086e061d950a7b567fb7ea0bd162d9f2d3c952.jpg");currentDownloadList.add("http://e.hiphotos.baidu.com/album/w%3D230/sign=9c6ef885023b5bb5bed727fd06d3d523/b90e7bec54e736d19b7475269a504fc2d56269bd.jpg");currentDownloadList.add("http://a.hiphotos.baidu.com/album/w%3D230/sign=b540585248540923aa69647da259d1dc/c9fcc3cec3fdfc03aab445c9d53f8794a5c226f6.jpg");currentDownloadList.add("http://b.hiphotos.baidu.com/album/w%3D230/sign=d0765733622762d0803ea3bc90ed0849/359b033b5bb5c9ea6c54ebf3d439b6003af3b37f.jpg");currentDownloadList.add("http://g.hiphotos.baidu.com/album/w%3D230/sign=e6fc366555e736d158138b0bab514ffc/cdbf6c81800a19d8e2ccfcbc32fa828ba71e46bf.jpg");currentDownloadList.add("http://e.hiphotos.baidu.com/album/w%3D230/sign=e640c2ea1ad5ad6eaaf963e9b1cb39a3/42a98226cffc1e17bed9751f4b90f603738de9be.jpg");currentDownloadList.add("http://f.hiphotos.baidu.com/album/w%3D230/sign=6cc9612a5882b2b7a79f3ec701accb0a/2cf5e0fe9925bc3191edf8b65fdf8db1cb137068.jpg");currentDownloadList.add("http://f.hiphotos.baidu.com/album/w%3D230/sign=e0331d0d72f082022d92963c7bfafb8a/9f2f070828381f30a45c7487a8014c086e06f053.jpg");}}
这里就是主要的算法以及功能了.这里用到了开源框架volley加载处理图片.嘿嘿,不多说了,到时候直接上源码.希望各位朋友多多指教!
下载地址:http://files.cnblogs.com/yinweiliang/ToggleButton.rar