Android ExpandableListView上的向下滑动效果
在展开/折叠ExpandableListView的项目时,是否可能具有良好的向上/向下滑动效果 如果是,如何进行Android ExpandableListView上的向下滑动效果,android,expandablelistview,expand,slidedown,slideup,Android,Expandablelistview,Expand,Slidedown,Slideup,在展开/折叠ExpandableListView的项目时,是否可能具有良好的向上/向下滑动效果 如果是,如何进行 提前谢谢。所以这是一份完整的副本。简言之,我使用了一个常规列表,创建了自己的下拉视图,使用了自定义的下拉动画和voila success(查看链接了解更多描述) 编辑:分步指南: 首先,我创建xml列表行: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schem
提前谢谢。所以这是一份完整的副本。简言之,我使用了一个常规列表,创建了自己的下拉视图,使用了自定义的下拉动画和voila success(查看链接了解更多描述) 编辑:分步指南: 首先,我创建xml列表行:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/row_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/row_simple_parent"
>
<View
android:id="@+id/row_simple_parent_invis_cover"
android:visibility="gone"
android:layout_height="something that fills out your content"
android:layout_width="match_parent"
android:background="@android:color/transparent"/>
</RelativeLayout>
<!-- Dropdown -->
<RelativeLayout
android:id="@+id/row_dropdown"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</RelativeLayout>
</LinearLayout>
然后神奇的事情发生在你的列表中:
@Override
public void onListItemClick(ListView list, View view, int position, long id) {
final ListItem item = (ListItem) list.getAdapter().getItem(position);
// set dropdown data
ViewHolder holder = (ViewHolder) view.getTag();
final View dropDown = holder.mDropDown;
// set click close on top part of view, this is so you can click the view
// and it can close or whatever, if you start to add buttons etc. you'll loose
// the ability to click the view until you set the dropdown view to gone again.
final View simpleView = view.findViewById(R.id.row_simple_parent_invis_cover);
simpleView.setVisibility(View.VISIBLE);
final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if(msg.what == 0) {
// first we measure height, we need to do this because we used wrap_content
// if we use a fixed height we could just pass that in px.
ExpandCollapseAnimation.setHeightForWrapContent(getActivity(), dropDown);
ExpandCollapseAnimation expandAni = new ExpandCollapseAnimation(dropDown, DROP_DOWN_TIME);
dropDown.startAnimation(expandAni);
Message newMsg = new Message();
} else if(msg.what == 1) {
ExpandCollapseAnimation expandAni = new ExpandCollapseAnimation(dropDown, DROP_DOWN_TIME);
dropDown.startAnimation(expandAni);
simpleView.setOnClickListener(null);
simpleView.setVisibility(View.GONE);
}
}
};
simpleView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
handler.sendEmptyMessage(1);
}
});
// start drop down animation
handler.sendEmptyMessage(0);
}
最后一点意见:我不确定这是最好的方法,但这对我来说是有效的
编辑:youtube上的开发者提供了一种不同的解决方案,可以查看。Warpzit指出的答案是正确的。我使用这种方法提供了一个库,您可以轻松地将它嵌入到应用程序中,而不必知道它实际上是如何工作的: 更多关于它的信息可以在这篇博文中阅读: 在java类中尝试此操作
expListView.setAdapter(listAdapter);
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
int previousGroup = -1;
@Override
public void onGroupExpand(int groupPosition) {
if(groupPosition != previousGroup)
expListView.collapseGroup(previousGroup);
previousGroup = groupPosition;
}
});
Warpzit的实现确实有效,但是如果您需要支持包含大量子项(比如100个)的组,则无法使用,因为您将不会使用ListView的优化结构(即子视图的重用/回收)。相反,我最终扩展了ExpandableListView来创建一个使用我描述的技术的AnimatedExpandableListView。通过这样做,AnimatedExpandableListView可以在提供最佳性能的同时为组扩展设置动画 展开/折叠不适用于此处的代码:因为
OnItemExpandCollapseListAdapter
中的OnItemExpandableListAdapter
为null
;当动画开始时,会调用方法NotifieExpandCollapsElistener,但侦听器为null
,因为:您有ActionSlideExpandableListView
:
ActionSlideExpandableListView lv = (ActionSlideExpandableListView) findViewById(R.id.list_view);
SlideExpandableListAdapter slideAdapter = new SlideExpandableListAdapter(adapter,R.id.expandable_toggle_button, R.id.expandable);
<com.your.package.project.class.location.AnimatedExpandableListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
然后设置适配器:lv.setAdapter(slidedapter)
从SlideExpandableListView
调用方法setAdapter
,然后创建了SlideExpandableListAdapter
的新实例
我是这样改变的:
ActionSlideExpandableListView
中的setAdapter
方法也将AbstractSlideExpandableListAdapter.OnItemExpandCollapseListener
作为参数,该参数从SlideExpandableListView
传递给setAdapter
方法。那里
当我创建SlideExpandableListAdapter
时,我还会传递此侦听器:
public void setAdapter(ListAdapter adapter, AbstractSlideExpandableListAdapter.OnItemExpandCollapseListener expandCollapseListener) {
this.adapter = new SlideExpandableListAdapter(adapter, expandCollapseListener);
super.setAdapter(this.adapter);
}
public SlideExpandableListAdapter(ListAdapter wrapped, OnItemExpandCollapseListener expandCollapseListener) {
this(wrapped, R.id.expandable_toggle_button, R.id.expandable);
setItemExpandCollapseListener(expandCollapseListener);
}
一个简单的解决方案是使用Gary Guo创建的类
AnimatedExpandableListView
,如果您已经在使用扩展了ExpandableListAdapter
的BaseExpandableListAdapter
。这样就不需要修改列表视图
您只需要子类化AnimatedExpandableListAdapter
,而不是baseexpandablelistapter
和AnimatedExpandableListView
来代替ExpandableListView
不要使用@Override-getChildrenCount
,只需使用@Override-getRealChildrenCount
。对@Override getChildView执行相同的操作,
在其位置使用@Override getRealChildView
然后使用动画,如下所示:
expandableListView.setOnGroupClickListener(new AnimatedExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
if (expandableListView.isGroupExpanded(groupPosition))
expandableListView.collapseGroupWithAnimation(groupPosition);
else
expandableListView.expandGroupWithAnimation(groupPosition);
return true;
}
});
另一个细节是,在布局xml文件中,您需要引用AnimatedExpandableListView
,而不是ExpandableListView
:
ActionSlideExpandableListView lv = (ActionSlideExpandableListView) findViewById(R.id.list_view);
SlideExpandableListAdapter slideAdapter = new SlideExpandableListAdapter(adapter,R.id.expandable_toggle_button, R.id.expandable);
<com.your.package.project.class.location.AnimatedExpandableListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
如果需要进一步帮助,项目中的示例代码和
AnimatedExpandableListView
类上的注释非常有用 也许有一种简单的方法可以降低ExpandableListView上展开效果的速度。有没有一种方法???我是唯一一个想知道如何做到这一点的人吗???你找到了解决办法吗?谢谢。不,我没有。我想我们必须让Android实现一个很好的小部件你有什么可扩展的解决方案吗listview@Kermia我以后再做。不确定是否在此处/在其他帖子中或同时在这两个帖子中发布=/。既然你要求它,我想我会把它贴在这里。嗨,我用你的例子做我自己的动画,但我不知道postDropHandler
做什么,如果可能的话,我也不知道它是如何实现的。ThanksI我很确定我自己的实现遗留下来的代码将在以后进行清理。我用它来告诉动画什么时候结束了,这样我就可以在动画结束时做些什么了。我有点困惑(基本上是因为我试图在我自己的测试应用程序中实现你答案中的代码):onListItemClick
和getView
方法是用于ListView的,但我们在这里使用的是ExpandableListView。那么为什么要使用这些ListView方法呢?另外,您的ViewHolder
类是什么?如果我们想在ExpandableListView中设置组扩展的动画,这将没有帮助。这个解决方案很好,但解决了一个不同的问题。