BottomSheet中的ListView [英] ListView in BottomSheet
问题描述
我遇到一个问题,我在BottomSheet
中有一个简单的ListView
,而ListView
的项目足以填满屏幕并滚动更多.
I ran into a problem where I had a simple ListView
in a BottomSheet
and ListView
had enough items to fill the screen and scroll even more.
当我向下滚动时,一切似乎都正常,但是当我尝试向上滚动时,它滚动的是BottomSheet
本身并关闭视图,而不仅仅是滚动的ListView
.
When I scroll down, everything seems to work however when I tried to scroll back up, it was scrolling the BottomSheet
itself and closing the view instead of just scrolling the ListView
.
过了一会儿我就找到了解决方案,因为我在这里找不到任何地方,所以我想将其发布在这里.
I was able to find a solution after a while and since I couldn't find it anywhere here, I figured I would post it here.
推荐答案
解决方案是像这样扩展ListView
:
The solution is to extend the ListView
like this:
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
.
This will provide a BottomSheet
that is fully working with ListView
scroll.
这篇关于BottomSheet中的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!