本帖最后由 voteon83 于 2011-07-29 17:23:21 编辑

解决方案 »

  1.   

    问题补充:不能把整行作为handle,那样只要触摸或者点击该范围,甚至是点击两侧按钮也是拉开或者关闭抽屉的操作了,两侧按钮不能施加自己的事件了。
      

  2.   

    你可以在SlidingDrawer布局中添加左右两个按钮啊
      

  3.   


    现在已经是左中右三个按钮,但是中间的如果作为handle的话,就会把两侧的撑出去
      

  4.   

    <TableLayout  android:id = "@+id/handle">
    <TableRow />
    <TableRow />
    <TableRow />
    </TableLayout>
    试试这种布局 ,TableRow依次放你的图片就行
      

  5.   

    首先,谢谢这位兄弟!
     @+id/handle 不能放在整行上面,这样点击或者触摸handle的时候两边的按钮也会受影响。
    但是如果放在了中间一列,又会出现我说的问题——左右两列没了!
      

  6.   

    自定义一个SlidingDrawer,自己处理onTouchpublic class MySlidingDrawer extends SlidingDrawer{
    private int mHandleId = 0; //抽屉行为控件ID
    private int[] mTouchableIds = null;    //Handle 部分其他控件ID

    public int[] getTouchableIds() {
    return mTouchableIds;
    } public void setTouchableIds(int[] mTouchableIds) {
    this.mTouchableIds = mTouchableIds;
    } public int getHandleId() {
    return mHandleId;
    } public void setHandleId(int mHandleId) {
    this.mHandleId = mHandleId;
    } public MySlidingDrawer(Context context, AttributeSet attrs) {
    super(context, attrs);
    }

        public MySlidingDrawer(Context context, AttributeSet attrs, int defStyle){
         super(context, attrs, defStyle);
        }
        
        /*
         * 获取控件的屏幕区域
         */
        public Rect getRectOnScreen(View view){
    Rect rect = new Rect();
    int[] location = new int[2];
    View parent = view;
    if(view.getParent() instanceof View){
    parent = (View)view.getParent();
    }
    parent.getLocationOnScreen(location);
    view.getHitRect(rect);
    rect.offset(location[0], location[1]);

    return rect;
        }
        
        public boolean onInterceptTouchEvent(MotionEvent event) {
         // 触摸位置转换为屏幕坐标
    int[] location = new int[2];
    int x = (int)event.getX();
    int y = (int)event.getY();
    this.getLocationOnScreen(location);
    x += location[0];
    y += location[1];

         // handle部分独立按钮
         if(mTouchableIds != null){
         for(int id : mTouchableIds){
         View view = findViewById(id);
         Rect rect = getRectOnScreen(view);
         if(rect.contains(x,y)){
              Log.i("MySlidingDrawer on touch"
              , String.format("Action=%d Button=%s"
              , event.getAction()
              , ((Button)view).getText().toString()
              ));
              //return 
              boolean result = view.dispatchTouchEvent(event);
              Log.i("MySlidingDrawer dispatchTouchEvent", "" + result);
         return false;
         }
         }
         }
        
         // 抽屉行为控件
         if(event.getAction() == MotionEvent.ACTION_DOWN && mHandleId != 0){
             View view = findViewById(mHandleId);
         Log.i("MySlidingDrawer on touch"
         , String.format("%d,%d", x, y));
    Rect rect = getRectOnScreen(view);
         Log.i("MySlidingDrawer handle screen rect"
         , String.format("%d,%d %d,%d", rect.left, rect.top, rect.right, rect.bottom));
             if(rect.contains(x, y)){//点击抽屉控件时交由系统处理
             Log.i("MySlidingDrawer", "Hit handle");
             }else{
             return false;
             }
         }
         return super.onInterceptTouchEvent(event);
        }
        
        @Override
        public boolean onTouchEvent(MotionEvent event) {
    Log.i("MySlidingDrawer on touch", "Action=" + event.getAction());
         return super.onTouchEvent(event);
        }}
    测试代码:public class SlidingDrawerTest extends Activity{
    Button btnLeft, btnRight, btnContent;
    MySlidingDrawer ctlSlidingDrawer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.slidingdrawer); ctlSlidingDrawer = (MySlidingDrawer)this.findViewById(R.id.ctlSlidingDrawer);
    ctlSlidingDrawer.setHandleId(R.id.ctlHandle);
    ctlSlidingDrawer.setTouchableIds(new int[]{R.id.btnLeft, R.id.btnRight});
    btnLeft = (Button)this.findViewById(R.id.btnLeft);
    btnLeft.setOnClickListener(onLeftClickListener);
    btnRight = (Button)this.findViewById(R.id.btnRight);
    btnRight.setOnClickListener(onButtonClickListener);
    btnContent = (Button)this.findViewById(R.id.btnContent);
    btnContent.setOnClickListener(onButtonClickListener);
    }
    private OnClickListener onLeftClickListener = new OnClickListener(){
    public void onClick(View v) {
    Log.i("SlidingDrawerTest", "left button click");
    }
    };

    private OnClickListener onButtonClickListener = new OnClickListener(){
    public void onClick(View v) {
    Button button = (Button)v;
    Log.i("SlidingDrawerTest", "Click button " + button.getText().toString());
    }
    };
    }
      

  7.   

    layout:<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
         <android.HelloTest.MySlidingDrawer android:layout_width="wrap_content" android:id="@+id/ctlSlidingDrawer" android:layout_height="match_parent" android:handle="@+id/handle" android:content="@+id/content">
           <LinearLayout android:id="@+id/handle" android:layout_width="wrap_content" android:layout_height="wrap_content">      
            <Button android:text="Left"  android:id="@+id/btnLeft" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>    
            <Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Handle" android:id="@+id/ctlHandle"></Button>
            <Button android:text="Right" android:id="@+id/btnRight" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>  
            </LinearLayout> 
            <LinearLayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent">
               <Button android:text="Middle" android:id="@+id/btnContent" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
               <TextView android:text="This is the content" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
            </LinearLayout>
         </android.HelloTest.MySlidingDrawer>
    </LinearLayout>
      

  8.   

    非常感谢金刀,基本搞定!!但是还有两个小问题,不知能否帮助检查一下:
    1.两侧按钮施加点击事件后,单击会执行两次;
    2.长按中间的handle区域,会触发两侧按钮的pressed状态,我在这个状态里面为按钮添加了背景图片。分数已送出,再次感谢!
      

  9.   

    请教第二点如何解决的?
    “2.长按中间的handle区域,会触发两侧按钮的pressed状态,我在这个状态里面为按钮添加了背景图片。”
      

  10.   

    对于大家遇到的第二种情况,解决方案如下:
    1.去掉在xml文件中对Button定义的点击状态,将android:background更改为一张默认的资源图片;
    2.在Activity里对左右两边的Button添加OnTouchListener的事件,在该监听事件中,可以针对左右两边的Button分别设置Down/Move/Up/Cancel的效果。希望对你有帮助。
      

  11.   

    不错  我也实现了这个
    但是在Miui的系统里面  LeftButton与RightButton点击事件不能执行
    请问有解决方法吗?
      

  12.   

    我最近也在做这块 我现在的handle是一个listview  点击不同的item 右侧显示不同的comment  求大牛指点。有份。