Pass scroll event to another view

孤街浪徒 提交于 2019-11-30 07:03:58
Mimmo Grottoli

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

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.

Edit

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;
        }
    }
}

Your pager content needs to have nested scrolling enabled. The easiest way to accomplish this is to just wrap it in a NestedScrollView:

fragment_image.xml

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <ImageView
        android:id="@+id/backdrop"
        android:layout_width="match_parent"
        android:layout_height="@dimen/detail_backdrop_height"
        android:scaleType="centerCrop" />
</android.support.v4.widget.NestedScrollView>

However this causes the parent height of the ImageView to be indeterminate, so you have to set it to some fixed height.

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