Android下拉刷新(仿网易)

  看这篇博文前,推荐先看本博文的姊妹篇(点击即可)         

 Android Gallery广告(仿网易)

完美PopupWindow(记住用户名模拟)

 

       最近看网易新闻,发现网易的界面相当漂亮,我也很想用反编译来查看源码,弄的我蛋痛,不行啊。我就直接自己写了一个下拉刷新来模拟。

为了便于理解,先上我的的代码测试效果图片:

                                               

代码部分如下:

package com.jefry;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.ScrollView;
import android.widget.TextView;

public class NetNewDemo extends Activity {
	public static ImageView fresh = null;
	View mRefreshView;
	int top = 0;
	int first = 0;
	TextView mRefreshViewText;
	ImageView mRefreshViewImage;
	ImageView mImageView;
	ProgressBar mRefreshViewProgress;
	TextView mRefreshViewLastUpdated;
	
	Handler hand = new Handler(){
		public void handleMessage(android.os.Message msg) {
			 mRefreshViewImage.setVisibility(View.GONE);
        	 mImageView.setVisibility(View.GONE);
        	 mRefreshView.setPadding(mRefreshView.getPaddingLeft(),0,mRefreshView.getPaddingRight(),mRefreshView.getPaddingBottom());
		}
	};
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mRefreshView = (View)findViewById(R.id.layout);
        mRefreshViewImage = (ImageView) findViewById(R.id.pull_to_refresh_image);
        mImageView = (ImageView)findViewById(R.id.going);
        ScrollView sc = (ScrollView)findViewById(R.id.scroll);
        sc.setOnTouchListener(new View.OnTouchListener() {
			public boolean onTouch(View arg0, MotionEvent event) {
				Log.i("TAG", "=============onTouchEvent=================");
		    	//当前手指的Y值
		        final int y = (int) event.getY();
		       // mRefreshViewImage.setImageResource(R.drawable.fresh_going);
		        switch (event.getAction()) {
		            case MotionEvent.ACTION_UP:
		               try{
		            	   first = 0;
		            	   mRefreshViewImage.setImageResource(R.drawable.fresh_going);
		            	   mRefreshView.setPadding(mRefreshView.getPaddingLeft(),0,mRefreshView.getPaddingRight(),mRefreshView.getPaddingBottom());
		            	   new Thread(new Runnable() {
							public void run() {
								try {
									Thread.sleep(1500);
								} catch (InterruptedException e) {
								}
								hand.sendEmptyMessage(1);
							}
						  }).start();
		            	}catch (Exception e) {}
		            	 break;
		            case MotionEvent.ACTION_DOWN:
		                first = y;            
		                break;
		            
		            case MotionEvent.ACTION_MOVE:
		            	mImageView.setVisibility(View.GONE);
		            	int instance = y-first;
		            	if(instance>0 && instance < 30) {
		            		mRefreshViewImage.setVisibility(View.VISIBLE);
		            		mRefreshViewImage.setImageResource(R.drawable.down);
		            	} else if(instance >=30){
		            		mRefreshViewImage.setVisibility(View.VISIBLE);
		            		mRefreshViewImage.setImageResource(R.drawable.fresh_before);
		            	}
		            	
		            	mRefreshView.setPadding(mRefreshView.getPaddingLeft(),instance,mRefreshView.getPaddingRight(),mRefreshView.getPaddingBottom());
		            	//mRefreshViewImage.setImageResource(R.drawable.fresh_going);
		            	break;
		        }
				return true;
			}
		});
        
    }
	
	
	@Override
    public boolean onTouchEvent(MotionEvent event) {
    	Log.i("TAG", "=============onTouchEvent=================");
    	//当前手指的Y值
        final int y = (int) event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_UP:
            	 first = 0;
                 prepareForRefresh();  //准备刷新
                break;
            case MotionEvent.ACTION_DOWN:
                first = y;            
                break;
            
            case MotionEvent.ACTION_MOVE:
            	//更行头视图的toppadding 属性
            	Log.i("TAG", "=============ACTION_MOVE============" + (y - first));
            	mRefreshView.setPadding(mRefreshView.getPaddingLeft(),(y - first),mRefreshView.getPaddingRight(),mRefreshView.getPaddingBottom());
                //applyHeaderPadding(event);
                break;
        }
        return super.onTouchEvent(event);
    }


    public void prepareForRefresh() {
        resetHeaderPadding();   //初始化,头部文件

        mRefreshViewImage.setVisibility(View.GONE);
        mRefreshViewImage.setImageDrawable(null);
        mRefreshViewProgress.setVisibility(View.VISIBLE);

       mRefreshViewText.setText("Jefry ");
      
    }
    
    private void resetHeaderPadding() {
        mRefreshView.setPadding(
                mRefreshView.getPaddingLeft(),
                0,
                mRefreshView.getPaddingRight(),
                mRefreshView.getPaddingBottom());
    }
    
}


由于篇幅原因,我只贴出了部分代码。如果觉得上面的代码看起来比较繁琐,需要这个Demo项目源码的同志们,就留下邮箱。

评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值