Design lib - CoordinatorLayout/CollapsingToolbarLayout with GridView/listView

前端 未结 6 1211
别跟我提以往
别跟我提以往 2020-11-30 07:54

This might be silly question but I didn\'t understand Design lib well. I am following this reference to create below layout. The Blue area should work as parallax when I scr

相关标签:
6条回答
  • 2020-11-30 08:03

    With ListView/GridView, it works only on Lollipop by following code-

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
         listView.setNestedScrollingEnabled(true);
    }
    

    I think for Now CoordinatorLayout works only with RecyclerView and NestedScrollView

    EDIT :

    use -

    ViewCompat.setNestedScrollingEnabled(listView/gridview,true); (add Android Support v4 Library 23.1 or +)
    
    0 讨论(0)
  • 2020-11-30 08:03

    You have to put gridview inside NestedScrollview as usual then you have to add gridview height dynamically. then everything would work good.!!!

     <android.support.v4.widget.NestedScrollView
           xmlns:android="http://schemas.android.com/apk/res/android"
                   xmlns:app="http://schemas.android.com/apk/res-auto"
                   android:layout_width="match_parent"
                   android:layout_height="match_parent"
                   android:layout_gravity="fill_vertical"
                   android:fillViewport="true">
    
    
                   <LinearLayout
                       android:layout_width="match_parent"
                       android:layout_height="match_parent"
                       android:orientation="vertical">
    
                  <GridView
                   android:id="@+id/camp_list"
                   android:layout_width="fill_parent"
                   android:layout_height="fill_parent"
                   android:layout_below="@id/toolbar"
                   android:layout_margin="10dp"
                   android:gravity="center"
                   android:horizontalSpacing="10dp"
                   android:numColumns="3"
                   android:stretchMode="columnWidth"
                   android:verticalSpacing="10dp"
                   android:visibility="visible" >
               </GridView>
    
    
    
                   </LinearLayout>
               </android.support.v4.widget.NestedScrollView>
    
    0 讨论(0)
  • 2020-11-30 08:04

    This is working for me.

    https://gist.github.com/sakurabird/6868765

    I use GridView inside NestedScrollView

    0 讨论(0)
  • 2020-11-30 08:04

    For convenience i've created a subclass with the new ViewCompat solution:

    public class CoordinatedListView extends ListView {
    
        public CoordinatedListView(Context context) {
            super(context);
            init();
        }
    
        public CoordinatedListView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        public CoordinatedListView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init();
        }
    
        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        public CoordinatedListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
            init();
        }
    
        private void init() {
            ViewCompat.setNestedScrollingEnabled(this, true);
        }
    }
    

    Enjoy :)

    0 讨论(0)
  • 2020-11-30 08:14

    A simple solution was added to the support lib:

    ViewCompat.setNestedScrollingEnabled(listView,true);
    

    I've tested it with Android Support v4 Library 23.1 and it works well.

    0 讨论(0)
  • 2020-11-30 08:20

    Currently the ListView and the GridView have the expected behavior with the CoordinatorLayout only with API>21.

    To obtain this behavior you have to set:

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {   
        setNestedScrollingEnabled(true);
     }
    

    It is not enough to implement the NestedScrollingChild. The AbsListView isn't deployed with support libraries , then it depends by the SO running in the device.

    You have to override some methods in the AbsListView. For example you can check the onInterceptTouchEvent method.

    Inside this code you can see:

      case MotionEvent.ACTION_DOWN: {
        //......
        startNestedScroll(SCROLL_AXIS_VERTICAL);
        //....
      }
    
      case MotionEvent.ACTION_MOVE: {
        //.....
         if (startScrollIfNeeded((int) ev.getX(pointerIndex), y, null)) {
        //....     
       }
       case MotionEvent.ACTION_CANCEL:
       case MotionEvent.ACTION_UP: {
              //..
             stopNestedScroll();
                break;
       }
    

    This code is only in the implementation of AbsListView v21+. If you check the AbsListView with API 20 or lower, you will not find any nested scroll reference.

    0 讨论(0)
提交回复
热议问题