实现自定义view(2):仿Android QQ多屏幕显示ListView的效果

本文介绍如何实现仿Android QQ主界面的分屏ListView滑动效果,包括横向切换屏幕和纵向滚动ListView。提供了工程文件下载,并探讨了ViewGroup对触摸消息的分发规则,通过重载onInterceptTouchEvent(MotionEvent ev)方法判断并处理横向滚动事件。
摘要由CSDN通过智能技术生成

转载请注明出处。博客地址:http://blog.csdn.net/mylzc

本文在《仿 UC,墨迹天气左右拖动 多屏幕显示效果》的基础上对代码进行修改,模仿Android QQ主界面的分屏ListView滑动效果。

当进行横向滑动时,会切换屏幕,当纵向滑动时,ListView会滚动。

效果图如下:



代码如下:

工程文件下载

FlingGallery.java

package com.droidful.flinggallery;

import android.content.Context;
import android.util.Log;
import android.view.GestureDetector;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Interpolator;
import android.view.animation.Transformation;
import android.widget.Adapter;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

// TODO:

// 1. In order to improve performance Cache screen bitmap and use for animation
// 2. Establish superfluous memory allocations and delay or replace with reused objects
//	  Probably need to make sure we are not allocating objects (strings, etc.) in loops

public class FlingGallery extends FrameLayout
{
	// Constants
	
	private final int swipe_min_distance = 120;
    private final int swipe_max_off_path = 250;
    private final int swipe_threshold_veloicty = 400;

    // Properties
    
	private int mViewPaddingWidth = 0;
    private int mAnimationDuration = 250;
    private float mSnapBorderRatio = 0.5f;
    private boolean mIsGalleryCircular = true;
    private int mDetectScrollX = 50;

    // Members

    private int mGalleryWidth = 0;
    private boolean mIsTouched = false;
    private boolean mIsDragging = false;
    private float mCurrentOffset = 0.0f;
    private long mScrollTimestamp = 0;
    private int mFlingDirection = 0;
    private int mCurrentPosition = 0;
    private int mCurrentViewNumber = 0;

    private Context mContext;
    private Adapter mAdapter;
    private FlingGalleryView[] mViews;
    private FlingGalleryAnimation mAnimation;
    private GestureDetector mGestureDetector;
    private FlingGestureDetectorListener mGestureDetectorListener;
    private GestureDetector mInterruptDetector;
    private Interpolator mDecelerateInterpolater;

    public FlingGallery(Context context)
	{
		super(context);

		mContext = context;
		mAdapter = null;
		
        mViews = new FlingGalleryView[3];
        mViews[0] = new FlingGalleryView(0, this);
        mViews[1] = new FlingGalleryView(1, this);
        mViews[2] = new FlingGalleryView(2, this);

		mAnimation = new FlingGalleryAnimation();
		mGestureDetectorListener = new FlingGestureDetectorListener();
		mGestureDetector = new GestureDetector(mGestureDetectorListener);
		mInterruptDetector = new GestureDetector(new InterruptGestureDetectorListener());
		mDecelerateInterpolater = AnimationUtils.loadInterpolator(mContext, android.R.anim.decelerate_interpolator);
	}

	public void setPaddingWidth(int viewPaddingWidth)
	{
		mViewPaddingWidth = viewPaddingWidth;
	}

	public void setAnimationDuration(int animationDuration)
	{
		mAnimationDuration = animationDuration;
	}
	
	public void setSnapBorderRatio(float snapBorderRatio)
	{
		mSnapBorderRatio = snapBorderRatio;
	}

	public void setIsGalleryCircular(boolean isGalleryCircular) 
	{
		if (mIsGalleryCircular != isGalleryCircular)
		{
			mIsGalleryCircular = isGalleryCircular;
	
			if (mCurrentPosition == getFirstPosition())
			{
				// We need to reload the view immediately to the left to change it to circular view or blank
		    	mViews[getPrevViewNumber(mCurrentViewNumber)].recycleView(getPrevPosition(mCurrentPosition));			
			}
	
			if (mCurrentPosition == getLastPosition())
			{
				// We need to reload the view immediately to the right to change it to circular view or blank
		    	mViews[getNextViewNumber(mCurrentViewNumber)].recycleView(getNextPosition(mCurrentPosition));			
			}
		}
	}

	public int getGalleryCount()
	{
		return (mAdapter == null) ? 0 : mAdapter.getCount();
	}

	public int getFirstPosition()
	{
		return 0;
	}
	
	@Override
	public boolean onInterceptTouchEvent (MotionEvent ev) {

		return mInterruptDetector.onTouchEvent(ev);
	}
	
	@Override
	public boolean onTouchEvent (MotionEvent ev) {
		Log.d("Test", "test" );
		boolean value = onGalleryTouchEvent(ev);
		Log.d("Test", "" + value);
		return true;
	}

	public int getLastPosition()
	{
		return (getGalleryCount() == 0) ? 0 : getGalleryCount() - 1;
	}

	private int getPrevPosition(int relativePosition)
	{
		int prevPosition = relativePosition - 1;

		if (prevPosition < getFirstPosition())
		{
			prevPosition = getFirstPosition() - 1;

			if (mIsGalleryCircular == true)
			{
				prevPosition = getLastPosition();
			}
		}

		return prevPosition;
	}

	private int getNextPosition(int relativePosition)
	{
		int nextPosition = relativePosition + 1;

		if (nextPosition > getLastPosition())
		{
			nextPosition = getLastPosition() + 1;

			if (mIsGalleryCircular == true)
			{
				nextPosition = getFirstPosition();
			}
		}

		return nextPosition;
	}

	private int getPrevViewNumber(int relativeViewNumber)
	{
		return (relativeViewNumber == 0) ? 2 : relativeViewNumber - 1;
	}

	private int getNextViewNumber(int relativeViewNumb
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值