2017-02-15 47 views
2

我創建了一個BottomSheetDialog,其中有一個GridView。當BottomSheetDialog打開時,您可以正常滾動向下。這樣做會使BottomSheetDialog擴展爲全屏幕並正常向下滾動GridView如何在BottomSheetDialog中的GridView中向上滾動?

但是當用戶試圖滾動向上;而不是滾動在GridViewBottomSheetDialog縮小和關閉。

我想要的是能夠在GridView中上下滾動,而不需要更改尺寸BottomSheetDialog

怎麼辦?

我的代碼:

final BottomSheetDialog dialog = new BottomSheetDialog(context); 
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View view = inflater.inflate(R.layout.grid, null); 
dialog.setContentView(view); 

grid.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" android:layout_height="match_parent"> 

    <GridView 
     android:background="#FFFFFF" 
     android:id="@+id/gridview" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:numColumns="3" 
     android:stretchMode="columnWidth" 
     android:horizontalSpacing="4dp" 
     android:verticalSpacing="4dp" 
     android:gravity="center" 
     /> 

</LinearLayout> 

回答

2

解決:

dialog.setOnShowListener(new DialogInterface.OnShowListener() { 
       @Override 
       public void onShow(final DialogInterface dialog) { 
        BottomSheetDialog d = (BottomSheetDialog) dialog; 
        FrameLayout bottomSheet = (FrameLayout) d.findViewById(android.support.design.R.id.design_bottom_sheet); 
        // Right here! 
        final BottomSheetBehavior behaviour = BottomSheetBehavior.from(bottomSheet); 
        behaviour.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() { 
         @Override 
         public void onStateChanged(@NonNull View bottomSheet, int newState) { 
          if (newState == BottomSheetBehavior.STATE_HIDDEN) { 
           dialog.dismiss(); 
          } 

          if (newState == BottomSheetBehavior.STATE_DRAGGING) { 
           ((BottomSheetBehavior) behaviour).setState(BottomSheetBehavior.STATE_EXPANDED); 
          } 
         } 

         @Override 
         public void onSlide(@NonNull View bottomSheet, float slideOffset) { 

         } 
        }); 
       } 
      }); 
相關問題