让SlidingDrawer的内容始终可见?

时间:2012-07-15 13:29:19

标签: android slidingdrawer

我正在使用android:bottomOffset使抽屉从底部伸出100dip。这工作正常,但我的内容不可见。它只有在我触摸抽屉时才能看到。如何使其始终可见(100dip显示内容)?

我首先认为这是一个可见性问题,因为内容的可见性在onFinishInflate()prepareContent()closeDrawer()中设置为GONE ...复制了SlidingDrawer并删除了这些行,没有解决它。这似乎是一个位置问题,目前我正在玩这些数字,但仍然没有找到如何让内容出现在它应该的位置......而且没有更多时间用于此...任何帮助非常感谢。

以下是快速理解问题的图片:

enter image description here

我希望从一开始就看起来像是在正确的部分。

这个默认行为对我来说也是错误的,我不知道为什么有人会想只为句柄做偏移,在它和内容之间留一个间隙,然后在触摸时将内容直接放在句柄下...

2 个答案:

答案 0 :(得分:2)

这是一个有效的解决方案:

创建SlidingDrawer类的完整副本,然后替换方法dispatchDraw,使其如下所示:

@Override
protected void dispatchDraw(Canvas canvas) {
    final long drawingTime = getDrawingTime();
    final View handle = mHandle;
    final boolean isVertical = mVertical;

    drawChild(canvas, handle, drawingTime);

    //if (mTracking || mAnimating) {
        final Bitmap cache = mContent.getDrawingCache();
        if (cache != null) {
            if (isVertical) {
                canvas.drawBitmap(cache, 0, handle.getBottom(), null);
            } else {
                canvas.drawBitmap(cache, handle.getRight(), 0, null);                    
            }
        } else {
            canvas.save();
            canvas.translate(isVertical ? 0 : handle.getLeft() - mTopOffset,
                    isVertical ? handle.getTop() - mTopOffset : 0);
            drawChild(canvas, mContent, drawingTime);
            canvas.restore();
        }
    //} else if (mExpanded) {
        //drawChild(canvas, mContent, drawingTime);
    //}
}

我所做的是评论线。当您看到mContent(滑块的内容)时,只有在mTracking,mAnimating或mExpanded为true时才会被绘制,而滑块是"关闭"则不是这种情况。可能开发人员并没有想到一个滑块显示部分内容而关闭"所以在滑块关闭时绘制内容没有任何意义。

答案 1 :(得分:0)

lxx的答案完美无瑕。我无法将所有代码粘贴到响应中,因此我提出了另一个答案。唯一不同于lxx的代码是我没有复制所有SlidingDrawer类,而是通过创建新类来扩展它。以下是完整的代码。

package com.localini.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.widget.SlidingDrawer;

public class ExtendedSlidingDrawer extends SlidingDrawer {

    private boolean mVertical;
    private int mTopOffset;

    public ExtendedSlidingDrawer(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        int orientation = attrs
                .getAttributeIntValue("android", "orientation", ORIENTATION_VERTICAL);
        mTopOffset = attrs.getAttributeIntValue("android", "topOffset", 0);
        mVertical = (orientation == SlidingDrawer.ORIENTATION_VERTICAL);
    }

    public ExtendedSlidingDrawer(Context context, AttributeSet attrs) {
        super(context, attrs);

        int orientation = attrs
                .getAttributeIntValue("android", "orientation", ORIENTATION_VERTICAL);
        mTopOffset = attrs.getAttributeIntValue("android", "topOffset", 0);
        mVertical = (orientation == SlidingDrawer.ORIENTATION_VERTICAL);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        final long drawingTime = getDrawingTime();
        final View handle = getHandle();
        final boolean isVertical = mVertical;

        drawChild(canvas, handle, drawingTime);

        //if (mTracking || mAnimating) {
        final Bitmap cache = getContent().getDrawingCache();
        if (cache != null) {
            if (isVertical) {
                canvas.drawBitmap(cache, 0, handle.getBottom(), null);
            } else {
                canvas.drawBitmap(cache, handle.getRight(), 0, null);
            }
        } else {
            canvas.save();
            canvas.translate(isVertical ? 0 : handle.getLeft() - mTopOffset,
                        isVertical ? handle.getTop() - mTopOffset : 0);
            drawChild(canvas, getContent(), drawingTime);
            canvas.restore();
        }
        //} else if (mExpanded) {
        //drawChild(canvas, mContent, drawingTime);
        //}
    }
}

在xml布局文件中,您需要设置例如android:buttonOffser="-100dip"就像在原始问题中解释的一样。