我一直在使用
android设计支持库来折叠工具栏布局.
一切都正常,除了我想滚动整个视图滚动内容在collapsingBarlayout
一切都正常,除了我想滚动整个视图滚动内容在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 /工具栏之外的任何内容,滚动内容.
我也需要在浏览页面上垂直触摸和滚动滚动体验.
已经尝试覆盖ViewTager的onTouchEvent,将ACTION_UP运动事件传递给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); } } }); } });
解决方法
查看正在接收触摸事件的视图.在这种具体情况下,ViewPager正在接收我们要覆盖的触摸事件.一旦您看到接收触摸事件的视图,您可以安装手势检测器.看看这个
guide,特别是在
Detecting a Subset of Supported Gestures
以这种方式,您可以管理在外部视图上执行的所有手势,您可以以某种方式对手势做出反应.
注意:当您扩展GestureDetector.SimpleOnGestureListener时,请注意覆盖onDown方法,否则将不会得到任何其他事件.
编辑
我已经在这个github project测试了我的建议.如果您以这种方式修改了CheeseDetailActivity,那么当您在奶酪图像中滚动时,您将获得调用的onScroll事件.
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; } } }