我正在寻找创建类似于此页面上所示的手风琴式小部件的最佳方法。 是否有使用标准Android工具包实现相同效果的方法,或者我需要构建自定义小部件?如果需要 - 如果有的话,您推荐扩展哪个小部件?
我正在寻找创建类似于此页面上所示的手风琴式小部件的最佳方法。 是否有使用标准Android工具包实现相同效果的方法,或者我需要构建自定义小部件?如果需要 - 如果有的话,您推荐扩展哪个小部件?
如果你还在疑惑,这可以通过线性布局内堆叠的一对按钮/布局来实现。以下是伪代码
<LinearLayout android:orientation="vertical">
<Button android:text="Panel 1"/>
<SomeKindOfLayout android:id="@+id/panel1">
<!-- widgets in first panel go here -->
</SomeKindOfLayout>
<Button android:text="Panel 2"/>
<SomeKindOfLayout android:id="@+id/panel2" android:visibility="gone">
<!-- widgets in second panel go here -->
</SomeKindOfLayout>
<Button android:text="Panel 3"/>
<SomeKindOfLayout android:id="@+id/panel3" android:visibility="gone">
<!-- widgets in third panel go here -->
</SomeKindOfLayout>
<Button android:text="Panel 4"/>
<SomeKindOfLayout android:id="@+id/panel4" android:visibility="gone">
<!-- widgets in fourth panel go here -->
</SomeKindOfLayout></LinearLayout>
另一个可能尝试的方法是将可扩展列表视图(ExpandableListView)相互叠放在一起
ListActivity
(或ListFragment
)中使用布局,则应该只有一个带有android:id="@android:id/list"
的ListView。 - Alex Semeniuk我在Github上推出了Android手风琴视图项目。我们为客户使用它,在2.2、2.3.x、3.2和4.0.3上进行了测试。对我们来说效果非常不错。
接下来,我们将在展开/折叠时添加动画效果。
这里是一个小截图:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TasksListActivity">
<Button
android:id="@+id/magic_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Magic Btn"/>
<LinearLayout
android:id="@+id/magic_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
</LinearLayout>
</ScrollView>
2.- 然后去找到对应的JAVA文件,找到按钮并设置一个onClick监听器,在监听器内部你会找到布局。
Button findMagicBtn = (Button) findViewById(R.id.magic_btn);
findMagicBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
3.- 现在,在onClickListner中我们需要找到这个布局。正如您在第一步中看到的,该布局默认设置为GONE
,但现在我们需要将其设置为可见。问题是,我如何再次隐藏它并相反呢?因此,我们将添加一个条件来获取布局的可见性,并基于此来隐藏或显示:
Button findMagicBtn = (Button) findViewById(R.id.magic_btn);
findMagicBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout findMagicLl = (LinearLayout) findViewById(R.id.magic_layout);
if (findMagicLl.getVisibility() == View.VISIBLE) {
findMagicLl.setVisibility(View.GONE);
} else {
findMagicLl.setVisibility(View.VISIBLE);
}
}
});
findMagicBtn.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.right_arrow, 0);
您应该在onClick方法内执行此操作,并使用条件来更改箭头的方向,通过更改可绘制对象(如果是gone,则为下箭头,如果是visible则为上箭头)。
此外,您可以通过添加动画使其看起来更自然,如下所示:
ScaleAnimation animation = new ScaleAnimation(1f, 1f, 1f, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f);
animation.setDuration(180);
animation.setFillAfter(true);
findMagicLl.startAnimation(animation);
您需要考虑到上述动画必须在视图可见的情况下完成。请注意,setFillAfter(true)
会永久地改变容器的大小,因此如果使用了该属性,可能就不再需要使用VIEW.GONE
了。这个问题有一个关于缩放动画的很好的解释。
include':app',':easyaccordion'
implementation 'com.riyagayasen.android:easyaccordion:1.0.3'