Android: Scrolling issues with recyclerview inside a viewpager

折月煮酒 提交于 2019-12-05 09:58:54
public class CustomRecyclerView extends RecyclerView {
public CustomRecyclerView(Context context) {
    super(context);
}

public CustomRecyclerView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CustomRecyclerView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

int lastEvent = -1;

boolean isLastEventIntercepted = false;
private float xDistance, yDistance, lastX, lastY;

@Override
public boolean onInterceptTouchEvent(MotionEvent e) {
    switch (e.getAction()) {
        case MotionEvent.ACTION_DOWN:
            xDistance = yDistance = 0f;
            lastX = e.getX();
            lastY = e.getY();

            break;

        case MotionEvent.ACTION_MOVE:
            final float curX = e.getX();
            final float curY = e.getY();
            xDistance += Math.abs(curX - lastX);
            yDistance += Math.abs(curY - lastY);
            lastX = curX;
            lastY = curY;

            if (isLastEventIntercepted && lastEvent == MotionEvent.ACTION_MOVE) {
                return false;
            }

            if (xDistance > yDistance) {

                isLastEventIntercepted = true;
                lastEvent = MotionEvent.ACTION_MOVE;
                return false;
            }
    }

    lastEvent = e.getAction();

    isLastEventIntercepted = false;
    return super.onInterceptTouchEvent(e);
}

}

Just change your recyclerview by this .

I've done something very similar to this with a CollapsingToolbarLayout.

The trick here is to have two toolbars, one at the top with the searchview and another that is the toolbar dedicated to the collapsing layout.

Your searchview toolbar will have app:collapseMode="pin".

Your button bar will have app:collapseMode="pin".

Your LinearLayout below the button bar will have the default collapse mode so it will scroll. You can even give it a parallax effect with app:collapseMode="parallax".

Your TabLayout will have app:collapseMode="pin" so it scrolls up under the button bar and pins there.

Your ViewPager should be outside the CollapsingToolbarLayout and have the attribute app:layout_behavior="@string/appbar_scrolling_view_behavior".

The CollapsingToolbarLayout will want to display a title. Just call setTitle(null) on the collapsing toolbar and set your real title on the searchview toolbar.

You can put your navigation and menus on either the searchview toolbar and the collapsing toolbar and they should work as expected.

I will post some XML later; I just wanted to get this answer started.

It looks like you simply miss the app:layout_scrollFlags attribute. You should add it both to the Toolbar and your linear header layouts. For example, you could use: app:layout_scrollFlags="scroll|enterAlways"

As I see, your ViewPager already has app:layout_behavior="@string/appbar_scrolling_view_behavior", sou it should work right after you specify the scrollFlags

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!