`

左边和右边都可拖出页面的效果

阅读更多
这个是上一篇的加强版,现在实现的软件并不是很多。其实第一个搞懂的话,这个就呼之欲出了。现全部公开源码:
import java.util.ArrayList;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.GestureDetector.OnGestureListener;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.view.animation.Animation.AnimationListener;
import android.widget.AbsoluteLayout;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class SlideBothSideActivity extends BaseActivity implements OnTouchListener,OnGestureListener{
	private Context context;
	private Button btn_left,btn_right;
	private ViewGroup panel_left,panel_right,panel_mid;
	private ListView listViewLeft,listViewRight,listViewMid;
	private final int duration=400;
	private int width=400;//滑动的距离
	private boolean isShowingLeft=false;
	private boolean isShowingRight=false;
	
	private GestureDetector mGestureDetector;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_slide_both_side);
        context=this;
        initViews();
    }

	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		initValues();
	}

	@Override
	protected void updateViews(Object o) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void initViews() {
		// TODO Auto-generated method stub
		width=(int)(getResources().getDisplayMetrics().widthPixels*0.8);
		Log.i("tag", "width="+width);
		btn_left=(Button)findViewById(R.id.btn_left);
		btn_right=(Button)findViewById(R.id.btn_right);
		btn_left.setOnClickListener(onClickListener);
		btn_right.setOnClickListener(onClickListener);
		
		//定义手势识别  
		mGestureDetector = new GestureDetector(this,this);  
		mGestureDetector.setIsLongpressEnabled(false);
		
		panel_left=(ViewGroup)findViewById(R.id.panel_left);
		panel_right=(ViewGroup)findViewById(R.id.panel_right);
		panel_mid=(ViewGroup)findViewById(R.id.panel_mid);
		panel_mid.setOnTouchListener(this);
		listViewLeft=(ListView)findViewById(R.id.listViewLeft);
		listViewRight=(ListView)findViewById(R.id.listViewRight);
		listViewMid=(ListView)findViewById(R.id.listViewMid);
		ArrayList<String> texts=new ArrayList<String>();
  		texts.add("111");
  		texts.add("222");
  		texts.add("333");
  		texts.add("444");
  		texts.add("555");
  		texts.add("666");
  		texts.add("777");
  		ListViewAdapter adapterMid=new ListViewAdapter(texts);
  		listViewMid.setAdapter(adapterMid);
  		/**让ListView不拦截手势滑动*/
  		listViewMid.setOnTouchListener(new View.OnTouchListener(){

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				doSlideWhenTouchUp(event);
				mGestureDetector.onTouchEvent(event);
				return false;
			}
  			
  		});
  		listViewMid.setOnItemClickListener(new ListView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Log.i("tag", "position=="+position);
			}
		});
  		
  		ListViewAdapter adapterLeft=new ListViewAdapter(texts);
  		listViewLeft.setAdapter(adapterLeft);
  		ListViewAdapter adapterRight=new ListViewAdapter(texts);
  		listViewRight.setAdapter(adapterRight);
	}

	View.OnClickListener onClickListener=new View.OnClickListener(){

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.btn_left:
				if(isShowingLeft){
					doSlideCloseLeftAnimation(panel_mid,width);
				}else{
					doSlideOpenLeftAnimation(panel_mid,width);
				}
				break;
			case R.id.btn_right:
				if(isShowingRight){
					doSlideCloseRightAnimation(panel_mid,width);
				}else{
					doSlideOpenRightAnimation(panel_mid,width);
				}
				break;

			default:
				break;
			}
		}
		
	};
	
	@Override
	protected void initValues() {
		// TODO Auto-generated method stub
	}

	@Override
	protected void initHandler() {
		// TODO Auto-generated method stub

	}
	
	
	private void doSlideOpenLeftAnimation(View v,int width){
		TranslateAnimation animation = new TranslateAnimation(0, width, 0, 0);
		animation.setInterpolator(new LinearInterpolator());
		animation.setDuration(duration);
		animation.setFillAfter(true);
		v.startAnimation(animation);
		animation.setAnimationListener(new AnimationListener(){

			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub
				panel_left.setVisibility(View.VISIBLE);
				panel_right.setVisibility(View.GONE);
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				// TODO Auto-generated method stub
				resetMidLayout(SlideBothSideActivity.this.width,0);
                isShowingLeft=true;
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
		});
	}
	private void doSlideCloseLeftAnimation(View v,int width){
		TranslateAnimation animation = new TranslateAnimation(0, -width, 0, 0);
		animation.setInterpolator(new LinearInterpolator());
		animation.setDuration(duration);
		animation.setFillAfter(true);
		v.startAnimation(animation);
		animation.setAnimationListener(new AnimationListener(){

			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				// TODO Auto-generated method stub
				resetMidLayout(0,0);
				isShowingLeft=false;
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
		});
	}
	
	private void resetMidLayout(int width,int height){
		AbsoluteLayout.LayoutParams params = (AbsoluteLayout.LayoutParams)panel_mid.getLayoutParams();
		params.x=width;
		params.y=height;
		panel_mid.setLayoutParams(params);
		panel_mid.clearAnimation();
	}
	
	private void doSlideOpenRightAnimation(View v,final int width){
		TranslateAnimation animation = new TranslateAnimation(0, -width, 0, 0);
		animation.setInterpolator(new LinearInterpolator());
		animation.setDuration(duration);
		animation.setFillAfter(true);
		v.startAnimation(animation);
		animation.setAnimationListener(new AnimationListener(){

			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub
				panel_right.setVisibility(View.VISIBLE);
				panel_left.setVisibility(View.GONE);
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				// TODO Auto-generated method stub
				resetMidLayout(-SlideBothSideActivity.this.width,0);
				isShowingRight=true;
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
		});
	}
	private void doSlideCloseRightAnimation(View v,final int width){
		TranslateAnimation animation = new TranslateAnimation(0, width, 0, 0);
		animation.setInterpolator(new LinearInterpolator());
		animation.setDuration(duration);
		animation.setFillAfter(true);
		v.startAnimation(animation);
		animation.setAnimationListener(new AnimationListener(){
			
			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationEnd(Animation animation) {
				// TODO Auto-generated method stub
				resetMidLayout(0,0);
				isShowingRight=false;
			}
			
			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
		});
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return super.onKeyDown(keyCode, event);
	}
	
	 
	class ListViewAdapter extends BaseAdapter{

		private ArrayList<String> list;
		public ListViewAdapter(ArrayList<String> list){
			this.list=list;
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			if (convertView == null) {
				convertView = LayoutInflater.from(context).inflate(
						R.layout.simple_item_1_for_listview, null);
			}
			TextView tv0=(TextView)convertView.findViewById(R.id.simple_item_0);
			tv0.setText(list.get(position));
			return convertView;
		}
		
	}


	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return true;
	}


	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	private int mScrollx;
	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
//		Log.i("tag", "distanceX="+distanceX);
		mScrollx -= distanceX;//distanceX:向左为正,右为负
		AbsoluteLayout.LayoutParams params = (AbsoluteLayout.LayoutParams)panel_mid.getLayoutParams();
		params.x+=mScrollx;
		/**判断显示哪个部分*/
		if(params.x>=0){
			panel_left.setVisibility(View.VISIBLE);
			panel_right.setVisibility(View.GONE);
		}else if(params.x<0){
			panel_right.setVisibility(View.VISIBLE);
			panel_left.setVisibility(View.GONE);
		}
//		else if(params.x==0){
//			panel_left.setVisibility(View.GONE);
//			panel_right.setVisibility(View.GONE);
//		}
		/**边界判断*/
		if(params.x>width){//往右拖
			params.x=width;
		}
		if(params.x<-width){//往左拖
			params.x=-width;
		}
		panel_mid.setLayoutParams(params);
		/**完成后触发onTouch函数*/
		return false;
	}


	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		// TODO Auto-generated method stub
		/** 手势快速滑动,打开/关闭panel。现已被onScroll()完成同样的功能 */
		/*if(!isShowingLeft&&!isShowingRight){//都没打开
			if(velocityX>500&&panel_mid.getLeft()==0){
				doSlideOpenLeftAnimation(panel_mid, width);
			}else if(velocityX<-500&&panel_mid.getLeft()==0){
				doSlideOpenRightAnimation(panel_mid, width);
			}
		}else if(isShowingLeft&&!isShowingRight){//左边的打开着
			if(velocityX<-500&&panel_mid.getLeft()==width){
				doSlideCloseLeftAnimation(panel_mid, width);
			}
		}else if(!isShowingLeft&&isShowingRight){//右边的打开着
			if(velocityX>500&&panel_mid.getLeft()==-width){
				doSlideCloseRightAnimation(panel_mid, width);
			}
		}*/
		return false;
	}


	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		doSlideWhenTouchUp(event);
		return mGestureDetector.onTouchEvent(event);
	}
	/**当手指离开时执行响应的动画。如果使用AdapterView需要也让该函数响应。*/
	private void doSlideWhenTouchUp(MotionEvent event){
		switch (event.getAction()) {
		case MotionEvent.ACTION_UP:
			int scrollDistance=width>>3;//拖动的距离值,可根据实际效果调节
			AbsoluteLayout.LayoutParams params = (AbsoluteLayout.LayoutParams)panel_mid.getLayoutParams();
			if(!isShowingLeft&&!isShowingRight){//都没打开
				/** 手势拖动到一 半松手时,根据拖动的距离判断打开/关闭 */
				if (params.x >= scrollDistance) {//往右
					doSlideOpenLeftAnimation(panel_mid, width-params.x);
				}else if(params.x < scrollDistance&&params.x>=0){
					doSlideCloseLeftAnimation(panel_mid, params.x);
				}
				else if(params.x <= -scrollDistance){//往左
					doSlideOpenRightAnimation(panel_mid, width+params.x);
				}else if(params.x > -scrollDistance&&params.x<0){
					doSlideCloseRightAnimation(panel_mid, -params.x);
				}
			}else if(isShowingLeft&&!isShowingRight){//左边的打开着
				if (params.x >= width-scrollDistance) {//往右拖没超过scorllDistance
					doSlideOpenLeftAnimation(panel_mid, width-params.x);
				}else if(params.x < width-scrollDistance){
					doSlideCloseLeftAnimation(panel_mid, params.x);
				}
			}else if(!isShowingLeft&&isShowingRight){//右边的打开着
				if (params.x <= -(width-scrollDistance)) {//往左拖没超过scorllDistance
					doSlideOpenRightAnimation(panel_mid, width+params.x);
				}else if(params.x > -(width-scrollDistance)){
					doSlideCloseRightAnimation(panel_mid, -params.x);
				}
			}
		default:
			break;
		}
	}
	
	
}

布局:
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    >

    <LinearLayout
        android:id="@+id/panel_left"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" 
        android:layout_x="0dp"
        android:layout_y="0dp"
        android:visibility="gone"
        android:background="@color/bg_blue_1"
        >
        <ListView 
	    android:id="@+id/listViewLeft"
	    android:layout_width="fill_parent"
    	android:layout_height="fill_parent" 
    	android:cacheColorHint="@android:color/transparent"
	    />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/panel_right"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" 
        android:layout_x="0dp"
        android:layout_y="0dp"
        android:visibility="gone"
        android:background="@color/bg_blue_3"
        >
        <ListView 
	    android:id="@+id/listViewRight"
	    android:layout_width="fill_parent"
    	android:layout_height="fill_parent" 
    	android:cacheColorHint="@android:color/transparent"
	    />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/panel_mid"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" 
        android:background="@color/green"
        android:layout_x="0dp"
        android:layout_y="0dp"
        android:visibility="visible"
        >
        <RelativeLayout 
            android:layout_width="fill_parent"
        	android:layout_height="wrap_content"
            >
        <Button
            android:id="@+id/btn_left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="left" 
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            />
        <Button
            android:id="@+id/btn_right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="right" 
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            />
            
        </RelativeLayout>

        <ListView
            android:id="@+id/listViewMid"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:cacheColorHint="@android:color/transparent" />
    </LinearLayout>

</AbsoluteLayout>


最近又找到一个:
http://blog.csdn.net/lmj623565791/article/details/39670935

最后,google帮我们实现了:
http://blog.csdn.net/jjwwmlp456/article/details/38682637
分享到:
评论
3 楼 sada_022 2014-07-08  
亲, 有图才有真相啊!
2 楼 gundumw100 2013-05-27  
375809600 写道
源码呢,亲

只要将BaseActivity改成Activity,然后将其中带有@Override的方法去掉即可
1 楼 375809600 2013-05-27  
源码呢,亲

相关推荐

Global site tag (gtag.js) - Google Analytics