gpt4 book ai didi

android - SwipeRefreshLayout 干扰 setOnScrollListener

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:05:12 25 4
gpt4 key购买 nike

每次我向上或向下滚动列表时,我都会使用 OnScrollListener 隐藏(或取消隐藏)一些 View 。这是附加到我的 ListView 的代码。

lv.setOnScrollListener(new OnScrollListener() {

private int mLastFirstVisibleItem;
private boolean mIsScrollingUp = true;
private LinearLayout ll = (LinearLayout) getActivity()
.findViewById(R.id.llSearchPlaces);

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

}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {



if (view.getId() == lv.getId()) {
final int currentFirstVisibleItem = lv
.getFirstVisiblePosition();
if (currentFirstVisibleItem > mLastFirstVisibleItem) {

if (mIsScrollingUp == true) {
mIsScrollingUp = false;
Log.i("a", "scrolling down...");

floatingActionButton.hide();

Animation animation = new TranslateAnimation(0, 0,
0, 200);
animation.setDuration(300);
animation
.setAnimationListener(new AnimationListener() {

@Override
public void onAnimationEnd(
Animation animation) {
ll.setVisibility(View.INVISIBLE);
}

@Override
public void onAnimationRepeat(
Animation animation) {
}

@Override
public void onAnimationStart(
Animation animation) {
}
});

ll.startAnimation(animation);
}

} else if (currentFirstVisibleItem < mLastFirstVisibleItem) {

if (mIsScrollingUp == false) {
mIsScrollingUp = true;
floatingActionButton.show();
Log.i("a", "scrolling up...");

Animation animation = new TranslateAnimation(0, 0,
200, 0);
animation.setDuration(400);
animation
.setAnimationListener(new AnimationListener() {

@Override
public void onAnimationEnd(
Animation animation) {

}

@Override
public void onAnimationRepeat(
Animation animation) {

}

@Override
public void onAnimationStart(
Animation animation) {
ll.setVisibility(View.VISIBLE);

}
});

ll.startAnimation(animation);

}
}

mLastFirstVisibleItem = currentFirstVisibleItem;

}
}
});

布局:

<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fadeScrollbars="true"
android:listSelector="#00000000"
android:scrollbars="none" />

</android.support.v4.widget.SwipeRefreshLayout>

自从我添加了 SwipeRefreshLayout 之后,当我在上面的监听器中 Log 时,我什么也得不到。如何同时使用这两个项目?

编辑:好像this是我需要的,但我不能让它继续工作

作为那篇文章的一部分,我在 onScroll 中添加了它,但它似乎不起作用。

        if (firstVisibleItem == 0) {
swipeLayout.setEnabled(true);
} else {
swipeLayout.setEnabled(false);
}

EDIT2:这是问题的核心:似乎 onScroll 方法在 Activity 首次启动和加载列表时触发,然后再也不会触发。

最佳答案

我对 RecyclerViewListView 也有同样的问题。向下滚动无论项目数量如何,都无法返回到列表顶部。

这将禁用 SwipeRefreshLayout,直到第一个可见项目或任何项目位置可见。您还可以沿着这个绑定(bind)不同的滚动监听器。确保在重新填充列表时启用(如果之前禁用)SwipeRefreshLayout

public class SwipeRefreshLayoutToggleScrollListenerListView implements AbsListView.OnScrollListener {
private List<AbsListView.OnScrollListener> mScrollListeners = new ArrayList<AbsListView.OnScrollListener>();
private int mExpectedVisiblePosition = 0;

public SwipeRefreshLayoutToggleScrollListenerListView(SwipeRefreshLayout mSwipeLayout) {
this.mSwipeLayout = mSwipeLayout;
}

private SwipeRefreshLayout mSwipeLayout;
public void addScrollListener(AbsListView.OnScrollListener listener){
mScrollListeners.add(listener);
}
public boolean removeScrollListener(AbsListView.OnScrollListener listener){
return mScrollListeners.remove(listener);
}
public void setExpectedFirstVisiblePosition(int position){
mExpectedVisiblePosition = position;
}

private void notifyOnScrolled(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount){
for(AbsListView.OnScrollListener listener : mScrollListeners){
listener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
}
}
private void notifyScrollStateChanged(AbsListView view, int scrollState){
for(AbsListView.OnScrollListener listener : mScrollListeners){
listener.onScrollStateChanged(view, scrollState);
}
}

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

@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
notifyOnScrolled(view, firstVisibleItem, visibleItemCount, totalItemCount);
if(firstVisibleItem != RecyclerView.NO_POSITION)
mSwipeLayout.setEnabled(firstVisibleItem == mExpectedVisiblePosition);
}
}

编辑:

lv.setOnScrollListener(new SwipeRefreshLayoutToggleScrollListenerListView(mSwiperLayout){
//override methods here, don't forget the super calls.
});

关于android - SwipeRefreshLayout 干扰 setOnScrollListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27464351/

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