Android 当gridview不在顶部时,如何解压appbarlayout

Android 当gridview不在顶部时,如何解压appbarlayout,android,android-collapsingtoolbarlayout,android-nestedscrollview,Android,Android Collapsingtoolbarlayout,Android Nestedscrollview,我正在尝试实现一个类似instagram的gallery模块,其中包含图像的gridview将独立滚动,但在滚动gridview时,如果我触摸appbarlayout,它将开始向上滚动。我可以通过setnestedscrollview来实现这一点,但当appbarlayout折叠时,它不会折叠,直到我将gallery带到顶部。因此,当gridview不在起点而是介于两者之间时,如何在触摸工具栏时打开appbar布局,这是我的xml <android.support.design.w

我正在尝试实现一个类似instagram的gallery模块,其中包含图像的gridview将独立滚动,但在滚动gridview时,如果我触摸appbarlayout,它将开始向上滚动。我可以通过setnestedscrollview来实现这一点,但当appbarlayout折叠时,它不会折叠,直到我将gallery带到顶部。因此,当gridview不在起点而是介于两者之间时,如何在触摸工具栏时打开appbar布局,这是我的xml

    <android.support.design.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:id="@+id/coordinator"
        android:focusableInTouchMode="false"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">
    <GridView
            android:layout_below="@+id/iv"
            android:id="@+id/gridView"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:columnWidth="100dp"

            android:drawSelectorOnTop="true"
            android:gravity="center"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:numColumns="5"
            android:stretchMode="columnWidth"
            android:verticalSpacing="5dp"
   android:focusable="true"
            android:clickable="true"/>
    <android.support.design.widget.AppBarLayout
            android:id="@+id/app_bar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:animateLayoutChanges="true"
            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"
                android:animateLayoutChanges="true"
                app:layout_scrollFlags="scroll|exitUntilCollapsed"
    app:expandedTitleMarginStart="48dp"
                app:expandedTitleMarginEnd="64dp"
                android:fitsSystemWindows="true">
     <ImageView
                    android:id="@+id/image"
                    android:src="@drawable/pup"
                    android:layout_width="match_parent"
                    android:layout_height="400dp"
                    android:scaleType="centerCrop"
                    android:adjustViewBounds="true"
                    android:fitsSystemWindows="true"

                  />
    <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    app:layout_collapseMode="pin"
                    android:background="@android:color/transparent"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                    />
  </android.support.design.widget.CollapsingToolbarLayout>
  </android.support.design.widget.AppBarLayout>

下面是我如何实现nestedscrollview的

    gridView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                int y = (int)motionEvent.getRawY();
        if(y<=h)
                {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        gridView.setNestedScrollingEnabled(true);
        }
                    else{

                    }
                }
                else
                {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        gridView.setNestedScrollingEnabled(false);
                        }
                    else
                    {
                         }}
return false;
            }
        });
gridView.setOnTouchListener(新视图.OnTouchListener(){
@凌驾
公共布尔onTouch(视图、运动事件、运动事件){
int y=(int)motionEvent.getRawY();
if(y=Build.VERSION\u code.LOLLIPOP){
setNestedScrollingEnabled(true);
}
否则{
}
}
其他的
{
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.LOLLIPOP){
gridView.setNestedScrollingEnabled(false);
}
其他的
{
}}
返回false;
}
});

在搜索了很多之后,我找到了以下解决方案。在xml中,网格视图必须放在appbarlayout之后的嵌套滚动视图中

<android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:fillViewport="true"
        android:id="@+id/nsv">
        <GridView
            android:layout_below="@+id/iv"
            android:id="@+id/gridView"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:columnWidth="100dp"
            android:drawSelectorOnTop="true"
            android:gravity="center"
            android:numColumns="3"
            android:stretchMode="columnWidth"
            android:verticalSpacing="5dp"
            android:focusable="true"
            android:clickable="true"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    </android.support.v4.widget.NestedScrollView>
int flag=1,flag2=0;
 gridView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    v.getParent().requestDisallowInterceptTouchEvent(true);}
                if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
                    int y = (int) motionEvent.getRawY();
                    if (flag == 1) {
                        if (y <= h) {
                            v.getParent().requestDisallowInterceptTouchEvent(false);
                            boolean fullyExpanded = (appBarLayout.getHeight() - appBarLayout.getBottom()) == 0;
                            if (fullyExpanded) {
                                flag2=0;
                            }
                            else
                            {flag2=1;
                                v.getParent().requestDisallowInterceptTouchEvent(true);
                                if(gridView.getChildAt(0).getTop()==0)
                                {
                                    v.getParent().requestDisallowInterceptTouchEvent(false);
                                }
                                else
                                {
                                    v.getParent().requestDisallowInterceptTouchEvent(true);}}}
                        else
                        {
                            v.getParent().requestDisallowInterceptTouchEvent(true);
                            flag2=0;}
                    }}
                if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    v.getParent().requestDisallowInterceptTouchEvent(true);
                }
                return false;
            }});
coordinatorLayout.setOnTouchListener(new View.OnTouchListener() {
           @Override
           public boolean onTouch(View view, MotionEvent motionEvent) {
               boolean fullyExpanded = (appBarLayout.getHeight() - appBarLayout.getBottom()) == 0;
               if(flag2==0&&fullyExpanded)
                   return  true;
               else{
                   flag2=0;
               return false;
           }}
       });