Pull more waterfall streams from the Android pull-down refresh (with source code)

Source: Internet
Author: User
Tags addall

I believe many of my friends often use waterfall streams. however, we often encounter this problem. For example, the waterfall stream cannot be pulled down and refreshed, or pulled up more. Some are a simple touch event listener that pulls and loads more, but obviously, such features and user experience cannot be improved. so today, I took the time to develop the waterfall stream that can be pulled down and pulled down. I learned from the pull-down control of a fairy. let's take a look at it now.

Because the simulator screen is too small, I first set it to display two images in a row, you can obviously see the pull-down refresh and pull-up loading more.

Now let's take a look at our 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<LinearLayout> 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<String> currentDownloadList = new ArrayList<String>();        /**     * the total list of images's URLs     */    private List<String> imageUrlsList = new ArrayList<String>();    @Override    protected 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<LinearLayout>();        for (int i = 0; i < column_count; 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<String> imgNameList     */    public void refreshContainer(List<String> imgNameList) {        int columnNumber = 0;        imageUrlsList.clear();        imageUrlsList.addAll(imgNameList);        //remove all children images        for (int index = 0; index < column_count; index ++) {            containerList.get(index).removeAllViews();        }                for (int index = 0; index < imageUrlsList.size(); index ++) {            //check the  current column index by the position of the list            columnNumber = index % column_count;             addImage(imageUrlsList.get(index), columnNumber);        }            }        /**     * add items to waterfall     * @param imgNameList     */    public void addItemsToContainer(List<String> imgNameList) {        int columnNumber = 0;        int usualListLength = imageUrlsList.size();        imageUrlsList.addAll(imgNameList);        for (int index = usualListLength; index < imageUrlsList.size(); index ++) {            //check the  current column index by the position of the list            columnNumber = 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);    }       @Override    public void onFooterRefresh(PullToRefreshView view) {        new Thread(){            public void run() {                try {                    //to do network task                    Thread.sleep(1000);                    mHandler.sendEmptyMessage(MSG_WHAT_MORE);                } catch (Exception e) {                    e.printStackTrace();                }            }        }.start();    }    @Override    public void onHeaderRefresh(PullToRefreshView view) {        new Thread() {            public void run() {                try {                    //to do network task                    Thread.sleep(1000);                    mHandler.sendEmptyMessage(MSG_WHAT_REFRESH);                } catch (Exception e) {                    e.printStackTrace();                }            }        }.start();    }        private Handler mHandler = new Handler() {        @Override        public 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");    }}

Here are the main algorithms and functions. Here we use the open-source framework volley to load and process images. Hey hey, let's not talk about it much. I hope you will give me more advice!

: Http://files.cnblogs.com/yinweiliang/ToggleButton.rar

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.