左边和右边都可拖出页面的效果
来源:互联网 发布:mac系统安装字体 编辑:程序博客网 时间:2024/05/10 23:27
文章出处:http://gundumw100.iteye.com/blog/1874003
这个是上一篇的加强版,现在实现的软件并不是很多。其实第一个搞懂的话,这个就呼之欲出了。现全部公开源码:
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(); }@Overrideprotected void onResume() {// TODO Auto-generated method stubsuper.onResume();initValues();}@Overrideprotected void updateViews(Object o) {// TODO Auto-generated method stub}@Overrideprotected void initViews() {// TODO Auto-generated method stubwidth=(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(){@Overridepublic boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stubdoSlideWhenTouchUp(event);mGestureDetector.onTouchEvent(event);return false;} }); listViewMid.setOnItemClickListener(new ListView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view,int position, long id) {// TODO Auto-generated method stubLog.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(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch (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;}}};@Overrideprotected void initValues() {// TODO Auto-generated method stub}@Overrideprotected 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(){@Overridepublic void onAnimationStart(Animation animation) {// TODO Auto-generated method stubpanel_left.setVisibility(View.VISIBLE);panel_right.setVisibility(View.GONE);}@Overridepublic void onAnimationEnd(Animation animation) {// TODO Auto-generated method stubresetMidLayout(SlideBothSideActivity.this.width,0); isShowingLeft=true;}@Overridepublic 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(){@Overridepublic void onAnimationStart(Animation animation) {// TODO Auto-generated method stub}@Overridepublic void onAnimationEnd(Animation animation) {// TODO Auto-generated method stubresetMidLayout(0,0);isShowingLeft=false;}@Overridepublic 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(){@Overridepublic void onAnimationStart(Animation animation) {// TODO Auto-generated method stubpanel_right.setVisibility(View.VISIBLE);panel_left.setVisibility(View.GONE);}@Overridepublic void onAnimationEnd(Animation animation) {// TODO Auto-generated method stubresetMidLayout(-SlideBothSideActivity.this.width,0);isShowingRight=true;}@Overridepublic 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(){@Overridepublic void onAnimationStart(Animation animation) {// TODO Auto-generated method stub}@Overridepublic void onAnimationEnd(Animation animation) {// TODO Auto-generated method stubresetMidLayout(0,0);isShowingRight=false;}@Overridepublic void onAnimationRepeat(Animation animation) {// TODO Auto-generated method stub}});}@Overridepublic 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;}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn list.size();}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn list.get(position);}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubif (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;}}@Overridepublic boolean onDown(MotionEvent e) {// TODO Auto-generated method stubreturn true;}@Overridepublic void onShowPress(MotionEvent e) {// TODO Auto-generated method stub}@Overridepublic boolean onSingleTapUp(MotionEvent e) {// TODO Auto-generated method stubreturn false;}private int mScrollx;@Overridepublic 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;}@Overridepublic void onLongPress(MotionEvent e) {// TODO Auto-generated method stub}@Overridepublic 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;}@Overridepublic boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stubdoSlideWhenTouchUp(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&¶ms.x>=0){doSlideCloseLeftAnimation(panel_mid, params.x);}else if(params.x <= -scrollDistance){//往左doSlideOpenRightAnimation(panel_mid, width+params.x);}else if(params.x > -scrollDistance&¶ms.x<0){doSlideCloseRightAnimation(panel_mid, -params.x);}}else if(isShowingLeft&&!isShowingRight){//左边的打开着if (params.x >= width-scrollDistance) {//往右拖没超过scorllDistancedoSlideOpenLeftAnimation(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)) {//往左拖没超过scorllDistancedoSlideOpenRightAnimation(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>
- 左边和右边都可拖出页面的效果
- 左边和右边都可拖出页面的效果
- 左边可拖出菜单(页面)效果
- 基于Ajax+div的“左边菜单、右边内容”页面效果实现
- TreeView+Iframe构造左边目录右边详细页面效果
- 自定义ScrollView实现可拖出边界的效果
- IOS UIScrollView 翻页,并且露出左边视图和右边视图的边缘效果。
- 横向滑动ViewGoup(左边菜单右边内容)效果的实现
- 右边可推出的效果(有图)
- 左边的数都小于等于它,右边的数都大于等于它
- 在数组中 找左边都比其小右边都比其大的元素
- 获取光标处左边和右边的数据~
- html控制左边图右边标题效果
- 为什么一个按钮在左边,其余的按钮都在右边?
- 左边,右边
- 93 求数组,左边的数都小于等于它,右边的数都大于等于它
- 面试--求数组,左边的数都小于等于它,右边的数都大于等于它
- android仿人人网右边可推出的效果
- Removing and adding DataNodes in cluster by hadoop
- 互联网产品设计师如何设计推出一款新的产品
- 【算法导论】二叉树的深度优先遍历
- 微信自定义菜单开发报错
- Reset distribution tree
- 左边和右边都可拖出页面的效果
- 《大话数据结构》简体中文版勘误
- mysql增量备份
- 【CPU经常超频工作对CPU的损害】
- Oracle 使用exp&imp备份恢复数据库空问题
- hdu 1011 树形DP
- json数据为空
- (二十二)访问者模式详解(伪动态双分派)
- 已知不错的功能模块开放平台