`

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

阅读更多
这个是上一篇的加强版,现在实现的软件并不是很多。其实第一个搞懂的话,这个就呼之欲出了。现全部公开源码:
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  
源码呢,亲

相关推荐

    6-1 工作表打印设置

    在页眉页脚设置中,用户可以决定这些信息显示在页面的左边、中间还是右边。如果要删除页眉页脚,则可以在同一选项卡下进行操作。 此外,Excel提供的打印预览功能也是一个非常实用的工具,它允许用户在打印前看到...

    ps网页切片要点及切片范例.pdf

    1. 拖出参考线,按 Ctrl+R 先调出标尺!为了做到精确的9 等分,我们不采用直接从标尺上拖曳的方式来建立参考线,而采用:视图》新建参考线,在对话框中准确输入参考线的位置这时可以把文档放大,以便参考线更准确。 ...

    内控制度流程图模版.doc

    4. 鼠标指向左边“职能带区”按下左键不松手,拖拉到右边页面随意位置再松手,软件能自动对齐表格。 七、流程图绘制 1. 鼠标左键双击表格最上栏&lt;流程名称&gt;,输入流程图; 2. 鼠标左键双击&lt;职能&gt;,根据流程策划表的...

    css笔记课程笔记2019,5,22

    * 是页面显示效果更加好 * CSS将网页内容和显示样式进行分离,提高了显示功能。 2、css和html的结合方式(四种结合方式) (1)在每个html标签上面都有一个属性 style,把css和html结合在一起 - (2)使用...

    vue 中基于html5 drag drap的拖放效果案例分析

    案例二展示了左右布局的拖放,允许元素从左边列表拖拽到右边的自由区域,并在右边区域中自由拖动。案例三未在给定的部分内容中详细描述,但可以推测其内容延续了拖放主题,可能涉及更复杂的布局或者更高级的拖放特性...

    iSee视频监控 v5.2

    本系统可切换D1与CIF分辨率,切换分辨率设置后,在主界面必须关 闭通道视频,重新打开通道视频,设置才能生效。 本系统主机工作于常温(-10_3℃~55_2℃)、清洁的环境,并且保持 良好的通风状态。 本系统软件属于我...

    KODExplorer 芒果云-资源管理器

    和拖文件拽权限一致 - 文件上传失败检测 - 树目录同步优化 ###ver2.5 `2014/6/15` ---- ####update: - 增加创建副本功能 按住ctrl拖拽即可,可以到当前,也可以到文件夹。 - 多选拖拽优化:剪切到、移动到某个...

    精易模块[源码] V5.15

    4、改善(字节集_取左边|取右边|取中间)与未公开子程序重复,改名为 字节集_寻找取左|字节集_寻找取右|字节集_寻找取中,并修正BUG。 5、公开子程序(字节集_到文本|字节集_到整数|字节集_取左边|字节集_取右边|...

Global site tag (gtag.js) - Google Analytics