(转)抽屉中关闭时默认显示一部分的内容

来源:互联网 发布:java同步方法 编辑:程序博客网 时间:2024/06/09 17:31

先说下标题的意思,因为当时问了一些人,就是这么问的,说不知道啥意思,没理解我想干什么,这个意思就是。。。抽屉难道一定要完全关着或完全开着吗?我只开一部分然后声称我这个抽屉是关着的不行么。
算了,先贴个效果图:
这个情况是抽屉关着的时候:



 
这个情况是抽屉开着的时候

这是股票的一个抽屉。 然后boss给我说还有3个,顿时纠结,幸亏把内容最为丰富的股票给先搞定了。

。。。。就是这样,一开始就想着把抽屉的手柄写大点,用一个LinearLayout布局来做手柄,但是这样一来,发现布局里面的所有的按钮都相应了我的点击事件,擦。在抽屉手把位置的左边,有个按钮,点击一下会变成减号,但是如果是这样的话,那么我只要一点手柄,他也跟着变了。寻找了一些方法,未果,终于在csdn的一个问题帖子上找到了类似的情况。
基本的思路就是,先拦截了所有抽屉手柄的触摸事件,然后在找出用户想作为手柄的控件id,以及他们的屏幕位置。当点击到那些位置的时候,再做判断。


直接上代码,比较少代码就能实现的,理解起来也不困难,当然,要熟悉Android的控件所对应的实现方法会更简单:

 

Java代码  收藏代码
  1. public class MySlidingDrawer extends SlidingDrawer {  
  2.   
  3.     private int mHandleId = 0// 抽屉行为控件ID  
  4.     private int[] mTouchableIds = null// Handle 部分其他控件ID  
  5.   
  6.     public int getHandleId() {  
  7.         return mHandleId;  
  8.     }  
  9.   
  10.     public void setHandleId(int mHandleId) {  
  11.         this.mHandleId = mHandleId;  
  12.     }  
  13.   
  14.     public int[] getTouchableIds() {  
  15.         return mTouchableIds;  
  16.     }  
  17.   
  18.     public void setTouchableIds(int[] mTouchableIds) {  
  19.         this.mTouchableIds = mTouchableIds;  
  20.     }  
  21.   
  22.     public MySlidingDrawer(Context context, AttributeSet attrs) {  
  23.         super(context, attrs);  
  24.   
  25.     }  
  26.   
  27.     public MySlidingDrawer(Context context, AttributeSet attrs, int defStyle) {  
  28.         super(context, attrs, defStyle);  
  29.   
  30.     }  
  31.   
  32.     /* 
  33.      * 获取控件的屏幕区域 
  34.      */  
  35.     public Rect getRectOnScreen(View view) {  
  36.         Rect rect = new Rect();  
  37.         int[] location = new int[2];  
  38.         View parent = view;  
  39.         if (view.getParent() instanceof View) {  
  40.             parent = (View) view.getParent();  
  41.         }  
  42.         parent.getLocationOnScreen(location);  
  43.         view.getHitRect(rect);  
  44.         rect.offset(location[0], location[1]);  
  45.         return rect;  
  46.     }  
  47.   
  48.     // 拦截触摸事件,用以修改事件  
  49.     @Override  
  50.     public boolean onInterceptTouchEvent(MotionEvent event) {  
  51.   
  52.         // 确定控件的屏幕区域  
  53.         int[] location = new int[2];  
  54.         int x = (int) event.getX();  
  55.         int y = (int) event.getY();  
  56.         this.getLocationOnScreen(location);  
  57.         x += location[0];  
  58.         y += location[1];  
  59.         // handle部分独立按钮,循环寻找非抽屉手柄的布局。  
  60.         if (mTouchableIds != null) {  
  61.             for (int id : mTouchableIds) {  
  62.                 View view = findViewById(id);  
  63.                 Rect rect = getRectOnScreen(view);  
  64.                 if (rect.contains(x, y)) {  
  65.                     Log.i("MySlidingDrawer on touch", String.format(  
  66.                             "Action=%d Button=%s", event.getAction(),  
  67.                             ((LinearLayout) view).getContext().toString()));  
  68.                     // return  
  69.                     boolean result = view.dispatchTouchEvent(event);  
  70.                     Log.i("MySlidingDrawer dispatchTouchEvent""" + result);  
  71.                     return false;  
  72.                 }  
  73.             }  
  74.         }  
  75.   
  76.         // 抽屉行为控件,本想同上,写成数组,寻找多个手柄,但是这样就没有了抽屉拖动效果  
  77.         if (event.getAction() == MotionEvent.ACTION_DOWN && mHandleId != 0) {  
  78.             View view = findViewById(mHandleId);  
  79.   
  80.             Log.i("MySlidingDrawer on touch", String.format("%d,%d", x, y));  
  81.   
  82.             Rect rect = getRectOnScreen(view);  
  83.   
  84.             Log.i("MySlidingDrawer handle screen rect", String  
  85.                     .format("%d,%d %d,%d", rect.left, rect.top, rect.right,  
  86.                             rect.bottom));  
  87.             if (rect.contains(x, y)) {// 点击抽屉控件时交由系统处理  
  88.                 Log.i("MySlidingDrawer""Hit handle");  
  89.             } else {  
  90.                 return false;  
  91.             }  
  92.         }  
  93.         return super.onInterceptTouchEvent(event);  
  94.     }  
  95.   
  96.     // 获取触屏事件  
  97.     @Override  
  98.     public boolean onTouchEvent(MotionEvent event) {  
  99.   
  100.         return super.onTouchEvent(event);  
  101.     }  

 

 大概就是这样的重写。

 

再来是布局文件:

<?xml version="1.0" encoding="UTF-8"?>

Xml代码  收藏代码
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="fill_parent" android:layout_height="fill_parent">  
  3.   
  4.   
  5.     <Button android:id="@+id/buttontest" android:layout_height="wrap_content"  
  6.         android:layout_width="wrap_content" android:text="test" />  
  7.     <!-- bottom menu -->  
  8.   
  9.   
  10.     <!-- 股票类 -->  
  11.     <com.quan.MySlidingDrawer android:id="@+id/stockDrawer"  
  12.         android:layout_width="fill_parent" android:layout_height="wrap_content"  
  13.         android:orientation="vertical" android:handle="@+id/handleStock"  
  14.         android:content="@+id/contentStock" android:visibility="visible">  
  15.   
  16.   
  17.         <!-- top view 3 rows -->  
  18.         <LinearLayout android:layout_width="fill_parent"  
  19.             android:id="@+id/handleStock" android:layout_height="85dip"  
  20.             android:orientation="vertical">  
  21.             <LinearLayout android:layout_height="wrap_content"  
  22.                 android:layout_width="wrap_content" android:layout_gravity="center">  
  23.                 <ImageButton android:layout_width="60dip" android:id="@+id/mbutton"  
  24.                     android:layout_height="10dip" android:src="@drawable/up"  
  25.                     android:layout_gravity="center" />  
  26.             </LinearLayout>  
  27.             <!-- top left -->  
  28.             <LinearLayout android:layout_height="75dip"  
  29.                 android:layout_width="fill_parent" android:orientation="horizontal"  
  30.                 android:id="@+id/otherHandler" android:background="#000">  
  31.                 <!-- top left -->  
  32.                 <RelativeLayout android:id="@+id/topLeft"  
  33.                     android:layout_width="39dip" android:layout_height="75dip"  
  34.                     android:orientation="vertical">  
  35.   
  36.                     <Button android:layout_width="30dip" android:layout_height="30dip"  
  37.                         android:id="@+id/btn_add_cut" android:layout_alignParentTop="true"  
  38.                         android:layout_alignParentLeft="true" android:layout_gravity="center_vertical"  
  39.                         android:focusable="true" android:background="@drawable/add" />  
  40.                 </RelativeLayout>  
  41.   
  42.   
  43.                 <RelativeLayout android:layout_width="120dip"  
  44.                     android:layout_height="75dip">  
  45.                     <TextView android:layout_height="50dip" android:text="2616.99"  
  46.                         android:layout_width="wrap_content"  
  47.                         android:layout_alignParentRight="true"  
  48.                         android:layout_alignParentTop="true" android:gravity="right"  
  49.                         android:id="@+id/tvCurrentPrice" android:textColor="#f00"  
  50.                         android:textSize="30dip" android:layout_gravity="center_vertical" />  
  51.   
  52.                     <TextView android:text="+22.50" android:textColor="#f00"  
  53.                         android:layout_height="wrap_content" android:layout_width="wrap_content"  
  54.                         android:layout_alignParentBottom="true"  
  55.                         android:layout_alignParentLeft="true" android:id="@+id/tvUpDown" />  
  56.   
  57.                     <TextView android:layout_height="wrap_content"  
  58.                         android:text="0.21%" android:layout_width="wrap_content"  
  59.                         android:layout_alignParentBottom="true"  
  60.                         android:layout_alignParentRight="true" android:gravity="right"  
  61.                         android:textColor="#f00" android:id="@+id/tvAmplitude" />  
  62.                 </RelativeLayout>  
  63.   
  64.   
  65.   
  66.   
  67.   
  68.   
  69.                 <View android:layout_width="1dip" android:layout_height="fill_parent"  
  70.                     android:background="#ff0000" />  
  71.   
  72.                 <!-- top right -->  
  73.                 <TableLayout android:id="@+id/topRight"  
  74.                     android:layout_width="159dip" android:layout_height="fill_parent"  
  75.                     android:orientation="vertical">  
  76.                     <TableRow>  
  77.                         <TextView android:text="委比" android:textColor="#848684"  
  78.                             android:layout_height="24dip" android:layout_width="70dip"  
  79.                             android:gravity="left" />  
  80.                         <TextView android:id="@+id/tvAppoint" android:text="2.3%"  
  81.                             android:textColor="#f00" android:layout_height="24dip"  
  82.                             android:layout_width="89dip" android:gravity="right"  
  83.                             android:paddingRight="2dip" />  
  84.                     </TableRow>  
  85.                     <View android:layout_height="1dip" android:layout_width="fill_parent"  
  86.                         android:background="#f00" />  
  87.                     <TableRow>  
  88.                         <TextView android:text="委差" android:textColor="#848684"  
  89.                             android:layout_height="24dip" android:layout_width="70dip"  
  90.                             android:gravity="left" />  
  91.                         <TextView android:id="@+id/tvPoorCom" android:text="32"  
  92.                             android:textColor="#f00" android:layout_height="24dip"  
  93.                             android:layout_width="89dip" android:gravity="right"  
  94.                             android:paddingRight="2dip" />  
  95.                     </TableRow>  
  96.                     <View android:layout_height="1dip" android:layout_width="fill_parent"  
  97.                         android:background="#f00" />  
  98.                     <TableRow>  
  99.                         <TextView android:text="量比" android:textColor="#848684"  
  100.                             android:layout_height="24dip" android:layout_width="70dip"  
  101.                             android:gravity="left" />  
  102.                         <TextView android:id="@+id/tvReQuantity" android:text="-2.45"  
  103.                             android:textColor="#0f0" android:layout_height="24dip"  
  104.                             android:layout_width="89dip" android:gravity="right"  
  105.                             android:paddingRight="2dip" />  
  106.                     </TableRow>  
  107.                 </TableLayout>  
  108.   
  109.             </LinearLayout>  
  110.   
  111.         </LinearLayout>  
  112.   
  113.   
  114.   
  115.   
  116.   
  117.   
  118.   
  119.   
  120.         <LinearLayout android:id="@+id/contentStock"  
  121.             android:layout_width="fill_parent" android:layout_height="fill_parent"  
  122.             android:orientation="vertical" android:background="#000">  
  123.   
  124.   
  125.   
  126.             <View android:layout_height="1dip" android:layout_width="fill_parent"  
  127.                 android:background="#f00" />  
  128.   
  129.             <!-- bottom rows -->  
  130.             <LinearLayout android:id="@+id/bottomRows"  
  131.                 android:layout_height="253dip" android:layout_width="fill_parent"  
  132.                 android:orientation="horizontal">  
  133.                 <!-- left table -->  
  134.                 <TableLayout android:layout_height="fill_parent"  
  135.                     android:layout_width="160dip">  
  136.                     <TableRow>  
  137.                         <TextView android:textColor="#848684"  
  138.                             android:layout_height="25dip" android:layout_width="40dip"  
  139.                             android:text="均价" android:gravity="left" />  
  140.                         <TextView android:textColor="#f00" android:layout_height="25dip"  
  141.                             android:layout_width="80dip" android:gravity="center" />  
  142.                         <TextView android:textColor="#C39C00"  
  143.                             android:layout_height="25dip" android:layout_width="40dip"  
  144.                             android:gravity="right" android:paddingRight="2dip" />  
  145.                     </TableRow>  
  146.                     <View android:layout_height="1dip" android:background="#f00" />  
  147.                     <TableRow>  
  148.                         <TextView android:textColor="#848684"  
  149.                             android:layout_height="25dip" android:layout_width="40dip"  
  150.                             android:text="总手" android:gravity="left" />  
  151.                         <TextView android:textColor="#f00" android:layout_height="25dip"  
  152.                             android:layout_width="80dip" android:gravity="center" />  
  153.                         <TextView android:textColor="#C39C00"  
  154.                             android:layout_height="25dip" android:layout_width="40dip"  
  155.                             android:gravity="right" android:paddingRight="2dip" />  
  156.                     </TableRow>  
  157.                     <TableRow>  
  158.                         <TextView android:textColor="#848684"  
  159.                             android:layout_height="25dip" android:layout_width="40dip"  
  160.                             android:text="现手" android:gravity="left" />  
  161.                         <TextView android:textColor="#f00" android:layout_height="25dip"  
  162.                             android:layout_width="80dip" android:gravity="center" />  
  163.                         <TextView android:id="@+id/CURVOL" android:textColor="#C39C00"  
  164.                             android:layout_height="25dip" android:layout_width="40dip"  
  165.                             android:gravity="right" android:paddingRight="2dip" />  
  166.                     </TableRow>  
  167.                     <TableRow>  
  168.                         <TextView android:textColor="#848684"  
  169.                             android:layout_height="25dip" android:layout_width="40dip"  
  170.                             android:text="金额" android:gravity="left" />  
  171.                         <TextView android:textColor="#f00" android:layout_height="25dip"  
  172.                             android:layout_width="80dip" android:gravity="center" />  
  173.                         <TextView android:id="@+id/MONEY" android:textColor="#C39C00"  
  174.                             android:layout_height="25dip" android:layout_width="40dip"  
  175.                             android:gravity="right" android:paddingRight="2dip" />  
  176.                     </TableRow>  
  177.                     <View android:layout_height="1dip" android:background="#f00" />  
  178.                     <TableRow>  
  179.                         <TextView android:textColor="#848684"  
  180.                             android:layout_height="25dip" android:layout_width="40dip"  
  181.                             android:text="外盘" android:gravity="left" />  
  182.                         <TextView android:textColor="#f00" android:layout_height="25dip"  
  183.                             android:layout_width="80dip" android:gravity="center" />  
  184.                         <TextView android:textColor="#C39C00"  
  185.                             android:layout_height="25dip" android:layout_width="40dip"  
  186.                             android:gravity="right" android:paddingRight="2dip" />  
  187.                     </TableRow>  
  188.                     <View android:layout_height="1dip" android:background="#f00" />  
  189.                     <TableRow>  
  190.                         <TextView android:textColor="#848684"  
  191.                             android:layout_height="25dip" android:layout_width="40dip"  
  192.                             android:text="买一" android:gravity="left" />  
  193.                         <TextView android:text="2539.82" android:textColor="#f00"  
  194.                             android:layout_height="25dip" android:layout_width="80dip"  
  195.                             android:gravity="center" />  
  196.                         <TextView android:text="23" android:textColor="#AE8B00"  
  197.                             android:layout_height="25dip" android:layout_width="40dip"  
  198.                             android:gravity="right" android:paddingRight="2dip" />  
  199.                     </TableRow>  
  200.                     <TableRow>  
  201.                         <TextView android:textColor="#848684"  
  202.                             android:layout_height="25dip" android:layout_width="40dip"  
  203.                             android:text="买二" android:gravity="left" />  
  204.                         <TextView android:textColor="#f00" android:layout_height="25dip"  
  205.                             android:layout_width="80dip" android:gravity="center" />  
  206.                         <TextView android:textColor="#C39C00"  
  207.                             android:layout_height="25dip" android:layout_width="40dip"  
  208.                             android:gravity="right" android:paddingRight="2dip" />  
  209.                     </TableRow>  
  210.                     <TableRow>  
  211.                         <TextView android:textColor="#848684"  
  212.                             android:layout_height="25dip" android:layout_width="40dip"  
  213.                             android:text="买三" android:gravity="left" />  
  214.                         <TextView android:textColor="#f00" android:layout_height="25dip"  
  215.                             android:layout_width="80dip" android:gravity="center" />  
  216.                         <TextView android:textColor="#C39C00"  
  217.                             android:layout_height="25dip" android:layout_width="40dip"  
  218.                             android:gravity="right" android:paddingRight="2dip" />  
  219.                     </TableRow>  
  220.                     <TableRow>  
  221.                         <TextView android:textColor="#848684"  
  222.                             android:layout_height="25dip" android:layout_width="40dip"  
  223.                             android:text="买四" android:gravity="left" />  
  224.                         <TextView android:textColor="#f00" android:layout_height="25dip"  
  225.                             android:layout_width="80dip" android:gravity="center" />  
  226.                         <TextView android:textColor="#C39C00"  
  227.                             android:layout_height="25dip" android:layout_width="40dip"  
  228.                             android:gravity="right" android:paddingRight="2dip" />  
  229.                     </TableRow>  
  230.                     <TableRow>  
  231.                         <TextView android:textColor="#848684"  
  232.                             android:layout_height="25dip" android:layout_width="40dip"  
  233.                             android:text="买五" android:gravity="left" />  
  234.                         <TextView android:textColor="#f00" android:layout_height="25dip"  
  235.                             android:layout_width="80dip" android:gravity="center" />  
  236.                         <TextView android:textColor="#C39C00"  
  237.                             android:layout_height="25dip" android:layout_width="40dip"  
  238.                             android:gravity="right" android:paddingRight="2dip" />  
  239.                     </TableRow>  
  240.                 </TableLayout>  
  241.   
  242.                 <!-- right table -->  
  243.                 <TableLayout android:layout_height="fill_parent"  
  244.                     android:layout_width="160dip">  
  245.                     <TableRow>  
  246.                         <TextView android:textColor="#848684"  
  247.                             android:layout_height="25dip" android:layout_width="40dip"  
  248.                             android:text="昨收" android:gravity="left" />  
  249.                         <TextView android:textColor="#f00" android:layout_height="25dip"  
  250.                             android:layout_width="80dip" android:gravity="center" />  
  251.                         <TextView android:textColor="#C39C00"  
  252.                             android:layout_height="25dip" android:layout_width="40dip"  
  253.                             android:gravity="right" />  
  254.                     </TableRow>  
  255.                     <View android:layout_height="1dip" android:background="#f00" />  
  256.                     <TableRow>  
  257.                         <TextView android:textColor="#848684"  
  258.                             android:layout_height="25dip" android:layout_width="40dip"  
  259.                             android:text="开盘" android:gravity="left" />  
  260.                         <TextView android:textColor="#f00" android:layout_height="25dip"  
  261.                             android:layout_width="80dip" android:gravity="center" />  
  262.                         <TextView android:id="@+id/OPEN" android:textColor="#C39C00"  
  263.                             android:layout_height="25dip" android:layout_width="40dip"  
  264.                             android:gravity="right" />  
  265.                     </TableRow>  
  266.                     <TableRow>  
  267.                         <TextView android:textColor="#848684"  
  268.                             android:layout_height="25dip" android:layout_width="40dip"  
  269.                             android:text="最高" android:gravity="left" />  
  270.                         <TextView android:textColor="#f00" android:layout_height="25dip"  
  271.                             android:layout_width="80dip" android:gravity="center" />  
  272.                         <TextView android:id="@+id/MAX" android:textColor="#C39C00"  
  273.                             android:layout_height="25dip" android:layout_width="40dip"  
  274.                             android:gravity="right" />  
  275.                     </TableRow>  
  276.                     <TableRow>  
  277.                         <TextView android:textColor="#848684"  
  278.                             android:layout_height="25dip" android:layout_width="40dip"  
  279.                             android:text="最低" android:gravity="left" />  
  280.                         <TextView android:textColor="#f00" android:layout_height="25dip"  
  281.                             android:layout_width="80dip" android:gravity="center" />  
  282.                         <TextView android:id="@+id/MIN" android:textColor="#C39C00"  
  283.                             android:layout_height="25dip" android:layout_width="40dip"  
  284.                             android:gravity="right" />  
  285.                     </TableRow>  
  286.                     <View android:layout_height="1dip" android:background="#f00" />  
  287.                     <TableRow>  
  288.                         <TextView android:textColor="#848684"  
  289.                             android:layout_height="25dip" android:layout_width="40dip"  
  290.                             android:text="内盘" android:gravity="left" />  
  291.                         <TextView android:textColor="#f00" android:layout_height="25dip"  
  292.                             android:layout_width="80dip" android:gravity="center" />  
  293.                         <TextView android:id="@+id/INNER_TRAY" android:textColor="#C39C00"  
  294.                             android:layout_height="25dip" android:layout_width="40dip"  
  295.                             android:gravity="right" />  
  296.                     </TableRow>  
  297.                     <View android:layout_height="1dip" android:background="#f00" />  
  298.                     <TableRow>  
  299.                         <TextView android:textColor="#848684"  
  300.                             android:layout_height="25dip" android:layout_width="40dip"  
  301.                             android:text="卖一" android:gravity="left" />  
  302.                         <TextView android:text="2549.82" android:textColor="#f00"  
  303.                             android:layout_height="25dip" android:layout_width="80dip"  
  304.                             android:gravity="center" />  
  305.                         <TextView android:text="45" android:textColor="#C39C00"  
  306.                             android:layout_height="25dip" android:layout_width="40dip"  
  307.                             android:gravity="right" />  
  308.                     </TableRow>  
  309.                     <TableRow>  
  310.                         <TextView android:textColor="#848684"  
  311.                             android:layout_height="25dip" android:layout_width="40dip"  
  312.                             android:text="卖二" android:gravity="left" />  
  313.                         <TextView android:textColor="#f00" android:layout_height="25dip"  
  314.                             android:layout_width="80dip" android:gravity="center" />  
  315.                         <TextView android:textColor="#C39C00"  
  316.                             android:layout_height="25dip" android:layout_width="40dip"  
  317.                             android:gravity="right" />  
  318.                     </TableRow>  
  319.                     <TableRow>  
  320.                         <TextView android:textColor="#848684"  
  321.                             android:layout_height="25dip" android:layout_width="40dip"  
  322.                             android:text="卖三" android:gravity="left" />  
  323.                         <TextView android:textColor="#f00" android:layout_height="25dip"  
  324.                             android:layout_width="80dip" android:gravity="center" />  
  325.                         <TextView android:textColor="#C39C00"  
  326.                             android:layout_height="25dip" android:layout_width="40dip"  
  327.                             android:gravity="right" />  
  328.                     </TableRow>  
  329.                     <TableRow>  
  330.                         <TextView android:textColor="#848684"  
  331.                             android:layout_height="25dip" android:layout_width="40dip"  
  332.                             android:text="卖四" android:gravity="left" />  
  333.                         <TextView android:textColor="#f00" android:layout_height="25dip"  
  334.                             android:layout_width="80dip" android:gravity="center" />  
  335.                         <TextView android:textColor="#C39C00"  
  336.                             android:layout_height="25dip" android:layout_width="40dip"  
  337.                             android:gravity="right" />  
  338.                     </TableRow>  
  339.                     <TableRow>  
  340.                         <TextView android:textColor="#848684"  
  341.                             android:layout_height="25dip" android:layout_width="40dip"  
  342.                             android:text="卖五" android:gravity="left" />  
  343.                         <TextView android:textColor="#f00" android:layout_height="25dip"  
  344.                             android:layout_width="80dip" android:gravity="center" />  
  345.                         <TextView android:textColor="#AE8B00"  
  346.                             android:layout_height="25dip" android:layout_width="40dip"  
  347.                             android:gravity="right" />  
  348.                     </TableRow>  
  349.                 </TableLayout>  
  350.             </LinearLayout>  
  351.         </LinearLayout>  
  352.     </com.quan.MySlidingDrawer>  
  353.   
  354.   
  355. </RelativeLayout>   

         做完这些之后又把那三个布局写完,发现。。。你妹的,原来的布局是用一个相对布局来控制抽屉的拉伸长度。其他3个内容都没这么多,所以3个的下面都是空的,又搞笑呢。接着寻找抽屉适配内容进行长度控制。
   又是重写,烦躁的很啊。直接代码,主要思想就是先测量里面内容的高度。因为抽屉本身是没有写wrap_content(即适配内容高度来进行拉伸高度控制)的,所以重写的应该是这个部分。在一个英文的编程疑难解答上找到了答案。
           onMeasure是测量控件宽高的方法。setMeasuredDimension是设置到布局里~~~

 

Java代码  收藏代码
  1. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  2.         int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);  
  3.         int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);  
  4.         int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);  
  5.         int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);  
  6.         if (widthSpecMode == MeasureSpec.UNSPECIFIED  
  7.                 || heightSpecMode == MeasureSpec.UNSPECIFIED) {  
  8.             throw new RuntimeException(  
  9.                     "SlidingDrawer cannot have UNSPECIFIED dimensions");  
  10.         }  
  11.         final View handle = getHandle();  
  12.         final View content = getContent();  
  13.         measureChild(handle, widthMeasureSpec, heightMeasureSpec);  
  14.         int extra = handle.getHeight() / 6;  
  15.         System.out.println(handle.getMeasuredHeight() + "        "  
  16.                 + content.getHeight());  
  17.         if (mVertical) {  
  18.             int height = heightSpecSize - handle.getMeasuredHeight()/* + extra */  
  19.                     - mTopOffset;  
  20.             content.measure(widthMeasureSpec,  
  21.                     MeasureSpec.makeMeasureSpec(height, heightSpecMode));  
  22.             heightSpecSize = handle.getMeasuredHeight() /* + extra */  
  23.                     + mTopOffset + content.getMeasuredHeight();  
  24.             widthSpecSize = content.getMeasuredWidth();  
  25.             if (handle.getMeasuredWidth() > widthSpecSize)  
  26.                 widthSpecSize = handle.getMeasuredWidth();  
  27.         }  
  28.         setMeasuredDimension(widthSpecSize, heightSpecSize);  
  29.     }  
  30.     private boolean mVertical;  
  31.     private int mTopOffset;  

  直接把这段copy 到需要重写的抽屉类中,测试,搞定。

 

            但是发现左边的那个图标又不能点击了,变成了触摸,就是有一定位移才能改变他的变量。
经过多番努力,改了一下,把那个按钮放在一个布局里面再对布局进行点击监听,一切搞定~~

 

 

测试这个抽屉用的activity:

 

Java代码  收藏代码
  1. public class MainActivity extends Activity {  
  2.     private MySlidingDrawer mdrawer;// 抽屉  
  3.     private ImageButton mbutton;// 抽屉拉动按钮  
  4.     private LinearLayout otherHandler;  
  5.     private LinearLayout bottomRows;// 抽屉真正的内容  
  6.     private Button test;  
  7.     private TextView tv_top;  
  8.     private Button btn_add_cut;// 真实抽屉左上角按钮  
  9.     int i = 1;// 定义成员变量,协助点击左上角按钮时改变图标,点击+变成2,点击-变成1  
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  14.         setContentView(R.layout.main);  
  15.         findview();  
  16.         addlistener();  
  17.     }  
  18.     /** 初始化组件 */  
  19.     private void findview() {  
  20.         tv_top = (TextView) this.findViewById(R.id.tvCurrentPrice);  
  21.         test = (Button) this.findViewById(R.id.buttontest);  
  22.         mdrawer = (MySlidingDrawer) findViewById(R.id.stockDrawer);  
  23.         // 传入抽屉手柄的id  
  24.         mdrawer.setHandleId(R.id.mbutton);  
  25.         mdrawer.setTouchableIds(new int[] { R.id.otherHandler });  
  26.         mbutton = (ImageButton) findViewById(R.id.mbutton);  
  27.         otherHandler = (LinearLayout) this.findViewById(R.id.otherHandler);  
  28.         bottomRows = (LinearLayout) findViewById(R.id.bottomRows);  
  29.         btn_add_cut = (Button) findViewById(R.id.btn_add_cut);  
  30.     }  
  31.     /** 
  32.      * 给各个组建添加事件监听 
  33.      */  
  34.     private void addlistener() {  
  35.         test.setOnClickListener(new OnClickListener() {  
  36.             @Override  
  37.             public void onClick(View v) {  
  38.                 tv_top.setText("8585");  
  39.             }  
  40.         });  
  41.         // 抽屉打开  
  42.         mdrawer.setOnDrawerOpenListener(new MySlidingDrawer.OnDrawerOpenListener() {  
  43.             public void onDrawerOpened() {  
  44.                 mbutton.setImageResource(R.drawable.down);  
  45.             }  
  46.         });  
  47.         // 抽屉关闭  
  48.         mdrawer.setOnDrawerCloseListener(new MySlidingDrawer.OnDrawerCloseListener() {  
  49.             public void onDrawerClosed() {  
  50.                 mbutton.setImageResource(R.drawable.up);  
  51.             }  
  52.         });  
  53.         // 抽屉正在拉动或停止拉动  
  54.         mdrawer.setOnDrawerScrollListener(new MySlidingDrawer.OnDrawerScrollListener() {  
  55.             public void onScrollStarted() {  
  56.             }  
  57.             public void onScrollEnded() {  
  58.             }  
  59.         });  
  60.         btn_add_cut.setOnClickListener(new ButtonAddCutListener());  
  61.         //  
  62.         // 真实抽屉,非头部部分,在打开状态点击时关闭抽屉  
  63.         bottomRows.setOnClickListener(new OnClickListener() {  
  64.             @Override  
  65.             public void onClick(View v) {  
  66.                 if (mdrawer.isOpened()) {  
  67.                     mdrawer.animateClose();  
  68.                 }  
  69.             }  
  70.         });  
  71.         // otherHandler.setOnClickListener(new ButtonAddCutListener());  
  72.     }  
  73.     /** 抽屉左上角按钮点击改变图片事件 */  
  74.     private class ButtonAddCutListener implements OnClickListener {  
  75.         @Override  
  76.         public void onClick(View v) {  
  77.             switch (i) {  
  78.             case 1:  
  79.                 btn_add_cut.setBackgroundDrawable(getResources().getDrawable(  
  80.                         R.drawable.cut));  
  81.                 // btn_add_cut_on.setImageResource(R.drawable.cut);  
  82.                 i = 2;  
  83.                 System.out.println("+");  
  84.                 break;  
  85.             case 2:  
  86.                 btn_add_cut.setBackgroundDrawable(getResources().getDrawable(  
  87.                         R.drawable.add));  
  88.                 // btn_add_cut_on.setImageResource(R.drawable.add);  
  89.                 i = 1;  
  90.                 System.out.println("-");  
  91.                 break;  
  92.             }  
  93.         }  
  94.     }  
  95. }  

 要有会的高手帮我看看哪里写的不对哇。谢谢了。。。。。我是纯新手。





 

原创粉丝点击