通过滚动事件另一种观点 [英] Pass scroll event to another view

查看:181
本文介绍了通过滚动事件另一种观点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Android的设计支持库崩溃工具栏布局。 一切正常,只是我想要滚动整个视图上滚动collapsingBarlayout含量

I have been using android design support library for collapsing toolbar layout. Everything works fine except that I want to scroll the whole view on scrolling content in collapsingBarlayout

这里的XML布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">


    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="420dp"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            android:fitsSystemWindows="true">

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_scrollFlags="scroll|exitUntilCollapsed"
                app:contentScrim="?attr/colorPrimary"
                app:expandedTitleMarginStart="48dp"
                app:expandedTitleMarginEnd="64dp"
                android:fitsSystemWindows="true">

                <android.support.v4.view.ViewPager
                    xmlns:android="http://schemas.android.com/apk/res/android"
                    android:id="@+id/pager"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:scaleType="centerCrop"
                    app:layout_collapseMode="parallax"
                    app:layout_collapseParallaxMultiplier="0.8"
                    android:fitsSystemWindows="true"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior"
                    />

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                    app:layout_collapseMode="pin" />

            </android.support.design.widget.CollapsingToolbarLayout>

        </android.support.design.widget.AppBarLayout>

        <android.support.v4.widget.NestedScrollView
            android:id="@+id/scroll_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <include layout="@layout/activity_item_detail"
                />

        </android.support.v4.widget.NestedScrollView>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/activity_vertical_margin"
            android:clickable="true"
            android:src="@android:drawable/ic_menu_call"
            app:layout_anchor="@+id/appbar"
            app:layout_anchorGravity="bottom|right|end"
            style="@style/floating_action_button"
            />

    </android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/drawer"/>
</android.support.v4.widget.DrawerLayout>

滚动内容的工作,如果触及到任何东西,除了viewpager /工具栏。 我需要触摸和查看传呼机垂直滚动也同样的滚动体验。 曾试图重写来看寻呼机的onTouchEvent通过ACTION_UP运动事件scroll_view。

Scrolling the content works if touched on anything except viewpager/toolbar. I need the same scroll experience on touching and scrolling vertically on view pager also. Had tried overriding onTouchEvent of view pager to pass ACTION_UP motion event to scroll_view.

nestedScrollView.post(new Runnable() {
    @Override
    public void run() {
        mPager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    return nestedScrollView.onTouchEvent(event);
                } else {
                    return v.onTouchEvent(event);
                }
            }
        });
    }
});

不过,这是行不通的。请帮助一个解决方法/任何黑客工具,可以迅速解决这个问题了。

But it doesn't work. Please help with a workaround/any hack that can quickly fix this up.

推荐答案

找出哪些正在接收触摸事件的看法。在这种特殊情况下ViewPager接收我们希望覆盖触摸事件。一旦你拥有了它接收触摸事件的视图中,可以安装一个手势检测。看看这个指南,特别是在

Find out the view which is receiving the touch event. In this specific case ViewPager is receiving the touch event which we want to override. Once you have the View which is receiving touch event, you can install a Gesture Detector. Take a look at this guide, in particular at the

检测支持手势的一个子集

Detecting a Subset of Supported Gestures

在这种方式,您可以管理所有的在外视图下的姿态,你可以作出反应以某种方式的手势。
注意:当您扩展 GestureDetector.SimpleOnGestureListener 记得覆盖 onDown 方法,否则你不会得到任何其它事件。

In this way you can manage all the gesture that are performed on the outer view and you can react to the gesture in some way.
Note: when you extend the GestureDetector.SimpleOnGestureListener remember to override the onDown method, otherwise you won't get any other event.

我已经在这个 github上项目测试了我的建议。如果修改了CheeseDetailActivity这样,你就当你滚动奶酪图像内拿到称为onScroll事件。

I've tested my suggestion on this github project. If you modify the CheeseDetailActivity in this way, you will get the onScroll event called when you scroll inside the cheese image.

public class CheeseDetailActivity extends AppCompatActivity {

    public static final String EXTRA_NAME = "cheese_name";

    private GestureDetectorCompat mDetector;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);

        MyGestureListener listener = new MyGestureListener();
        mDetector = new GestureDetectorCompat(this, new MyGestureListener());
        View root = findViewById(R.id.pager);
        root.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return mDetector.onTouchEvent(event);
            }
        });


        Intent intent = getIntent();
        final String cheeseName = intent.getStringExtra(EXTRA_NAME);

        final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        CollapsingToolbarLayout collapsingToolbar =
                (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
        collapsingToolbar.setTitle(cheeseName);

        loadBackdrop();
    }

    private void loadBackdrop() {
        final ImageView imageView = (ImageView) findViewById(R.id.backdrop);
        Glide.with(this).load(Cheeses.getRandomCheeseDrawable()).centerCrop().into(imageView);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.sample_actions, menu);
        return true;
    }

    class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
        private static final String DEBUG_TAG = "Gestures";

        @Override
        public boolean onDown(MotionEvent event) {
            Log.d(DEBUG_TAG,"onDown: " + event.toString());
            return true;
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            Log.d(DEBUG_TAG, "onScroll: ");
            //Implement functionality you want e.g. horiontal scroll changes viewpager item, vertical scroll collpases the toolbar
            return true;
        }
    }
}

这篇关于通过滚动事件另一种观点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆