gpt4 book ai didi

安卓 : Hide relativelayout when scroll down on listview

转载 作者:行者123 更新时间:2023-11-29 00:18:57 25 4
gpt4 key购买 nike

当用户开始在 ListView 中向下滚动时,我尝试添加一个相对布局。当用户滚动到顶部并且显示 ListView 的第一项时,我想再次显示 relativelayout。

我的代码可以工作,但是当用户尝试滚动时会有一点“爬升”。我认为问题出在 listAtTop 函数上,但我不知道出在哪里。

enter image description here

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);
}

}
});


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

最佳答案

试试这个

 list.setOnScrollListener(new OnScrollListener() {

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
}
int mPosition=0;
int mOffset=0;

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
int position = list.getFirstVisiblePosition();
View v = list.getChildAt(0);
int offset = (v == null) ? 0 : v.getTop();

if (mPosition < position || (mPosition == position && mOffset < offset)){
// Scrolled up
//search_layout.setVisibility(View.GONE);

} else {
// Scrolled down
//search_layout.setVisibility(View.VISIBLE);

}
}
});

在这段代码中,当您向下滚动时隐藏布局,当您向上滚动并到达顶部时隐藏布局

关于安卓 : Hide relativelayout when scroll down on listview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24386418/

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