内部回收视图位于第一个位置并被下拉时滚动协调器布局

Scroll coordinatorlayout when inner recyclerview is at first position and is being pulled down

我有以下 activity 的布局。

<android.support.design.widget.CoordinatorLayout>
    <View/>
    <FrameLayout
    android:id="@+id/fragment_container"
    app:layout_behavior="@string/bottom_sheet_behavior"/>
</android.support.design.widget.CoordinatorLayout>

我有包含垂直 RecyclerView 的片段。这充当另一个水平滚动的 recyclerview 的容器。片段布局为:

<RelativeLayout>
<android.support.v7.widget.RecyclerView
    android:id="@+id/wall_rooms"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="@dimen/app_bar_height"
    android:background="@color/pinkDark"
    android:overScrollMode="never" />
</RelativeLayout>

当 RecyclerView 在顶部显示第一个项目并且用户向下滚动回收器视图时,协调器布局应该滚动内容。如果 RecyclerView 显示其他项目,向下滚动应该滚动 RecyclerView 的内容。

我尝试使用 setNestedScrollingEnabled = false,但 RecyclerView 中的项目不滚动。

在花了很多时间,尝试了不同的东西之后,终于让它工作了。扩展 BottomSheetBehavior class 并覆盖 onInterceptTouchEvent 方法解决了它。发布它希望它能帮助某人。

public class MyBottomsheetBehaviour extends BottomSheetBehavior {

    int lastY = 0;

    MyBottomsheetBehaviour() {
        super();
    }

    @Keep
    public MyBottomsheetBehaviour(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onInterceptTouchEvent(CoordinatorLayout parent, View view, MotionEvent event) {

        int dy = 0;
        boolean shouldWeConsumeIt = false;

        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            lastY = (int) event.getY();
        } else if (event.getAction() == MotionEvent.ACTION_CANCEL || event.getAction() == MotionEvent.ACTION_UP) {
            lastY = 0;
        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
            dy = (int) event.getY() - lastY;
        }
        Log.d("Swapnil", "onInterceptTouchEvent() event Y=" + event.getY() + " x=" + event.getX() + getActionType(event.getActionMasked()) + view.getClass().getSimpleName() + "dy=" + dy);
        if (view instanceof FrameLayout) {

            View child = ((ViewGroup) ((FrameLayout) view).getChildAt(0)).getChildAt(0);
            LinearLayoutManager manager = (LinearLayoutManager) ((RecyclerView) child).getLayoutManager();

            if (manager.findFirstCompletelyVisibleItemPosition() == 0 && dy > 0) {

                shouldWeConsumeIt = true;
                int parentHeight = view.getHeight();

                ViewCompat.offsetTopAndBottom(view, dy);
            } else if (manager.findFirstCompletelyVisibleItemPosition() == 0) {
                int height = ((View) view.getParent()).getHeight();
                int top = view.getTop();
                if ((event.getAction() == MotionEvent.ACTION_CANCEL || event.getAction() == MotionEvent.ACTION_UP) && top < (height >> 2)) {
                    view.setTop(0);
                    setState(BottomSheetBehavior.STATE_EXPANDED);
                } else if ((event.getAction() == MotionEvent.ACTION_CANCEL || event.getAction() == MotionEvent.ACTION_UP)) {
                    setState(BottomSheetBehavior.STATE_COLLAPSED);
                }
            }
        }
        if (event.getAction() == MotionEvent.ACTION_MOVE) {
            lastY = (int) event.getY();
        }
        if (!shouldWeConsumeIt)
            return super.onInterceptTouchEvent(parent, view, event);

        return true;
    }

    @Override
    public boolean onTouchEvent(CoordinatorLayout parent, View child, MotionEvent event) {
        return super.onTouchEvent(parent, child, event);
    }

    private String getActionType(int eventType) {

        StringBuilder builder = new StringBuilder();
        if (eventType == MotionEvent.ACTION_DOWN)
            builder.append(" DOWN");
        else if ((eventType & MotionEvent.ACTION_UP) == MotionEvent.ACTION_UP)
            builder.append(" UP");
        else if ((eventType & MotionEvent.ACTION_CANCEL) == MotionEvent.ACTION_CANCEL)
            builder.append(" CANCEL");
        else if ((eventType & MotionEvent.ACTION_MOVE) == MotionEvent.ACTION_MOVE)
            builder.append(" MOVE");

        return builder.toString();
    }
}