gpt4 book ai didi

android - 动画 ListView 以增加大小

转载 作者:太空宇宙 更新时间:2023-11-03 12:53:03 26 4
gpt4 key购买 nike

当我滚动 ListView 时,我试图让动画工作。我想要的是,当用户向下滚动以查看列表中的更多条目时,其上方的布局 (A RelativeLayout) 将缩小大小。我希望 RelativeLayout 只占屏幕的四分之一,滚动时只占屏幕的一半。

我现在的代码很不稳定,看起来很糟糕。我需要一个更好的解决方案来解决这个问题

这是我目前的代码

@Override
public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) {
if(mLastFirstVisibleItem < firstVisibleItem){
Display display = getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int screenHeight = size.y;

System.out.println("Screen Height / 2: " + (screenHeight / 2));
System.out.println("Balance Height 1: " + relBal.getHeight());
if(relBal.getHeight() > (screenHeight / 2)){
newBalHeight = (screenHeight / 2);
}
else {
newBalHeight = relBal.getHeight() + 100;
}

System.out.println("Balance Height 2: " + newBalHeight);

ResizeAnimation resize = new ResizeAnimation(relBal, relBal.getWidth(), relBal.getHeight(), relBal.getWidth(), newBalHeight);
relBal.startAnimation(resize);
}
if(mLastFirstVisibleItem > firstVisibleItem){
Display display = getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int screenHeight = size.y;

System.out.println("Screen Height / 4: " + (screenHeight / 4));
System.out.println("Balance Height: " + relBal.getHeight());
if(relBal.getHeight() < (screenHeight / 4)){
newBalHeight = (screenHeight / 4);
}
else {
newBalHeight = relBal.getHeight() - 100;
}

System.out.println("Balance Height 2: " + newBalHeight);
ResizeAnimation resize = new ResizeAnimation(relBal, relBal.getWidth(), relBal.getHeight(), relBal.getWidth(), newBalHeight);
relBal.startAnimation(resize);
}
mLastFirstVisibleItem = firstVisibleItem;

这是我的页面的样子

enter image description here

当我滚动查看更多项目时,我希望它看起来像这样

enter image description here

最佳答案

我在 https://github.com/ramanadv/ShrinkingHeader 上放了一个示例项目.下面是我使用的逻辑,它有点不稳定,因为我实际上并没有太在意那里。这只是给你一个方向。不要使用动画,因为 OnScroll 被调用的次数足够多,可以为您提供平滑的高度变化过渡。

    list.setOnScrollListener(new OnScrollListener() {

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {

}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) {
int scroll = getScroll(list);
changeHeight(price,scroll);
System.out.println("scroll "+scroll);
}
});

}

protected int getScroll(ListView listView) {// as list recycles views , getscrollY wont give us how much it has scrolled, hence we use this hack
firstChildInList = listView.getChildAt(0);
if(firstChildInList == null)return 0;
return -firstChildInList.getTop() + listView.getFirstVisiblePosition() * firstChildInList.getHeight();
}

protected void setHeightForView(View v ,int h){ // you need to set params to change height
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)v.getLayoutParams();
params.height = h;
v.setLayoutParams(params);
}
protected void changeHeight(View view,int scroll) { // this is a simple logic , this is a little shaky , but its simple , you can smoothen from here
int priceHeight = price.getHeight();
if(priceHeight>=screenHeight/4 && priceHeight<=screenHeight/2){
setHeightForView(view, screenHeight/2-scroll);
}
else if(priceHeight < screenHeight/4){

}
}

关于android - 动画 ListView 以增加大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26637982/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com