码迷,mamicode.com
首页 > 其他好文 > 详细

采用Volley,实现瀑布流

时间:2014-11-02 22:41:29      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   io   ar   os   java   for   

今天停下php,研究了下Volley框架的源码,实现了瀑布流的效果。

要实现最终的瀑布流效果,需要掌握几点知识:

(1)自定义布局,因为我们要监听滑到底部的事件就要实现自定义的ScrollView并通过回调函数实现监听

(2)对Vollet框架的掌握,我们需要新建一个requestQueue队列,通过研究源码发现在新建这个队列的时候传入相应的构造函数,然后会调整其中的start方法,有四个线程池收发请求,每个在一个while循环中实现队列的监听。

(3)动态布局,在自定义的scrollView里面放一个LinearLay然后在代码里面计算每列的宽度,添加ImageView到每列的布局里面。

自定义的MyScrollView

package com.fallview;

import com.android.volley.RetryPolicy;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ScrollView;

public class MyScrollView extends ScrollView implements OnTouchListener {

	public MyScrollView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	ScrollTouch scrollTouch;

	public void setScrollTouch(ScrollTouch touch) {
		scrollTouch = touch;
		this.setOnTouchListener(this);
	}

	@Override
	public boolean onTouch(View view, MotionEvent arg1) {
		// TODO Auto-generated method stub

		switch (arg1.getAction()) {
		case MotionEvent.ACTION_DOWN:

			break;
		case MotionEvent.ACTION_UP:
			if (view.getMeasuredHeight() <= getHeight() + getScrollY()) {
				scrollTouch.onButtom();
			}

			return true;

		default:
			break;
		}

		return false;
	}

	public interface ScrollTouch {

		public void onButtom();
	}

}

实现的java主类

package com.fallview;

import com.android.volley.RequestQueue;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
import com.example.wangyitest.R;
import com.fallview.MyScrollView.ScrollTouch;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class FallImageAct extends Activity implements PicAdds {
	String[] myPics = pics;
	LinearLayout linearLayout;
	int culomWidth;
	int culNum;
	RequestQueue queue;
	int time = 1;

	class scrollListener implements ScrollTouch {

		@Override
		public void onButtom() {
			// TODO Auto-generated method stub
			intLayOne();
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.falllay);
		linearLayout = (LinearLayout) findViewById(R.id.Linearlay);
		((MyScrollView) findViewById(R.id.scroll))
				.setScrollTouch(new scrollListener());
		int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
		culomWidth = (screenWidth - 4) / 3;
		culNum = myPics.length / 3;
		queue = Volley.newRequestQueue(getApplicationContext());
		iniadd3Lay();
		intLayOne();

	}

	LinearLayout.LayoutParams params;

	private void iniadd3Lay() {
		// TODO Auto-generated method stub
		layout1 = new LinearLayout(getApplicationContext());
		params = new LinearLayout.LayoutParams(culomWidth,
				LinearLayout.LayoutParams.WRAP_CONTENT);
		layout1.setPadding(2, 2, 2, 2);
		layout1.setOrientation(LinearLayout.VERTICAL);
		layout1.setLayoutParams(new LinearLayout.LayoutParams(culomWidth,
				LinearLayout.LayoutParams.WRAP_CONTENT));
		linearLayout.addView(layout1);

		layout2 = new LinearLayout(getApplicationContext());
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				culomWidth, LinearLayout.LayoutParams.WRAP_CONTENT);
		layout2.setPadding(2, 2, 2, 2);
		layout2.setOrientation(LinearLayout.VERTICAL);
		layout2.setLayoutParams(new LinearLayout.LayoutParams(culomWidth,
				LinearLayout.LayoutParams.WRAP_CONTENT));
		linearLayout.addView(layout2);

		layout3 = new LinearLayout(getApplicationContext());
		params = new LinearLayout.LayoutParams(culomWidth,
				LinearLayout.LayoutParams.WRAP_CONTENT);
		layout3.setPadding(2, 2, 2, 2);
		layout3.setOrientation(LinearLayout.VERTICAL);
		layout3.setLayoutParams(new LinearLayout.LayoutParams(culomWidth,
				LinearLayout.LayoutParams.WRAP_CONTENT));
		linearLayout.addView(layout3);

	}

	LinearLayout layout1;
	LinearLayout layout2;
	LinearLayout layout3;

	private void intLayOne() {
		// TODO Auto-generated method stub

		time++;
		if (time > 5)
			return;

		for (int i = 0; i < culNum; i++) {
			ImageRequest imageRequest = new ImageRequest(pics[i],
					new Listener<Bitmap>() {

						@Override
						public void onResponse(Bitmap response) {
							// TODO Auto-generated method stub
							ImageView imageView = new ImageView(
									getApplicationContext());
							imageView.setLayoutParams(new LayoutParams(params));
							imageView.setImageBitmap(response);
							layout1.addView(imageView);
						}
					}, culomWidth, 0, Config.RGB_565, null);
			queue.add(imageRequest);
		}

		intLayTwo();
	}

	private void intLayTwo() {
		// TODO Auto-generated method stub

		for (int i = culNum; i < 2 * culNum; i++) {
			ImageRequest imageRequest = new ImageRequest(pics[i],
					new Listener<Bitmap>() {

						@Override
						public void onResponse(Bitmap response) {
							// TODO Auto-generated method stub
							ImageView imageView = new ImageView(
									getApplicationContext());
							imageView.setLayoutParams(new LayoutParams(params));
							imageView.setImageBitmap(response);
							layout2.addView(imageView);
						}
					}, culomWidth, 0, Config.RGB_565, null);
			queue.add(imageRequest);
		}
		intLaythree();
	}

	private void intLaythree() {
		// TODO Auto-generated method stub

		for (int i = 2 * culNum; i < pics.length; i++) {
			ImageRequest imageRequest = new ImageRequest(pics[i],
					new Listener<Bitmap>() {

						@Override
						public void onResponse(Bitmap response) {
							// TODO Auto-generated method stub
							ImageView imageView = new ImageView(
									getApplicationContext());
							imageView.setLayoutParams(new LayoutParams(params));
							imageView.setImageBitmap(response);
							layout3.addView(imageView);
						}
					}, culomWidth, 0, Config.RGB_565, null);
			queue.add(imageRequest);
		}

	}
}


bubuko.com,布布扣



采用Volley,实现瀑布流

标签:android   style   blog   http   io   ar   os   java   for   

原文地址:http://blog.csdn.net/scboyhj__/article/details/40713995

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!