gpt4 book ai didi

安卓 : Hide RelativeLayout with animationwhen scroll down/up listview

转载 作者:太空狗 更新时间:2023-10-29 15:03:06 25 4
gpt4 key购买 nike

我尝试实现一个动画来隐藏 ListView 上方的 relativeLayout。我想要这种行为:

  • 向下滚动时:隐藏 RelativeLayout
  • 向上滚动:仅当我在顶部时才显示 RelativeLayout

enter image description here

我创建的以下代码有效,但有 2 个问题:

  1. 当我向下滚动或向上滚动时,如果我位于 ListView 的顶部,则相关布局会闪烁。为了隐藏这次攀登,我必须进行一次快速滚动。 => 我的代码有问题,可能是在 listIsAtTop() 函数上?
  2. 第二个问题,我没有动画。我使用 setVisivility(VISIBLE 或 GONE) 来显示或隐藏 relativeLayout。它不是用户友好的,不是吗? :) 我不知道如何添加一点翻译动画来隐藏和显示这个 relativeLayout。

在 Account_Activity 类的 onCreate() 中:

list = (com.app.frisbeee.account_view.CustomListView)findViewById(R.id.accountview_listview);
adapter = new Account_view_custom_adapter(Account_view_act.this, annoncesList);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
registerForContextMenu(list);

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
list.showContextMenuForChild(view);
}
});

list.setOnDetectScrollListener(new OnDetectScrollListener() {
@Override
public void onUpScrolling() {
/* do something */
Log.d("INFO", "UPPPPPPPPPPP");
if (listIsAtTop()) {
RelativeLayout relative1 = (RelativeLayout) findViewById(R.id.relative1);
relative1.setVisibility(View.VISIBLE);
}
}

@Override
public void onDownScrolling() {
/* do something */
Log.d("INFO", "DOWNNNNNNNN");
if (!listIsAtTop()) {
RelativeLayout relative1 = (RelativeLayout) findViewById(R.id.relative1);
relative1.setVisibility(View.GONE);
}

}
});

}

listIsAtTop 函数代码:

private boolean listIsAtTop()   {   
if (list.getChildCount() == 0) {
return true;
}
return list.getFirstVisiblePosition() == 0 && (list.getChildCount() == 0 || list.getChildAt(0).getTop() == 0);
}

CustomListView 用于检测向上和向下滚动:

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AbsListView;

public class CustomListView extends android.widget.ListView {

private OnScrollListener onScrollListener;
private OnDetectScrollListener onDetectScrollListener;

public CustomListView(Context context) {
super(context);
onCreate(context, null, null);
}

public CustomListView(Context context, AttributeSet attrs) {
super(context, attrs);
onCreate(context, attrs, null);
}

public CustomListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
onCreate(context, attrs, defStyle);
}

private void onCreate(Context context, AttributeSet attrs, Integer defStyle) {
setListeners();
}

private void setListeners() {
super.setOnScrollListener(new OnScrollListener() {

private int oldTop;
private int oldFirstVisibleItem;

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (onScrollListener != null) {
onScrollListener.onScrollStateChanged(view, scrollState);
}
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (onScrollListener != null) {
onScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
}

if (onDetectScrollListener != null) {
onDetectedListScroll(view, firstVisibleItem);
}
}

private void onDetectedListScroll(AbsListView absListView, int firstVisibleItem) {
View view = absListView.getChildAt(0);
int top = (view == null) ? 0 : view.getTop();

if (firstVisibleItem == oldFirstVisibleItem) {
if (top > oldTop) {
onDetectScrollListener.onUpScrolling();
} else if (top < oldTop) {
onDetectScrollListener.onDownScrolling();
}
} else {
if (firstVisibleItem < oldFirstVisibleItem) {
onDetectScrollListener.onUpScrolling();
} else {
onDetectScrollListener.onDownScrolling();
}
}

oldTop = top;
oldFirstVisibleItem = firstVisibleItem;
}
});
}

@Override
public void setOnScrollListener(OnScrollListener onScrollListener) {
this.onScrollListener = onScrollListener;
}

public void setOnDetectScrollListener(OnDetectScrollListener onDetectScrollListener) {
this.onDetectScrollListener = onDetectScrollListener;
}
}

最佳答案

对 listIsAtTop() 函数试试这个:

private boolean listIsAtTop()   {   
if(listView.getChildCount() == 0) return true;
return listView.getChildAt(0).getTop() == 0;
}

关于滑动动画:

slide_down.xml 代码:

<set xmlns:android="http://schemas.android.com/apk/res/android">  
<translate android:fromXDelta="0" android:fromYDelta="-1000" android:duration="1500"/>
</set>

slide_up.xml 代码:

<set xmlns:android="http://schemas.android.com/apk/res/android">  
<translate android:fromXDelta="0" android:fromYDelta="1000" android:duration="1500"/>
</set>

然后在onCreate方法中加载动画:

Animation slideUp = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_up);

然后将它附加到您的 RelativeLayout:

relative1.startAnimation(slideUp);

希望这对您有所帮助:)

关于安卓 : Hide RelativeLayout with animationwhen scroll down/up listview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24560139/

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