问题描述
随着新的Facebook应用程序,它配备了一个隐藏的侧边栏,我喜欢使用类似的东西在我的应用程序。它看起来有点像Firefox移动版拥有侧栏...
With the new facebook app it comes with an hidden sidebar that I would love to use something like that in my applications. It looks kinda like the sidebars that firefox mobile have...
你有什么想法如何实现它除了重新实现ViewPager?我试着用HorizontalScrollView但也将导致重新实现它...
Do you have any idea how to implement it besides re-implementing the ViewPager? I've tried with an HorizontalScrollView but that would also lead to re-implementation of it...
我没有看到任何其他方式除了这两个...有什么建议?
I'm not seeing any other way besides these two... any suggestions?
在此先感谢
推荐答案
我想出了一个解决办法...我不知道这是否是完美的,但它运作良好。
I came up with a solution... I don't know if it is perfect but it is working well.
所以,我所做的就是与两个叠在一起的布局单一的FrameLayout,然后我只是动画的顶尖布局滑动到屏幕的右侧(只需要调用slideTo或scrollBy。基本上它是的!挺简单而有效的(在code,虽然不是很pretty的:P)!
So what I did was a single FrameLayout with both of the Layouts stacked together and then I just animate the top layout to slide to the right of the screen (just need to call the slideTo or scrollBy. And basically it's that! Quite simple and effective! (the code though is not very pretty :P)
编辑:
有些code样本。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFF" >
<include
android:id="@+id/menu_layout"
layout="@layout/menu_list"
android:visibility="invisible"/>
<include
android:id="@+id/news_list_parent"
layout="@layout/main_news_list"
/>
</FrameLayout>
这是布局的xml,相当simpe。所包含的.xml简单LinearLayouts有一个标题和一个列表视图。
This is the layout xml, quite simpe. The included .xml are simple LinearLayouts with a heading and a listview.
神奇发生在动画:
protected void applyTransformation(float interpolatedTime, Transformation t) {
int newOffset;
if(expanded) {
newOffset = 0;
newOffset = (int)(endOffset*(1-interpolatedTime));
} else {
newOffset = (int)(endOffset*(interpolatedTime));
}
view.scrollTo(-newOffset, 0);
}
的endOffset是目标的运动。我将它之前,我开始了动画,查看我要动画(在这种情况下是用id = news_list_parent视图)它被设置在构造函数中。
The endOffset is the target movement. I set it before I start the animation, and the View I want to animate (in this case is the view with the id=news_list_parent) it is set on the constructor.
但只是为了了解如何工作做一个按钮,它的听众会做这样的事情:
But just to understand how that works make a button and its listener would do something like this:
if(viewBeneath.getVisibility() == View.INVISIBLE) {
viewBeneath.setVisibility(View.Visible);
viewToSlide.slideTo(-(width-50), 0);
}
最后覆盖后退按钮做的按钮相对
And finally override the back button to do the opposite of the button
if(viewBeneath.getVisibility() == View.VISIBLE) {
viewToSlide.slideTo(0, 0);
viewBeneath.setVisibility(View.Visible);
}
看这是伪code =)这是我在开始的时候一样,就是code丢失:P
Read this as pseudo-code =) This is what I did in the beginning, that code is lost :P
这篇关于Android的侧边栏,如Facebook或Firefox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!