有没有办法在 Android 中展开可扩展列表时添加动画效果?
我希望当用户点击可扩展列表时,它具有类似于打开滑动抽屉的动画/效果。
它会缓慢移动,直到完全打开。
有没有办法在 Android 中展开可扩展列表时添加动画效果?
我希望当用户点击可扩展列表时,它具有类似于打开滑动抽屉的动画/效果。
它会缓慢移动,直到完全打开。
所以,我创建了自己的ListAdapter
,将整个视图缓存成一个Bitmap
,然后对缓存的视图执行动画,而不是直接操作视图本身。 这样可以更快速地运行。
在这里:https://github.com/dmitry-zaitsev/ExpandableAdapter
好消息是,你不需要重写大量代码 - 只需在你的适配器周围包装我的ExpandableAdapter
,并提供充当切换按钮的视图的ID和保存第二级内容的视图的ID:
new ExpandableAdapter(context, yourAdapter, R.id.switch, R.id.holder);
这就是全部。
RecyclerView
。 - Dmitry ZaytsevListAdapter
包装成一个SlideExpandableListAdapter
。包装器会为你的ListView
添加幻灯片功能及动画效果。anim height -48
或0
;有什么想法吗? - amcc 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);
}
});
@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;
}
}
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,因此不需要对它们进行操作。
if(mResultList.isGroupExpanded(groupPosition)){ mResultList.collapseGroup(groupPosition); }
以正确禁用折叠动画?原代码如下:if(mResultList.isGroupExpanded(groupPosition)){
mProgAdap.prepareToCollapseGroup(groupPosition);
setupLayoutAnimationClose(groupPosition);
mResultList.requestLayout();
}
- havchrListView
,然后在onListItemClick中执行动画。
该动画类似于我在此链接Android animate drop down/up view proper中所做的内容。<somelayout>
<normal>
</normal>
<expanded>
</expanded>
</somelayout>
正常情况下不使用扩展。当激活扩展时,扩展将设置为可见而不是隐藏。在关闭时需要保持控制并将其再次设置为隐藏(请记住这是在您的转换视图中设置的,这些视图会被回收利用)。
如果需要,我可以进一步澄清,但需要放入相当多的代码。
在这种情况下,这就是我所做的。
ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(list1, PropertyValuesHolder.ofInt("bottom", currentlistHeight,currentlistHeight*2 ));
这将使listView的高度加倍,并且会动画化。 如果您将当前列表高度设置为ZERO,则会像抽屉一样运作。