2016-07-08 52 views
1

BottomSheetDialogFragmentを実装し、問題に直面する必要があります。 私はBottomSheetDialogFragmentの高さが固定されている必要があります。誰もそれをする方法を持っていますか?ここで固定高さでBottomSheetDialogFragmentを実装するには

は、フラグメントコンテンツの私のXML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="@dimen/bottom_sheet_height" 
    android:background="@android:color/white" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/drag_title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="24sp" 
     android:textColor="#FF0000" 
     android:text="Title"/> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recycler_view" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:background="@android:color/white" 
     android:layout_weight="1"/> 

    <TextView 
     android:id="@+id/ok_button" 
     android:layout_width="match_parent" 
     android:layout_height="54dp" 
     android:background="@android:color/holo_blue_dark" 
     android:gravity="center" 
     android:text="Hello" 
     android:textColor="@android:color/white" 
     android:textSize="24sp"/> 

</LinearLayout> 

され、setupDialog()に私はこれやっている:

@Override 
public void setupDialog(Dialog dialog, int style) { 
    super.setupDialog(dialog, style); 
    View contentView = View.inflate(getContext(), R.layout.bottom_sheet_dialog_content_view, null); 
    dialog.setContentView(contentView); 
    CoordinatorLayout.LayoutParams layoutParams = ((CoordinatorLayout.LayoutParams) ((View) contentView.getParent()).getLayoutParams()); 
    CoordinatorLayout.Behavior behavior = layoutParams.getBehavior(); 
    if (behavior != null && behavior instanceof BottomSheetBehavior) { 
     ((BottomSheetBehavior) behavior).setBottomSheetCallback(bottomSheetCallback); 
     ((BottomSheetBehavior) behavior).setPeekHeight(getResources().getDimensionPixelSize(R.dimen.bottom_sheet_height)); 
    } 

    initRecyclerView(contentView); 
} 

と動作が非常に一般的です:

private BottomSheetBehavior.BottomSheetCallback bottomSheetCallback = new BottomSheetBehavior.BottomSheetCallback() { 
    @Override 
    public void onStateChanged(@NonNull View bottomSheet, int newState) { 
     if (newState == BottomSheetBehavior.STATE_HIDDEN) { 
      dismiss(); 
     } 
    } 

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

UPD:固定高さをRecyclerViewに設定して解決しました。誰かがより良いアプローチを知っていますか?

答えて

2

修正の高さは、「スタイルを作成する」で直接指定できます。

styles.xml

<style name="BottomSheetDialog" parent="Theme.Design.Light.BottomSheetDialog"> 
    <item name="bottomSheetStyle">@style/bottomSheetStyleWrapper</item> 
</style> 

<style name="bottomSheetStyleWrapper" parent="Widget.Design.BottomSheet.Modal"> 
    <item name="behavior_peekHeight">500dp</item> 
</style> 

更新:

BottomSheetDialog dialog = new BottomSheetDialog(this, R.style.BottomSheetDialog); 
dialog.setContentView(R.layout.layout_bottom_sheet); 
dialog.show(); 

又は第二Approch:

CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) ((View) contentView.getParent()).getLayoutParams(); 
    CoordinatorLayout.Behavior behavior = params.getBehavior(); 

    if(behavior != null && behavior instanceof BottomSheetBehavior) { 
     ((BottomSheetBehavior) behavior).setBottomSheetCallback(mBottomSheetBehaviorCallback); 
     ((BottomSheetBehavior) behavior).setPeekHeight(300); 
    } 
+0

をどのように私はこのスタイルを使用することができますか? –

+0

@VladislavSazanovich私の更新を見てください。 – Ironman

+0

そうですね。しかし、残念ながらそれは私のために働いていません。私は問題がRecyclerViewにあると思うが、それは高さが固定されていても親レイアウトのサイズを変更するように思えると思う。 –

1

RecyclerViewコンテンツは、01内に充填された場合BottomSheetを表示するとその高さはよくわかります。動的BottomSheetの高さを設定するとBottomSheetDialogFragmentonResume関数内のグローバルレイアウトリスナーを追加し、コンテンツをラップする:

@Override 
public void onResume() { 
    super.onResume(); 
    addGlobaLayoutListener(getView()); 
} 

private void addGlobaLayoutListener(final View view) { 
    view.addOnLayoutChangeListener(new OnLayoutChangeListener() { 
     @Override 
     public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { 
      setPeekHeight(v.getMeasuredHeight()); 
      v.removeOnLayoutChangeListener(this); 
     } 
    }); 
} 

public void setPeekHeight(int peekHeight) { 
    BottomSheetBehavior behavior = getBottomSheetBehaviour(); 
    if (behavior == null) { 
     return; 
    } 
    behavior.setPeekHeight(peekHeight); 
} 

private BottomSheetBehavior getBottomSheetBehaviour() { 
    CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) ((View) getView().getParent()).getLayoutParams(); 
    CoordinatorLayout.Behavior behavior = layoutParams.getBehavior(); 
    if (behavior != null && behavior instanceof BottomSheetBehavior) { 
     ((BottomSheetBehavior) behavior).setBottomSheetCallback(mBottomSheetBehaviorCallback); 
     return (BottomSheetBehavior) behavior; 
    } 
    return null; 
} 
+0

私は本当にOnLayoutChangeListenerを削除する考えが嫌いです。これは将来の問題につながる可能性があります。 –

+0

初期化中にBottomSheetの実際の高さを1度取得するためにのみ使用されます。その後、リソースを解放するために削除されます。これを追加して削除しない場合は、 'BottomSheet'が表示されるたびに、新しい' OnLayoutChangeListener'が添付されます。すばやくリソース(メモリ)が足りなくなります。 –

+0

@R.ZagórskiBottomSheetDialogFragmentのドラッグダウンを完全に削除する方法はありますか? recyclerviewを使っている間?? https://github.com/rubensousa/BottomSheetExample/issues/3 –

関連する問題