在Android中打开可扩展列表时有没有办法添加动画?我想要它,以便当用户单击可扩展列表时,它具有动画/效果,就像我正在打开滑动抽屉一样。
它移动缓慢,直到完全打开。
在Android中打开可扩展列表时有没有办法添加动画?我想要它,以便当用户单击可扩展列表时,它具有动画/效果,就像我正在打开滑动抽屉一样。
它移动缓慢,直到完全打开。
我花了很多时间寻找没有运气。现有的解决方案不够流畅 - 如果您的布局比 2 个按钮更复杂,它就会变得滞后。
因此,我创建了自己ListAdapter
的将整个视图缓存到 a 中Bitmap
,然后在缓存的视图而不是视图本身上执行动画。它工作得更快。
这是:https ://github.com/dmitry-zaitsev/ExpandableAdapter
好消息是您不需要重写一堆代码 - 只需将 my 包裹ExpandableAdapter
在您的适配器周围并提供将充当切换按钮的视图的 id 和保存第二个内容的视图的 id等级:
new ExpandableAdapter(context, yourAdapter, R.id.switch, R.id.holder);
仅此而已。
我有同样的问题。我一劳永逸地修复了它。我把它开源到 github。 https://github.com/tjerkw/Android-SlideExpandableListView
基本上,您将这个项目依赖项包含在您的 Android 项目中。然后将你的包装ListAdapter
成一个SlideExpandableListAdapter
. 然后,包装器会将带有动画的幻灯片功能添加到您的ListView
.
希望它可以帮助你,我已经在两个项目中使用它。
我也尝试过使这项工作。我找到了一种适用于 childViews 的解决方案。虽然它不会为组的实际扩展设置动画,但会在子单元格填充扩展留下的空间时为它们设置动画。
编辑:折叠中有一个错误,这会使一些不应该隐藏的单元格变为隐藏。这可能与listView中的View-recycling有关。当我有解决方案时,我会更新。
在 setOnGroupClickListener 中使用 layoutAnimation 制作动画
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);
}
});
我们需要更多的调整以使动画仅适用于展开/折叠组的实际子项。因为我们无法在LayoutAnimationController中重载正确的部分,所以我们需要创建一个特殊的ViewGroup类。这与“Can LayoutAnimationController 只能为指定的视图设置动画”中的技术相同。
在 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;
}
}
并在孩子们的 ViewHolder 中。
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);
}
GroupViews 也包含在 AnimationAverseRelativeLayout 中。由于我已将“shouldAnimate”设置为默认为 false,因此我不需要触摸它们。
所以我所做的是使用常规ListView
,然后在 onListItemClick 中执行动画。动画类似于我在这个链接上所做的:Android animate drop down/up view proper
但仅适用于行视图的一部分。行视图在 xml 中以下列方式实现:
<somelayout>
<normal>
</normal>
<expanded>
</expanded>
</somelayout>
法线在没有展开的情况下使用。当扩展被激活时,扩展被设置为可见而不是消失。您需要控制在关闭时将其设置为再次消失(请记住,这是在您的可回收的转换视图中设置的)。
如果需要,我可以进一步澄清,只是需要输入很多代码。
这就是我在这种情况下所做的。
ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(list1, PropertyValuesHolder.ofInt("底部", currentlistHeight,currentlistHeight*2 ));
这样做的结果是 listView 的高度将加倍,并且会被动画化。如果您设置当前列表高度ZERO。这就像一个抽屉。