我正在尝试使用AndroidSlidingUpPanel复制谷歌地图。
我的问题是面板无法拖动,只是列表视图正在滚动。
正如您在google地图中看到的那样。在面板完全展开之前,列表视图不可滚动。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="@color/app_secondary_color"
app:contentInsetLeft="14dp"
app:contentInsetRight="14dp"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<com.sothree.slidinguppanel.SlidingUpPanelLayout xmlns:sothree="http://schemas.android.com/apk/res-auto"
android:id="@+id/sliding_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar"
android:gravity="bottom"
sothree:dragView="@+id/dragView"
sothree:panelHeight="140dp"
sothree:paralaxOffset="100dp"
sothree:shadowHeight="4dp">
<FrameLayout
android:id="@+id/maps_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
<RelativeLayout
android:id="@+id/dragView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="false">
<ListView
android:id="@+id/hospital_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
</com.sothree.slidinguppanel.SlidingUpPanelLayout>
</RelativeLayout>
正如JASON G PETERSON所说
这是我试过的
更新
public class CustomListView extends ListView {
private boolean interceptTouch = true;
public void InterceptTouch(boolean bool) {
interceptTouch = bool;
}
public CustomListView(Context context) {
super(context);
}
public CustomListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public CustomListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if(interceptTouch){
return false;
}else{
return super.onTouchEvent(ev);
}
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if(interceptTouch){
return false;
}else{
return super.onInterceptTouchEvent(event);
}
}
}
关于我的活动
mLayout.setPanelSlideListener(new SlidingUpPanelLayout.PanelSlideListener() {
@Override
public void onPanelSlide(View view, float v) {
}
@Override
public void onPanelCollapsed(View view) {
LogUtil.d("COLLAPSED");
mListView.InterceptTouch(true);
}
@Override
public void onPanelExpanded(View view) {
LogUtil.d("EXPANDED");
mListView.InterceptTouch(false);
}
@Override
public void onPanelAnchored(View view) {
}
@Override
public void onPanelHidden(View view) {
}
});
问题是mListView.InterceptTouch(false);当你快速滚动面板崩溃时,特别是工作不正常。
我只是希望在第一行显示第一行并且用户向下滚动时面板崩溃,就像拉动刷新效果一样。有什么帮助吗?
//更新3
我添加了这个。但拖动时面板不会滑动。我需要再次拖动然后面板滑动生效。为什么呢?
mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
int cFirstVisibleItem = 0;
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
interceptScroll(view);
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (view.getChildCount() > 0) {
cFirstVisibleItem = firstVisibleItem;
LogUtil.d("GET firstVisibleItem :" + firstVisibleItem);
LogUtil.d("GET Y :" + view.getChildAt(0).getY());
interceptScroll(view);
}
}
public void interceptScroll(AbsListView view) {
if (view.getChildAt(0).getY() == 0 && cFirstVisibleItem == 0) {
mLayout.setSlidingEnabled(true);
mListView.InterceptTouch(true);
LogUtil.d("intercept");
} else {
mListView.InterceptTouch(false);
mLayout.setSlidingEnabled(false);
LogUtil.d("dont intercept");
}
}
});
答案 0 :(得分:1)
我怀疑这是因为你的ListView首先消耗了触摸事件。您可以扩展ListView,然后添加条件,以便何时以及何时不将触摸事件传递给滑动面板,其中包含以下代码段:
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (/*Your conditions.*/) {
return false;//Don't intercept. Let sliding panel get touch.
}
return super.onInterceptTouchEvent(event); //Don't let sliding panel get touch.
}