2016-02-05 12 views
9

है मैं एक बहुत विशिष्ट उदाहरण सूची एक CoordinatorLayout, AppBarLayout, SwipeRefreshLayout और RecyclerView का उपयोग कर कार्यक्षमता है। जब RecyclerView खाली है या स्क्रॉल करने के लिए पर्याप्त सामग्री नहीं है, तो व्यवहार यह है कि app:layout_scrollFlags="scroll|enterAlwaysCollapsed" वाले ऐपबारलाउट बच्चों को स्क्रॉल करना जारी रहेगा - जो अजीब दिखता है।बंद करो AppBarLayout स्क्रीन बंद स्क्रॉल जब NestedScrollView खाली

क्या NestedScrollView खाली होने पर AppBarLayout बच्चों को स्क्रॉल करने का कोई तरीका है?

enter image description here

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout 
    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.support.design.widget.CoordinatorLayout 
     android:id="@+id/coordinatorLayout" 
     android:background="@android:color/transparent" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <android.support.design.widget.AppBarLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@android:color/transparent" 
      android:elevation="4dp"> 

      <LinearLayout 
       android:id="@+id/eventHeader" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:padding="16dp" 
       android:background="@color/green" 
       android:orientation="horizontal" 
       app:layout_scrollFlags="scroll|enterAlwaysCollapsed"> 

       <View 
        android:layout_width="0dp" 
        android:layout_height="0dp" 
        android:layout_weight="1"/> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="scroll|enterAlwaysCollapsed" 
        android:textColor="@color/white" 
        android:textSize="15sp"/> 

       <View 
        android:layout_width="0dp" 
        android:layout_height="0dp" 
        android:layout_weight="1"/> 

      </LinearLayout> 

     </android.support.design.widget.AppBarLayout> 

     <android.support.v4.widget.SwipeRefreshLayout 
      android:id="@+id/swipeToRefresh" 
      android:layout_width="match_parent" 
      android:layout_gravity="fill_vertical" 
      android:layout_height="match_parent" 
      app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

      <android.support.v7.widget.RecyclerView 
       android:id="@+id/recyclerView" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="@android:color/transparent" 
       android:dividerHeight="0dp" 
       android:layout_gravity="fill_vertical" 
       android:drawSelectorOnTop="true" 
       android:listSelector="@drawable/selector_ripple_grey_transparent" 
       android:scrollbars="vertical"/> 

     </android.support.v4.widget.SwipeRefreshLayout> 

    </android.support.design.widget.CoordinatorLayout> 

    <TextView 
     android:id="@+id/noData" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:padding="16dp" 
     android:text="@string/no_data_available" 
     android:textSize="17sp"/> 

</FrameLayout> 

उत्तर

5

सुनिश्चित नहीं हैं कि यह कैसे सुरुचिपूर्ण एक समाधान है, लेकिन, मैं सिर्फ आग में onStartNestedScroll() घटना overrode अगर NestedScrollView स्क्रॉल है

onCreate में

(इस मामले में एक RecyclerView में) ():

CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams(); 
layoutParams.setBehavior(new AppBarLayoutNoEmptyScrollBehavior(mAppBarLayout, mRecyclerView)); 

व्यवहार:

0,123,
public class AppBarLayoutNoEmptyScrollBehavior extends AppBarLayout.Behavior { 

    AppBarLayout mAppBarLayout; 
    RecyclerView mRecyclerView; 
    public AppBarLayoutNoEmptyScrollBehavior(AppBarLayout appBarLayout, RecyclerView recyclerView) { 
     mAppBarLayout = appBarLayout; 
     mRecyclerView = recyclerView; 
    } 

    public boolean isRecylerViewScrollable(RecyclerView recyclerView) { 
     int recyclerViewHeight = recyclerView.getHeight(); // Height includes RecyclerView plus AppBarLayout at same level 
     int appCompatHeight = mAppBarLayout != null ? mAppBarLayout.getHeight() : 0; 
     recyclerViewHeight -= appCompatHeight; 

     return recyclerView.computeVerticalScrollRange() > recyclerViewHeight; 
    } 

    @Override 
    public boolean onStartNestedScroll(CoordinatorLayout parent, AppBarLayout child, View directTargetChild, View target, int nestedScrollAxes) { 
     if (isRecylerViewScrollable(mRecyclerView)) { 
      return super.onStartNestedScroll(parent, child, directTargetChild, target, nestedScrollAxes); 
     } 
     return false; 
    } 

} 

संपादित

संपादित समाधान RecyclerView ऊंचाई देता है के रूप में के रूप में दिखाई RecyclerView ऊंचाई AppBarLayout ऊंचाई (जो CoordinatorLayout ऊंचाई है)।

हालांकि, अपने पुस्तक इशारा दिखाई AppBarLayout क्षेत्र पर शुरू होता है, तो, एक स्क्रॉल होगा अभी भी ले जगह, भले ही आप के रूप में अच्छी AppBarLayout को यह व्यवहार जोड़ें। इसलिए यह जवाब समस्या के लिए एक फिक्स नहीं है।

2

(के आधार पर: Reference)

(1) इस वर्ग बनाएँ। अपने RecyclerView करने के लिए डेटा लोड हो रहा है के बाद

app:layout_behavior="com.xxxx.xxxxxx.AppBarLayoutBehaviorForEmptyRecyclerView" 
0

, यदि वह रिक्त हो, बस स्क्रॉल झंडा मैन्युअल disalbe:

public class AppBarLayoutBehaviorForEmptyRecyclerView extends AppBarLayout.Behavior 
{ 
    private boolean canRecyclerViewBeScrolled = false; 

    public AppBarLayoutBehaviorForEmptyRecyclerView() 
    { 
    } 

    public AppBarLayoutBehaviorForEmptyRecyclerView(Context context, AttributeSet attrs) 
    { 
     super(context, attrs); 
    } 

    @Override 
    public boolean onInterceptTouchEvent(CoordinatorLayout parent, AppBarLayout child, MotionEvent ev) 
    { 
     return canRecyclerViewBeScrolled && super.onInterceptTouchEvent(parent, child, ev); 
    } 

    @Override 
    public boolean onStartNestedScroll(CoordinatorLayout parent, AppBarLayout child, View directTargetChild, View target, int nestedScrollAxes) 
    { 
     updateScrollable(target); 
     return canRecyclerViewBeScrolled && super.onStartNestedScroll(parent, child, directTargetChild, target, nestedScrollAxes); 
    } 

    @Override 
    public boolean onNestedFling(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, float velocityX, float velocityY, boolean consumed) 
    { 
     return canRecyclerViewBeScrolled && super.onNestedFling(coordinatorLayout, child, target, velocityX, velocityY, consumed); 
    } 

    private void updateScrollable(View targetChild) 
    { 
     if(targetChild instanceof RecyclerView) 
     { 
      RecyclerView.Adapter adapter = ((RecyclerView) targetChild).getAdapter(); 

      canRecyclerViewBeScrolled = adapter != null && adapter.getItemCount() > 0; 
     } 
     else 
     { 
      canRecyclerViewBeScrolled = true; 
     } 
    } 
} 

(2) निम्न विशेषता अपने AppBarLayout XML तत्व में जोड़े

AppBarLayout.LayoutParams toolbarLayoutParams = (AppBarLayout.LayoutParams) mEventHeader.getLayoutParams(); 
toolbarLayoutParams.setScrollFlags(0); 
mEventHeader.setLayoutParams(toolbarLayoutParams); 

और यदि खाली नहीं है, तो स्क्रॉल ध्वज वापस सेट करें:

AppBarLayout.LayoutParams toolbarLayoutParams = (AppBarLayout.LayoutParams) mEventHeader.getLayoutParams(); 
toolbarLayoutParams.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS); 
mEventHeader.setLayoutParams(toolbarLayoutParams); 
0

Graeme जवाब ठीक है लेकिन मैं यह भी निर्माता में जोड़ा

public AppBarLayoutOnEmptyRecyclerViewScrollBehavior(@NonNull AppBarLayout appBarLayout, @NonNull RecyclerView recyclerView) { 
    this.appBarLayout = checkNotNull(appBarLayout); 
    this.recyclerView = checkNotNull(recyclerView); 

    setDragCallback(new DragCallback() { 
     @Override 
     public boolean canDrag(@NonNull AppBarLayout appBarLayout) { 
      return isRecylerViewScrollable(recyclerView); 
     } 
    }); 
} 

इसलिए जब RecyclerView खाली मैं AppBarLayout

से भी अक्षम खींचें
संबंधित मुद्दे