Pass scroll event to another view

前端 未结 2 1752
终归单人心
终归单人心 2020-12-30 08:47

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 col

相关标签:
2条回答
  • 2020-12-30 09:07

    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.

    0 讨论(0)
  • 2020-12-30 09:30

    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;
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题