【问题标题】:Make SlidingDrawer's content's part always visible?让 SlidingDrawer 的内容部分始终可见?
【发布时间】:2013-03-24 03:37:23
【问题描述】:

我正在使用 android:bottomOffset 使抽屉从底部伸出 100dip。效果很好,但我的内容不可见。只有当我触摸抽屉时它才可见。如何让它始终可见(100dip 显示内容)?

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

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

我希望它从一开始就在正确的部分。

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

【问题讨论】:

  • 为什么要这样做?如果内容总是可见的,为什么还要使用滑动抽屉呢?
  • 请阅读这个问题,它是关于 100dip 的一部分,而不是全部内容。
  • 问题的原始版本不清楚。听起来 100dp 就是一切。
  • 啊,不,抱歉,这只是前 100dip,内容要大得多。
  • @lxx 当抽屉折叠时你能点击列表的可见项目吗?

标签: android slidingdrawer


【解决方案1】:

这是一个可行的解决方案:

创建一个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 时才会绘制,而滑块“关闭”时则不然。可能开发人员没有想到在“关闭”时显示部分内容的滑块,因此在滑块关闭时绘制内容没有任何意义。

【讨论】:

  • @lxx 嘿,在我的第一行(可见部分)我有按钮。当抽屉关闭并且我的某些内容可见时,我无法单击它们。你能建议我如何实现它吗?
【解决方案2】:

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" 就像在原始问题中解释的那样。

【讨论】:

  • 是的,扩展是正确的做法。据我所知,在我评论这些行之前,这是不可能的,因为私人成员。之后我没有改变它。
  • @Dragan Marjanovic 嘿,在我的第一行(可见部分)我有按钮。当抽屉关闭并且我的某些内容可见时,我无法单击它们。你能建议我如何实现它吗? ——
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-23
  • 2013-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多