滑动抽屉没有关闭

时间:2014-10-01 10:15:55

标签: java android

我创建了一个简单的SliderDrawer。单击手柄时,它会正确打开,但再次单击时,它不会关闭。我在类上设置了onclick,所以当单击手柄并且滑动抽屉打开时它会关闭但不起作用。任何想法为什么请?

Slider.java

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SlidingDrawer;

@SuppressWarnings("deprecation")
public class Slider extends Activity {

    SlidingDrawer sd;
    Button handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sliding);
        sd = (SlidingDrawer) findViewById(R.id.slidingD);
        handler = (Button) findViewById(R.id.handle);
        handler.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(sd.isOpened() == true){
                    sd.animateClose();
                }
            }
        });
    }
}

sliding.xml

<?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:orientation="vertical" >

    <SlidingDrawer
        android:id="@+id/slidingD"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:content="@+id/content"
        android:handle="@+id/handle" >
        <!-- android:handle calls the button (Handle) -->
        <!-- android:content calls the linear layout (Hidden Content) -->

        <Button
            android:id="@+id/handle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Handle" />

        <LinearLayout
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <CheckBox
                android:id="@+id/cSlideable"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="CheckBox" />
        </LinearLayout>
    </SlidingDrawer>

</LinearLayout>

2 个答案:

答案 0 :(得分:3)

使用抽屉式监听器,它将为您节省处理不同状态的压力

mDrawerLayout.setDrawerListener(mListener);

https://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html

答案 1 :(得分:0)

我所做的工作是设置

  

handler.OnClickListener

  

(本)

并使用了覆盖onClick方法

@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.handle:
            sd.close();
            break;
        }
    }