Android抽屉之完整篇

发布时间:2025-01-14 08:11

保持抽屉整洁:抽屉内的物品要定期整理,保持整洁感。 #生活技巧# #收纳整理技巧# #厨房抽屉整理法#

  1.支持手势拖拉

  2.拖拉到一半时,可以自动展开或者收缩。

  具体如下图:

  这次就在基础篇的基础上加入拖拉功能。拖拉功能基于GestureDetector,GestureDetector的基本使用方式

  本文的源码可以到这里下载:http://download.csdn.net/detail/hellogv/3642418

  只贴出抽屉组件的源码,其他源文件与基础篇的一样:

  [java] view plaincopyprint?

  《span style=“font-family:Comic Sans MS;font-size:18px;”》public class Panel extends LinearLayout implements GestureDetector.OnGestureListener{

  public interface PanelClosedEvent {

  void onPanelClosed(View panel);

  }

  public interface PanelOpenedEvent {

  void onPanelOpened(View panel);

  }

  private final static int HANDLE_WIDTH=30;

  private final static int MOVE_WIDTH=20;

  private Button btnHandler;

  private LinearLayout panelContainer;

  private int mRightMargin=0;

  private Context mContext;

  private GestureDetector mGestureDetector;

  private boolean mIsScrolling=false;

  private float mScrollX;

  private PanelClosedEvent panelClosedEvent=null;

  private PanelOpenedEvent panelOpenedEvent=null;

  public Panel(Context context,View otherView,int width,int height) {

  super(context);

  this.mContext=context;

  //定义手势识别

  mGestureDetector = new GestureDetector(mContext,this);

  mGestureDetector.setIsLongpressEnabled(false);

  //改变Panel附近组件的属性

  LayoutParams otherLP=(LayoutParams) otherView.getLayoutParams();

  otherLP.weight=1;

  otherView.setLayoutParams(otherLP);

  //设置Panel本身的属性

  LayoutParams lp=new LayoutParams(width, height);

  lp.rightMargin=-lp.width+HANDLE_WIDTH;

  mRightMargin=Math.abs(lp.rightMargin);

  this.setLayoutParams(lp);

  this.setOrientation(LinearLayout.HORIZONTAL);

  //设置Handler的属性

  btnHandler=new Button(context);

  btnHandler.setLayoutParams(new LayoutParams(HANDLE_WIDTH,height));

  //btnHandler.setOnClickListener(handlerClickEvent);

  btnHandler.setOnTouchListener(handlerTouchEvent);

  this.addView(btnHandler);

  //设置Container的属性

  panelContainer=new LinearLayout(context);

  panelContainer.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,

  LayoutParams.FILL_PARENT));

  this.addView(panelContainer);

  }

  private View.OnTouchListener handlerTouchEvent=new View.OnTouchListener() {

  @Override

  public boolean onTouch(View v, MotionEvent event) {

  if(event.getAction()==MotionEvent.ACTION_UP && //onScroll时的ACTION_UP

  mIsScrolling==true)

  {

  LayoutParams lp=(LayoutParams) Panel.this.getLayoutParams();

  if (lp.rightMargin 》= (-mRightMargin/2)) {//往左超过一半

  new AsynMove().execute(new Integer[] { MOVE_WIDTH });// 正数展开

  }

  else if (lp.rightMargin 《 (-mRightMargin/2)) {//往右拖拉

  new AsynMove().execute(new Integer[] { -MOVE_WIDTH });// 负数收缩

  }

  }

  return mGestureDetector.onTouchEvent(event);

  }

  };

  /**

  * 定义收缩时的回调函数

  * @param event

  */

  public void setPanelClosedEvent(PanelClosedEvent event)

  {

  this.panelClosedEvent=event;

  }

  /**

  * 定义展开时的回调函数

  * @param event

  */

  public void setPanelOpenedEvent(PanelOpenedEvent event)

  {

  this.panelOpenedEvent=event;

  }

网址:Android抽屉之完整篇 https://www.yuejiaxmz.com/news/view/711611

相关内容

android左右抽屉的简单介绍
抽屉软件
Android 组件的抽屉(初级)
如何正确整理抽屉
抽屉整理看这一篇就够了
文房具社 ? 如何整理抽屉?
抽屉整理术
实用厨房最基本要素:抽屉、抽屉、大抽屉
抽屉盒是什么?抽屉式整理柜安装方法
如何整理抽屉.ppt

随便看看