gpt4 book ai didi

android - BottomSheet 中的 ListView

转载 作者:可可西里 更新时间:2023-11-01 18:49:55 25 4
gpt4 key购买 nike

我遇到了一个问题,我在 BottomSheet 中有一个简单的 ListView 并且 ListView 有足够的项目来填满屏幕甚至滚动更多。

当我向下滚动时,一切似乎都正常,但是当我尝试向上滚动时,它正在滚动 BottomSheet 本身并关闭 View ,而不是仅仅滚动 ListView.

一段时间后我找到了解决方案,但由于我在这里找不到它,所以我想我会把它张贴在这里。

最佳答案

解决方案是像这样扩展 ListView:

public class BottomSheetListView extends ListView {
public BottomSheetListView (Context context, AttributeSet p_attrs) {
super (context, p_attrs);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return true;
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
if (canScrollVertically(this)) {
getParent().requestDisallowInterceptTouchEvent(true);
}
return super.onTouchEvent(ev);
}

public boolean canScrollVertically (AbsListView view) {
boolean canScroll = false;

if (view !=null && view.getChildCount ()> 0) {
boolean isOnTop = view.getFirstVisiblePosition() != 0 || view.getChildAt(0).getTop() != 0;
boolean isAllItemsVisible = isOnTop && view.getLastVisiblePosition() == view.getChildCount();

if (isOnTop || isAllItemsVisible) {
canScroll = true;
}
}

return canScroll;
}
}

然后在你的布局文件bottom_sheet_view.xml中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.mypackage.name.BottomSheetListView
android:id="@+id/listViewBtmSheet"
android:divider="@color/colorPrimary"
android:dividerHeight="1dp"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp" />

</LinearLayout>

最后,在您的 Activity/Fragment 中:

BottomSheetDialog dialog = new BottomSheetDialog(context);
dialog.setContentView(R.layout.bottom_sheet_view);

BottomSheetListView listView = (BottomSheetListView) dialog.findViewById(R.id.listViewBtmSheet);
// apply some adapter - add some data to listview

dialog.show();

这将提供一个完全与 ListView 滚动一起工作的 BottomSheet

关于android - BottomSheet 中的 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40570985/

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