当我尝试滚动列表时,有时这不正确 - BottomSheet拦截滚动事件并隐藏。
如何重现:
结果:将隐藏BottomSheet。
以下是示例代码:
compile' com.android.support:design:23.4.0'
MainActivity.java
package com.nkdroid.bottomsheetsample;
import android.os.Bundle;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.TabLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public
class MainActivity
extends AppCompatActivity
{
private BottomSheetBehavior behavior;
@Override
protected
void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btnView = (Button) findViewById(R.id.btnView);
btnView.setOnClickListener(new View.OnClickListener()
{
@Override
public
void onClick(final View v) {
behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
});
final View bottomSheet = findViewById(R.id.bottom_sheet);
behavior = BottomSheetBehavior.from(bottomSheet);
final ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPager.setAdapter(new MyPagerAdapter());
final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private
class MyPagerAdapter
extends PagerAdapter
{
@Override
public
int getCount() {
return 15;
}
@Override
public
Object instantiateItem(final ViewGroup container, final int position) {
final RecyclerView recyclerView = new RecyclerView(MainActivity.this);
recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
recyclerView.setAdapter(new ItemAdapter());
container.addView(recyclerView);
return recyclerView;
}
@Override
public
boolean isViewFromObject(final View view, final Object object) {
return view.equals(object);
}
@Override
public
void destroyItem(final ViewGroup container, final int position, final Object object) {
container.removeView((View) object);
}
@Override
public
CharSequence getPageTitle(final int position) {
return String.valueOf(position);
}
}
public
class ItemAdapter
extends RecyclerView.Adapter<ItemAdapter.ViewHolder>
{
@Override
public
ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
return new ViewHolder(new TextView(MainActivity.this));
}
@Override
public
void onBindViewHolder(final ViewHolder holder, final int position) {
}
@Override
public
int getItemCount() {
return 100;
}
public
class ViewHolder
extends RecyclerView.ViewHolder
{
public TextView textView;
public
ViewHolder(final View itemView) {
super(itemView);
textView = (TextView) itemView;
}
}
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout android:id = "@+id/coordinatorLayout"
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:background = "#a3b1ef"
android:fitsSystemWindows = "true"
tools:context = ".ui.MainActivity"
>
<Button
android:id = "@+id/btnView"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:text = "Show view"
app:layout_behavior = "@string/appbar_scrolling_view_behavior"
/>
<LinearLayout
android:id = "@+id/bottom_sheet"
android:layout_width = "match_parent"
android:layout_height = "400dp"
android:background = "#fff"
android:gravity = "center"
android:orientation = "vertical"
app:layout_behavior = "@string/bottom_sheet_behavior"
>
<android.support.design.widget.TabLayout
android:id = "@+id/tabs"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
app:tabMode = "scrollable"
/>
<android.support.v4.view.ViewPager
android:id = "@+id/viewPager"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
/>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
任何解决方法的想法?
答案 0 :(得分:20)
我遇到了同样的限制,但能够解决它。
您所描述的效果的原因是BottomSheetBehavior
(自v24.2.0起)仅支持一个滚动子项,该子项在布局期间以下列方式标识:
private View findScrollingChild(View view) {
if (view instanceof NestedScrollingChild) {
return view;
}
if (view instanceof ViewGroup) {
ViewGroup group = (ViewGroup) view;
for (int i = 0, count = group.getChildCount(); i < count; i++) {
View scrollingChild = findScrollingChild(group.getChildAt(i));
if (scrollingChild != null) {
return scrollingChild;
}
}
}
return null;
}
您可以看到它实际上是使用DFS找到第一个滚动子项。
我略微增强了这个实现,并组装了一个小的库以及一个示例应用。你可以在这里找到它: https://github.com/laenger/ViewPagerBottomSheet
只需将maven repo url添加到build.gradle:
repositories {
maven { url "https://raw.github.com/laenger/maven-releases/master/releases" }
}
将库添加到依赖项:
dependencies {
compile "biz.laenger.android:vpbs:0.0.2"
}
使用ViewPagerBottomSheetBehavior
作为底部工作表视图:
app:layout_behavior="@string/view_pager_bottom_sheet_behavior"
在底部工作表中设置任何嵌套的ViewPager:
BottomSheetUtils.setupViewPager(bottomSheetViewPager)
(这在ViewPager 底部工作表视图和进一步嵌套的ViewPagers时也有效)
答案 1 :(得分:2)
还有另一种方法,不需要修改BottomSheetBehavior,而是利用BottomSheetBehavior仅识别找到的第一个NestedScrollView及其NestedScrollingEnabled的事实。因此,不要在BottomSheetBehavior中更改此逻辑,而是启用和禁用适当的滚动视图。我在这里发现了这种方法:https://imnotyourson.com/cannot-scroll-scrollable-content-inside-viewpager-as-bottomsheet-of-coordinatorlayout/
在我的情况下,我的BottomSheetBehavior使用的是带有FragmentPagerAdapter的TabLayout,因此我的FragmentPagerAdapter需要以下代码:
@Override
public void setPrimaryItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
super.setPrimaryItem(container, position, object);
Fragment f = ((Fragment)object);
String activeFragmentTag = f.getTag();
View view = f.getView();
if (view != null) {
View nestedView = view.findViewWithTag("nested");
if ( nestedView != null && nestedView instanceof NestedScrollView) {
((NestedScrollView)nestedView).setNestedScrollingEnabled(true);
}
}
FragmentManager fm = f.getFragmentManager();
for(Fragment frag : fm.getFragments()) {
if (frag.getTag() != activeFragmentTag) {
View v = frag.getView();
if (v!= null) {
View nestedView = v.findViewWithTag("nested");
if (nestedView!= null && nestedView instanceof NestedScrollView) {
((NestedScrollView)nestedView).setNestedScrollingEnabled(false);
}
}
}
}
container.requestLayout();
}
片段中的任何嵌套滚动视图都只需具有“嵌套”标签。
这是一个示例片段布局文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.MLeftFragment">
<androidx.core.widget.NestedScrollView
android:id="@+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="nested"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_mool_left_fragment" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
答案 2 :(得分:1)
看起来所需要做的就是适当地更新nestedScrollingChildRef
。
只需将其设置为target
中的onStartNestedScroll
参数即可:
package com.google.android.material.bottomsheet
class ViewPagerBottomSheetBehavior<V : View>(context: Context, attrs: AttributeSet?) : BottomSheetBehavior<V>(context, attrs) {
override fun onStartNestedScroll(coordinatorLayout: CoordinatorLayout, child: V, directTargetChild: View, target: View, axes: Int, type: Int): Boolean {
nestedScrollingChildRef = WeakReference(target)
return super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, axes, type)
}
}
答案 3 :(得分:0)
在底部工作表中显示针对 ViewPager 的修复程序。
package com.google.android.material.bottomsheet
import android.view.View
import androidx.annotation.VisibleForTesting
import androidx.viewpager.widget.ViewPager
import java.lang.ref.WeakReference
class BottomSheetBehaviorFix<V : View> : BottomSheetBehavior<V>(), ViewPager.OnPageChangeListener {
override fun onPageScrollStateChanged(state: Int) {}
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
override fun onPageSelected(position: Int) {
val container = viewRef?.get() ?: return
nestedScrollingChildRef = WeakReference(findScrollingChild(container))
}
@VisibleForTesting
override fun findScrollingChild(view: View): View? {
return if (view is ViewPager) {
view.focusedChild?.let { findScrollingChild(it) }
} else {
super.findScrollingChild(view)
}
}
}
答案 4 :(得分:0)
我有针对AndroidX的解决方案,Kotlin。 经过测试并正在使用'com.google.android.material:material:1.1.0-alpha06'。
我还使用了以下内容:MEDIUM BLOG作为指导。
这是我的ViewPagerBottomSheetBehavior Kotlin类:
package com.google.android.material.bottomsheet
import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.annotation.VisibleForTesting
import androidx.viewpager.widget.ViewPager
import java.lang.ref.WeakReference
class ViewPagerBottomSheetBehavior<V : View>
: com.google.android.material.bottomsheet.BottomSheetBehavior<V>,
ViewPager.OnPageChangeListener {
constructor() : super()
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
override fun onPageScrollStateChanged(state: Int) {}
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
override fun onPageSelected(position: Int) {
val container = viewRef?.get() ?: return
nestedScrollingChildRef = WeakReference(findScrollingChild(container))
}
@VisibleForTesting
override fun findScrollingChild(view: View?): View? {
return if (view is ViewPager) {
view.focusedChild?.let { findScrollingChild(it) }
} else {
super.findScrollingChild(view)
}
}
}
最终的解决方案是在类中添加超级构造函数:
constructor() : super()
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
请记住,您必须在下一个路径中添加 ViewPagerBottomSheetBehavior Kotlin类: Path Class Image reference,因为您必须覆盖私有方法>
@VisibleForTesting
override fun findScrollingChild(view: View?): View? {
return if (view is ViewPager) {
view.focusedChild?.let { findScrollingChild(it) }
} else {
super.findScrollingChild(view)
}
}
之后,您可以将其用作View属性,就像这样>
<androidx.constraintlayout.widget.ConstraintLayout
app:layout_behavior="com.google.android.material.bottomsheet.ViewPagerBottomSheetBehavior"
android:layout_height="match_parent"
android:layout_width="match_parent">
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="@layout/you_content_with_a_viewPager_scroll"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
答案 5 :(得分:0)
我最近也遇到过这种情况,我使用了以下自定义viewpager类,而不是XML上的viewpager类,它工作得很好,我认为它将对您和其他人有所帮助:
import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.viewpager.widget.ViewPager
import java.lang.reflect.Field
class BottomSheetViewPager(context: Context, attrs: AttributeSet?) : ViewPager(context, attrs) {
constructor(context: Context) : this(context, null)
private val positionField: Field =
ViewPager.LayoutParams::class.java.getDeclaredField("position").also {
it.isAccessible = true
}
init {
addOnPageChangeListener(object : SimpleOnPageChangeListener() {
override fun onPageSelected(position: Int) {
requestLayout()
}
})
}
override fun getChildAt(index: Int): View {
val stackTrace = Throwable().stackTrace
val calledFromFindScrollingChild = stackTrace.getOrNull(1)?.let {
it.className == "com.google.android.material.bottomsheet.BottomSheetBehavior" &&
it.methodName == "findScrollingChild"
}
if (calledFromFindScrollingChild != true) {
return super.getChildAt(index)
}
val currentView = getCurrentView() ?: return super.getChildAt(index)
return if (index == 0) {
currentView
} else {
var view = super.getChildAt(index)
if (view == currentView) {
view = super.getChildAt(0)
}
return view
}
}
private fun getCurrentView(): View? {
for (i in 0 until childCount) {
val child = super.getChildAt(i)
val lp = child.layoutParams as? ViewPager.LayoutParams
if (lp != null) {
val position = positionField.getInt(lp)
if (!lp.isDecor && currentItem == position) {
return child
}
}
}
return null
}
}
答案 6 :(得分:0)
假设page
是NestedScrollView
,我能够通过根据其是传入页面还是传出页面来切换其isNestedScrollingEnabled
属性来解决该问题。
val viewPager = findViewById<ViewPager>(R.id.viewPager)
viewPager.setPageTransformer(false) { page, position ->
if (position == 0.0f) {
page.isNestedScrollingEnabled = true
} else if (position % 1 == 0.0f) {
page.isNestedScrollingEnabled = false
}
}