Android 将动画添加到ExpandableListView
在Android中打开可扩展列表时,有没有添加动画的方法? 我希望这样,当用户单击可展开列表时,它会产生一种动画/效果,就像我打开一个滑动抽屉一样Android 将动画添加到ExpandableListView,android,expandablelistview,android-sliding,Android,Expandablelistview,Android Sliding,在Android中打开可扩展列表时,有没有添加动画的方法? 我希望这样,当用户单击可展开列表时,它会产生一种动画/效果,就像我打开一个滑动抽屉一样 它缓慢移动,直到完全打开。因此,我所做的是使用常规的列表视图,然后在onListItemClick中执行动画。 动画与我在该链接中所做的类似: 但仅适用于行视图的一部分。行视图以以下方式在xml中实现: <somelayout> <normal> </normal> <expanded
它缓慢移动,直到完全打开。因此,我所做的是使用常规的
列表视图,然后在onListItemClick中执行动画。
动画与我在该链接中所做的类似:
但仅适用于行视图的一部分。行视图以以下方式在xml中实现:
<somelayout>
<normal>
</normal>
<expanded>
</expanded>
</somelayout>
使用法线时不展开。激活“展开”后,展开设置为“可见”,而不是“消失”。您需要控制在关闭时再次将其设置为gone(请记住,这是在回收的ConvertView中设置的)
如果需要,我可以进一步澄清,这只是需要输入大量代码。我也遇到了同样的问题。我一劳永逸地把它修好了。我把它开源到github。
基本上,您将此项目依赖项包含在Android项目中。然后将列表适配器
包装到幻灯片ExpandableListAdapter
中。包装器随后会将带有动画的幻灯片功能添加到列表视图中
希望它能对您有所帮助,我已经在两个项目中使用了它。我花了很多时间搜索,但运气不佳。现有的解决方案不够平滑-如果您的布局比2个按钮更复杂,它就会变得滞后
因此,我创建了自己的ListAdapter
,它将整个视图缓存到位图中,然后在缓存的视图而不是视图本身上执行动画。它工作得更快
这是:
好消息是,您不需要重写一堆代码—只需将myExpandableAdapter
包装在适配器周围,并提供视图的id,该id将充当切换按钮,以及保存第二级内容的视图的id:
new ExpandableAdapter(context, yourAdapter, R.id.switch, R.id.holder);
这就是我在这种情况下所做的一切
对象动画师对象动画师=
PropertyValuesHolder的ObjectAnimator,
PropertyValuesHolder.ofit(“底部”,
currentlistHeight,currentlistHeight*2))
这将使listView的高度增加一倍,并设置动画。
如果将当前列表高度设置为零。这就像一个抽屉。我也试着让它工作起来。我找到了一个适用于ChildView的解决方案。但它不会为组的实际展开设置动画,而是在子单元填充展开后留下的空间时为其设置动画
编辑:折叠中有一个错误,它会使一些不应该隐藏的单元格变得隐藏。这可能与listView中的视图循环有关。我会在找到解决方案后更新
在设置组ClickListener中使用LayoutImation设置动画
mResultList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
if(mResultList.isGroupExpanded(groupPosition)){
mProgAdap.prepareToCollapseGroup(groupPosition);
setupLayoutAnimationClose(groupPosition);
mResultList.requestLayout();
}else{
boolean autoScrollToExpandedGroup = false;
mResultList.expandGroup(groupPosition,autoScrollToExpandedGroup);
setupLayoutAnimation();
//*/
}
//telling the listView we have handled the group click, and don't want the default actions.
return true;
}
private void setupLayoutAnimation() {
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(50);
set.addAnimation(animation);
animation = new ScaleAnimation(1.0f, 1.0f, 0.0f, 1.0f, 0.5f, 1.0f);
animation.setDuration(50);
set.addAnimation(animation);
LayoutAnimationController controller = new LayoutAnimationController(set, 0.75f);
mResultList.setLayoutAnimationListener(null);
mResultList.setLayoutAnimation(controller);
}
private void setupLayoutAnimationClose(final int groupPosition) {
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(1.0f, 0.0f);
animation.setDuration(50);
animation.setFillAfter(true);
animation.setFillEnabled(true);
set.addAnimation(animation);
animation = new ScaleAnimation(1.0f, 1.0f, 1.0f, 0.0f, 0.5f, 0.0f);
animation.setDuration(50);
animation.setFillAfter(true);
animation.setFillEnabled(true);
set.addAnimation(animation);
set.setFillAfter(true);
set.setFillEnabled(true);
LayoutAnimationController controller = new LayoutAnimationController(set, 0.75f);
controller.setOrder(LayoutAnimationController.ORDER_REVERSE);
mResultList.setLayoutAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
mResultList.collapseGroup(groupPosition);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
mResultList.setLayoutAnimation(controller);
}
});
我们需要进行更多调整,使动画仅应用于展开/折叠组的实际子对象。由于无法在LayoutImationController中重载正确的部分,因此需要创建一个特殊的ViewGroup类。这与中的技术相同
在ExpandableListViewAdapter中,我们现在需要一些状态处理来允许或忽略列表中项目的动画
@Override
public void onGroupExpanded(int groupPos){
super.onGroupExpanded(groupPos);
int childCount = getChildrenCount(groupPos);
Log.d("EXPLIST","setting children to be expanded:" + childCount);
for(int j=0; j < getGroupCount(); j++){
for(int k=0; k < getChildrenCount(j); k++){
GoalServiceCell cell = (GoalServiceCell)getChild(j,k);
cell.expandAnimState = GoalServiceCell.ExpandAnimState.SHOULD_NOT_ANIMATE;
}
}
for(int i=0; i < childCount; i++){
GoalServiceCell cell = (GoalServiceCell)getChild(groupPos,i);
cell.expandAnimState = GoalServiceCell.ExpandAnimState.SHOULD_START_EXPAND;
}
}
public void prepareToCollapseGroup(int groupPos){
int childCount = getChildrenCount(groupPos);
for(int j=0; j < getGroupCount(); j++){
for(int k=0; k < getChildrenCount(j); k++){
GoalServiceCell cell = (GoalServiceCell)getChild(j,k);
cell.expandAnimState = GoalServiceCell.ExpandAnimState.SHOULD_NOT_ANIMATE;
}
}
for(int i=0; i < childCount; i++){
GoalServiceCell cell = (GoalServiceCell)getChild(groupPos,i);
cell.expandAnimState = GoalServiceCell.ExpandAnimState.SHOULD_START_COLLAPSIN;
}
}
@Override
public void onGroupCollapsed(int groupPos){
super.onGroupCollapsed(groupPos);
int childCount = getChildrenCount(groupPos);
for(int i=0; i < childCount; i++){
GoalServiceCell cell = (GoalServiceCell)getChild(groupPos,i);
cell.expandAnimState = GoalServiceCell.ExpandAnimState.SHOULD_NOT_ANIMATE;
}
}
组视图也包含在AnimationAversrelativeLayout中。因为我已经将“shouldAnimate”设置为默认值false,所以我不需要触摸它们。我已经开始使用您的库,但发现一旦我开始添加复杂视图,它就会变得非常缓慢。我认为这可能是因为您正在设置页边距动画。有没有关于解决性能问题的建议?@KentAndersen实际上已经接受了一个pull请求,应该可以解决这个问题。您使用的是最新版本吗?如果您仍然有问题,请在github票证中详细解释。但是您没有使用ExpandableListView。我想为ExpandableListView而不是ListView的子视图设置动画。@TjerkW我正在尝试使用您的库,它似乎在触摸时启动,但没有任何可见的更改(该项未展开)日志条目如下所示:anim height-48
或0
;有什么想法吗?这是用于扩展listView单元格,而不是在ExpandableListView中扩展组这是用于扩展listView单元格,而不是在中扩展组ExpandableListView@havchr正确,但有时扩展项的内容可以放在常规项中,并且可以使用此解决方案。很好的解决方案,但这是针对ListView的,而不是像海报所问的可扩展ListView。@havchrExpandableListView
基本上是无用的(因为缺乏自定义功能),因此此解决方案旨在用一堆ListView
+ExpandableAdapter
替换ExpandableListView
。R.id.holder是什么?我应该通过什么考试?Thanks@AugustoPicciani将展开/折叠的视图的id。事实上,不要使用这个图书馆,它已经过时了。使用RecyclerView
谢谢,那么我应该使用哪个库呢?你能提供一个链接吗?再次感谢。我不得不禁用“折叠”动画,因为我从未让它工作过。当你只做扩展动画时,这是流氓吗?你确定这已经被设置为controller.setOrder(layouanimationcontroller.ORDER\u REVERSE);而不是顺序随机?您是否将if(mResultList.isGroupExpanded(groupPosition)){mProgAdap.preparetoclapposegroup(groupPosition);setupLayoutAnimationClose(groupPosition);mResultList.requestLayout();}
更改为if(mResultList.isGroupExpanded(groupPosition)){mResultList.collapseGroup(groupPosition);}
要正确禁用折叠动画吗?
void expandOrCollapse(GoalServiceCell cell,int position){
AnimationAverseRelativeLayout hack = (AnimationAverseRelativeLayout)master;
boolean shouldAnim = cell.expandAnimState == GoalServiceCell.ExpandAnimState.SHOULD_START_EXPAND ||
cell.expandAnimState == GoalServiceCell.ExpandAnimState.SHOULD_START_COLLAPSIN;
hack.setIfShouldAnimate(shouldAnim);
}