android之Volley实现瀑布流
1.首先我们来看下主布局文件activity_main.xml。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<com.chen.photodemo.MyScrollView
android:id="@+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:id="@+id/mianContainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
</LinearLayout>
</com.chen.photodemo.MyScrollView>
</RelativeLayout>
再看看自己定义的一个imageview布局文件line_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageView android:id="@+id/itemImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
2.接下来自己定义的一个类来实现Scrollview类
package com.chen.photodemo; import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ScrollView; /**
*
* @author jianrong.zheng
*
*/
public class MyScrollView extends ScrollView implements OnTouchListener{
private ScrollCallBack scrollCallBack;
private View view;
private Handler refrshHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 1:
//view.getMeasuredHeight()代表LinearLayout需要的高度
//getScrollY()代表Y轴的上边线 +getHeight()固定不变的屏幕高度,如果大于原有LinearLayout高度则证明是向上划
// System.out.println("===getScrollY()"+getScrollY());
// System.out.println("===getHeight()"+getHeight());
if (view.getMeasuredHeight() <= getScrollY() + getHeight()) {
if (scrollCallBack != null) {
scrollCallBack.onBottom();
} } else if (getScrollY() == 0) {
if (scrollCallBack != null) {
scrollCallBack.onTop();
}
} else {
if (scrollCallBack != null) {
scrollCallBack.onScroll();
}
}
break; default:
break;
}
} }; public MyScrollView(Context context) {
super(context);
} public MyScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
} public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
} @Override
public boolean dispatchKeyEvent(KeyEvent event) {
System.out.println();
return super.dispatchKeyEvent(event);
} @Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
return super.onTouchEvent(event);
case MotionEvent.ACTION_UP:
if (view != null && scrollCallBack != null) {
refrshHandler.sendMessageDelayed(refrshHandler.obtainMessage(1), 200);
}
return true;
default:
break;
}
return super.onTouchEvent(event);
} public int computeVerticalScrollRange() {
return super.computeVerticalScrollRange();
} public int computeVerticalScrollOffset() {
return super.computeVerticalScrollOffset();
} public void setScrollCallBack(ScrollCallBack scrollCallBack) {
this.scrollCallBack = scrollCallBack;
this.view = getChildAt(0); //为了获得它的MeasuredHeight,然后和滚动条的ScrollY+getHeight作比较。
this.setOnTouchListener(this);
} /**
*
*接口回调
*/
public interface ScrollCallBack {
public void onTop();
public void onBottom();
public void onScroll();
}
}
3.接下来再看自己要往list放入你想放的图片的的url
package com.chen.photodemo; /**
* 图片
* @author jianrong.zheng
*/
public class ImageConst {
public static String[] urls;
static{
urls = new String[]{
"http://a.hiphotos.baidu.com/pic/w%3D230/sign=bf59456cc9fcc3ceb4c0ce30a244d6b7/4afbfbedab64034f80b90b48aec379310a551d0c.jpg",
"http://d.hiphotos.baidu.com/pic/w%3D230/sign=e92db34bc995d143da76e32043f18296/8601a18b87d6277f35a56d7029381f30e824fcc7.jpg",
"http://a.hiphotos.baidu.com/pic/w%3D230/sign=f71ba7639c16fdfad86cc1ed848e8cea/241f95cad1c8a78699e316466609c93d71cf50a8.jpg",
"http://f.hiphotos.baidu.com/pic/w%3D230/sign=dbe3e3fab8014a90813e41be99763971/63d0f703918fa0eccd0e3845279759ee3c6ddba9.jpg",
"http://d.hiphotos.baidu.com/pic/w%3D230/sign=bb37f35dbd315c6043956cecbdb0cbe6/d000baa1cd11728b1cd9d36ac9fcc3cec2fd2c85.jpg",
"http://c.hiphotos.baidu.com/pic/w%3D230/sign=59a8fcd9f91986184147e8877aec2e69/3c6d55fbb2fb4316879103c221a4462308f7d3f8.jpg",
"http://e.hiphotos.baidu.com/pic/w%3D230/sign=20f4fb5b96dda144da096bb182b6d009/95eef01f3a292df5ccddf35dbd315c6034a8735f.jpg",
"http://e.hiphotos.baidu.com/pic/w%3D230/sign=dc101ae5bba1cd1105b675238910c8b0/d01373f082025aaf80c3801efaedab64024f1a75.jpg",
"http://f.hiphotos.baidu.com/pic/w%3D230/sign=4e07a985fd039245a1b5e60cb795a4a8/024f78f0f736afc3f4f8602db219ebc4b6451285.jpg",
"http://e.hiphotos.baidu.com/pic/w%3D230/sign=1522ba04b151f819f1250449eab44a76/58ee3d6d55fbb2fb6f26643d4e4a20a44623dc8b.jpg",
"http://d.hiphotos.baidu.com/pic/w%3D230/sign=348b736477c6a7efb926af25cdfbafe9/4034970a304e251f46503243a686c9177e3e53e2.jpg",
"http://b.hiphotos.baidu.com/pic/w%3D230/sign=60e9d1767aec54e741ec1d1d89399bfd/d058ccbf6c81800a580ed546b03533fa838b478a.jpg",
"http://b.hiphotos.baidu.com/pic/w%3D230/sign=3f09e4c9ca1349547e1eef67664f92dd/b812c8fcc3cec3fd9dd5fb3dd788d43f8694278a.jpg",
"http://b.hiphotos.baidu.com/pic/w%3D230/sign=3f09e4c9ca1349547e1eef67664f92dd/b812c8fcc3cec3fd9dd5fb3dd788d43f8694278a.jpg",
"http://e.hiphotos.baidu.com/pic/w%3D230/sign=f95c1f21b3b7d0a27bc9039efbee760d/e1fe9925bc315c6060c0187c8cb1cb13485477cf.jpg",
"http://f.hiphotos.baidu.com/pic/w%3D230/sign=3497a62dc8177f3e1034fb0e40ce3bb9/7a899e510fb30f24a7f8d898c995d143ac4b03ce.jpg",
"http://a.hiphotos.baidu.com/pic/w%3D230/sign=f61a1f6efcfaaf5184e386bcbc5494ed/94cad1c8a786c917473fe571c83d70cf3bc757bd.jpg",
"http://f.hiphotos.baidu.com/pic/w%3D230/sign=55b651b1279759ee4a5067c882fa434e/b7003af33a87e9506866c6d011385343fbf2b41c.jpg",
"http://e.hiphotos.baidu.com/pic/w%3D230/sign=3a06cb97cf1b9d168ac79d62c3dfb4eb/314e251f95cad1c8eb46a56f7e3e6709c83d51fc.jpg",
"http://g.hiphotos.baidu.com/pic/w%3D230/sign=ea85ce6bf11f3a295ac8d2cda924bce3/c83d70cf3bc79f3d9e0d8b85bba1cd11738b2992.jpg",
"http://h.hiphotos.baidu.com/pic/w%3D230/sign=8b92084b359b033b2c88fbd925cf3620/203fb80e7bec54e769690f51b8389b504ec26af3.jpg",
"http://b.hiphotos.baidu.com/pic/w%3D230/sign=60e61a8883025aafd33279c8cbecab8d/060828381f30e924096cc0794d086e061c95f7aa.jpg",
"http://a.hiphotos.baidu.com/pic/w%3D230/sign=e869d0a6d52a283443a631086bb4c92e/00e93901213fb80ef3dca16c37d12f2eb9389478.jpg",
"http://e.hiphotos.baidu.com/pic/w%3D230/sign=b9aae94c0ff41bd5da53eff761db81a0/b64543a98226cffcdaa3b1d5b8014a90f703eac8.jpg",
"http://d.hiphotos.baidu.com/pic/w%3D230/sign=3bde0aab08f79052ef1f403d3cf2d738/9213b07eca80653817adddab96dda144ac3482c6.jpg",
"http://b.hiphotos.baidu.com/pic/w%3D230/sign=639f5a60a9d3fd1f3609a539004f25ce/b7fd5266d01609244a0c6b55d50735fae6cd3457.jpg"
};
}
}
4.最后我们来看下activity中是如何实现的。
package com.chen.photodemo; import java.util.ArrayList;
import java.util.List; import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout; import com.android.volley.RequestQueue;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
/**
*
* @author jianrong.zheng
*
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
public class MainActivity extends Activity{
private static final int column = 3;//3列
private static final int pageCount = 12; //每页加载个数
private int currentPage = 0; //当前页
private int columnWidth = 0;//列宽
private LinearLayout mianContainer;//主容器
private RequestQueue queue;
private List<LinearLayout> columnLayouts = new ArrayList<LinearLayout>();
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mianContainer = (LinearLayout) findViewById(R.id.mianContainer);
queue = Volley.newRequestQueue(this);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
columnWidth = (width - 4)/ 3 ;//4为中间2条空隙
((MyScrollView)findViewById(R.id.scrollView)).setScrollCallBack(new MyScrollCallBack());
addColumn(); } /**
* 滚动回调
*/
class MyScrollCallBack implements MyScrollView.ScrollCallBack { @Override
public void onTop() { } @Override
public void onBottom() {
currentPage++;
addImageView2Column();
} @Override
public void onScroll() { } } /**
* 构造列
*/
private void addColumn() {
for(int i = 0;i < column;i++) {//构造列
LinearLayout columnLayout = new LinearLayout(this);
columnLayout.setLayoutParams(new LayoutParams(columnWidth, LayoutParams.MATCH_PARENT));
columnLayout.setOrientation(LinearLayout.VERTICAL);
columnLayouts.add(columnLayout);
mianContainer.addView(columnLayout);
}
addImageView2Column();
} /**
* 构造完后开始加入imageView到列中
*/
private void addImageView2Column() {
//网上抄的方法,目前没发现什么BUG
int columnIndex = 0;
int imageCount = ImageConst.urls.length;
for(int i = currentPage * pageCount;i< (currentPage +1)*pageCount && i < imageCount;i++) {
columnIndex = columnIndex >= column ? columnIndex = 0 : columnIndex;
ImageView itemImage = new ImageView(this);
itemImage.setLayoutParams(new LayoutParams(columnWidth,LayoutParams.WRAP_CONTENT));
itemImage.setPadding(2, 2, 2, 2);
columnLayouts.get(columnIndex).addView(itemImage);
downloadImage(itemImage,i);
columnIndex++;
}
} /**
* 下载图片,自带缓存
* @param itemImage
* @param index
*/
private void downloadImage(final ImageView itemImage, int index) {
//columnWidth这个是设置下载图片的maxWidth,0代表不限定
ImageRequest request = new ImageRequest(ImageConst.urls[index], new Listener<Bitmap>() {
@Override
public void onResponse(Bitmap response) {
itemImage.setImageBitmap(response);
}
}, columnWidth, 0, Config.RGB_565, null);
request.setShouldCache(true);//设置缓存 缓存路径看我以前的帖子
queue.add(request); } @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
如果大家有更好地方法,希望大家多多推荐,相互学习。
来看看效果图吧!




android之Volley实现瀑布流的更多相关文章
- 【转】android中最好的瀑布流控件PinterestLikeAdapterView
[源地址]http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0919/1696.html 之前我们介绍过一个开源的瀑布流控件Stag ...
- Android UI 之WaterFall瀑布流效果
所谓瀑布流效果,简单说就是宽度相同但是高度不同的一大堆图片,分成几列,然后像水流一样向下排列,并随着用户的上下滑动自动加载更多的图片内容. 语言描述比较抽象,具体效果看下面的截图: ...
- [转]android中最好的瀑布流控件PinterestLikeAdapterView
PinterestLikeAdapterView 项目地址:https://github.com/GDG-Korea/PinterestLikeAdapterView 使用方法类似于ListView下 ...
- [Android Pro] RecyclerView实现瀑布流效果(二)
referece to : http://blog.csdn.net/u010687392 在上篇中我们知道RecyclerView中默认给我们提供了三种布局管理器,分别是LinearLayoutMa ...
- 采取Volley,实现瀑布流
今天停止php,在研究Volley框架的源代码,实现了瀑布流的效果. 为了实现最终的级联效应,一些需要掌握的知识: (1)自己定义布局,由于我们要监听滑究竟部的事件就要实现自己定义的ScrollVie ...
- android 瀑布流
我们还是来看一款示例: 看起来很像我们的gridview吧,不过又不像,因为item大小不固定的,看起来是不是别有一番风味,确实如此.就如我们的方角图形,斯通见惯后也就出现了圆角.下面我简单介绍下实现 ...
- Android瀑布流照片墙实现,体验不规则排列的美感
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/10470797 传统界面的布局方式总是行列分明.坐落有序的,这种布局已是司空见惯,在 ...
- android自定义viewgroup之我也玩瀑布流
先看效果图吧, 继上一篇<android自定义viewgroup实现等分格子布局>中实现的布局效果,这里稍微有些区别,每个格子的高度不规则,就是传说的瀑布流布局,一般实现这种效果,要么用第 ...
- android瀑布流效果(仿蘑菇街)
Android 转载分享(10) 我们还是来看一款示例:(蘑菇街) 看起来很像我们的gridview吧,不过又不像,因为item大小不固定的,看起来是不是别有一番风味,确实如此. ...
随机推荐
- webpack初学
写在前面的话 阅读本文之前,先看下面这个webpack的配置文件,如果每一项你都懂,那本文能带给你的收获也许就比较有限,你可以快速浏览或直接跳过:如果你和十天前的我一样,对很多选项存在着疑惑,那花一段 ...
- google closure--继承模块一:goog.inherits()
最近在项目是使用了google closure框架,深入研究了一下源码,先从最简单的继承系统吧. 废话不多说,closure继承模块主要有2个API: goog.inherits(childCtor, ...
- extjs combobox
states.js中 Ext.example.states=[ ['AL','ALabama','The Heart of Dixie'], ['AK','Alaska','The Land of t ...
- Exploratory Undersampling for Class-Imbalance Learning
Abstract - Undersampling is a popular method in dealing with class-imbalance problems, which uses on ...
- SQLserver日期函数
------------------日期转化成年月日时分秒毫秒--------------- select 'R'+CONVERT(varchar(100), GETDATE(), 112)+ri ...
- 细说JAVA反射
Reflection 是 Java 程序开发语言的特征之一,它允许运行中的 Java 程序对自身进行检查,或者说“自审”,并能直接操作程序的内部属性.例如,使用它能获得 Java 类中各成员的名称并显 ...
- SQLite常用命令总结
1) 创建数据库文件: >SQLite3 d:\test.db 回车 就生成了一个test.db在d盘. 这样同时也SQLite3挂上了这个test.db 2) 用.help可以看看有什么命令 ...
- entity framework 新手入门篇(1)-建立模型
entity framework是微软官方免费提供给大家的一套ORM(Object Relational Mapping对象关系映射)解决方案.它不仅可以帮助我们解决数据缓存的问题,还能在最小的开销下 ...
- 一个百万数量级的mysql实例
1.想做数据库调优的学习首先就要有一个较大数据集合的实例,在网上找了很久都没有找到具体的实例,后来在书中看到了employees_db字样,发现 mysql官方提供了一个做测试的较大的数据集,这正是我 ...
- The first day to learn Englisht
IF you want to go fast,to alone. IF you want to go far,go with others.