我正在处理我的应用程序的一部分,我想知道是否可以让滑动抽屉部分打开而不是完全关闭,以便用户可以在单击显示更多按钮之前达到文本内容的峰值,这将能够显示其余部分的文本。
提前致谢。
我正在处理我的应用程序的一部分,我想知道是否可以让滑动抽屉部分打开而不是完全关闭,以便用户可以在单击显示更多按钮之前达到文本内容的峰值,这将能够显示其余部分的文本。
提前致谢。
我遇到了类似的问题,我最终修改了 SlidingDrawer 以便能够在抽屉折叠/关闭时显示部分内容。使用SemiClosedSlidingDrawer,您可以使用尺寸属性“semiClosedContentSize”指定应在折叠/关闭模式下显示多少内容屏幕:
<se.smartrefill.view.SemiClosedSlidingDrawer
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/se.smartrefill.app.x"
android:id="@+id/mySlidingDrawer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
custom:orientation="horizontal"
custom:handle="@+id/handle"
custom:content="@+id/content"
custom:allowSingleTap="true"
custom:semiClosedContentSize="40dp"
>
然后您的 attrs.xml(在 res/values/ 中)还需要包含:
<declare-styleable name="SemiClosedSlidingDrawer">
<attr name="handle" format="integer"/>
<attr name="content" format="integer"/>
<attr name="orientation" format="string" />
<attr name="bottomOffset" format="dimension" />
<attr name="topOffset" format="dimension" />
<attr name="allowSingleTap" format="boolean" />
<attr name="animateOnClick" format="boolean" />
<attr name="semiClosedContentSize" format="dimension" />
</declare-styleable>
,其中“se.smartrefill.app.x”指的是AndroidManifest中定义的应用包。
这个特定的 SlidingDrawer(上图)被配置为使用水平方向,并且内容视图的 40dp 宽(和 fill_parent 高)条将在折叠/关闭模式下显示(在右侧)。在展开/打开模式下,此实现将与标准 SlidingDrawer 一样工作,但在折叠/关闭模式下,内容屏幕将永远不会完全隐藏(除非您指定 semiClosedContentSize="0dp",这会将其变成标准 SlidingDrawer)。此实现基于 Android-4(“1.6”)中的 SlidingDrawer 实现,但兼容(包括)Android-14(“4.0”)。此实现也应该与未来版本的 Android 兼容(API 4 和 API 14 之间的 SlidingDrawer 几乎没有任何变化)。
试试看!
问候,雅各布
我遇到了和你类似的问题。一开始我只需要在我的滑动抽屉里有一个(相当狭窄的)列表视图,这样用户就可以对内容有一个高峰。只有当列表中的一个项目被选中并且该项目的一些内容显示在列表视图旁边的图像视图中时,抽屉才应该完全打开。
因此我的抽屉布局是这样的:
<RelativeLayout
android:id="@+id/drawerContainer"
android:layout_alignParentRight="true"
android:layout_width="120dp"
android:layout_height="match_parent">
<SlidingDrawer
android:id="@+id/drawer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:handle="@+id/handle"
android:content="@+id/content">
<ImageView
android:id="@+id/handle"
android:layout_width="20dip"
android:layout_height="match_parent"
android:src="@drawable/drawer_handle />
<LinearLayout
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<ListView
android:id="@+id/list"
android:layout_width="100dp"
android:layout_height="match_parent">
</ListView>
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone">
</ImageView>
</LinearLayout>
</SlidingDrawer>
</RelativeLayout>
所以一开始图像视图就消失了。在我的列表视图的 OnItemClickListener 中,我控制抽屉的布局参数:
private OnItemClickListener onItemClickListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (imageView.getVisibility() == View.GONE) {
ExpandViewAnimation animation = new ExpandViewAnimation(drawerContainer, ((View) drawerContainer.getParent()).getWidth());
animation.setDuration(500);
drawerContainer.setAnimation(animation);
animation.start();
imageView.setVisibility(View.VISIBLE);
}
//code for displaying an image in the imageview
}
};
如您所见,我将自定义动画用于抽屉容器,以便保留打开的滑动抽屉的精细度。这是动画类:
public class ExpandViewAnimation extends Animation {
int targetWidth;
int initialWidth;
View view;
public ExpandViewAnimation(View view, int targetWidth) {
this.view = view;
this.targetWidth = targetWidth;
initialWidth = view.getWidth();
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
int newWidth = initialWidth + (int) ((targetWidth - initialWidth) * interpolatedTime);
LayoutParams lp = new LayoutParams((LayoutParams) view.getLayoutParams());
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
view.setLayoutParams(lp);
view.getLayoutParams().width = newWidth;
view.requestLayout();
if (newWidth == targetWidth) {
LayoutParams matchParentParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
view.setLayoutParams(matchParentParams);
view.clearAnimation();
}
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
}
@Override
public boolean willChangeBounds() {
return true;
}
}
我设置新布局参数的原因是因为在开始时我保存了初始参数集(如在布局文件中)并将其应用回 onDrawerClose:
private OnDrawerCloseListener onDrawerCloseListener = new OnDrawerCloseListener() {
@Override
public void onDrawerClosed() {
imageView.setVisibility(View.GONE);
imageView.setImageDrawable(null);
drawerContainer.setLayoutParams(initialLayoutParams);
}
};
如果您希望用户能够通过手柄上的滑动移动完全打开抽屉,您可以在 onTouchListener 中应用类似的动画。
最后一点:我的滑动抽屉位于布局中,因为它包含在该布局中,因此抽屉可以在多个地方访问。一般来说,您不需要抽屉周围的布局。
也许尝试设置滑动抽屉的内部内容之类的东西
<SlidingDrawer android:layout_height="wrap_content"
android:handle="@+id/handle"
android:content="@+id/content"
android:id="@+id/slide"
android:orientation="vertical"
android:layout_width="fill_parent">
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"...>
<Button android:id="@+id/ShowMore"... />
<LinearLayout android:id="@+id/More" android:visibility="gone" ...>
... more stuff
</LinearLayout>
</LinearLayout>
</SlidingDrawer>
然后设置按钮 ShowMore 以在 View.GONE 和 View.VISIBLE 之间切换线性布局“更多”的可见性
**编辑:嗯,重新阅读你的问题,我实际上有点困惑:标题是“..opened on start”,但你说“部分打开”。您是否希望它以部分打开的状态启动?还是在用户拖动时部分打开?我的建议(未经测试)是让滑动抽屉将其大小包装到内容中,并让内容最初开始很小,仅显示按钮 + 任何您想在“更多”内部线性布局之外显示的内容,然后显示当他们单击按钮。
我最近开发了应用程序,其中我有你要求的要求。通过谷歌搜索,我发现我们已经修改了原始滑动抽屉的源代码,我这样做了,整个修改后的代码都在我身上,我已经测试过了,它工作正常。
我可以将整个文件发送给您。只需给我您的电子邮件 ID。
您还需要在项目的 values 文件夹中使用此代码的 attrs.xml。attrs.xml 代码发布在下面。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="SlidingDrawer">
<attr name="handle" format="integer"/>
<attr name="content" format="integer"/>
</declare-styleable>
</resources>
我希望这能帮到您。