2016-02-25 14 views
22

試してみたいBottomSheetDialogがAndroidサポートライブラリ23.2に導入されましたが、正しく動作していないようです。BottomSheetDialogの使い方は?

BottomSheetBehaviorは、永続的な底面シートケースを取り込みながら、このリリースではまた、モーダルボトムシートはケースを使用埋めるためにBottomSheetDialogと BottomSheetDialogFragmentを提供しています。ここではドキュメントが言うことです。 AppCompatDialogまたはAppCompatDialogFragmentを ボトムシートに置き換えて、ダイアログボックスの下部を シートにするだけです。

だから私はBottomSheetDialogに私のAppCompatDialogを変更:ここで

package my.package.ui.dialog; 

import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.support.design.widget.BottomSheetDialog; 

import my.package.R; 

public class AccountActionsDialog extends BottomSheetDialog { 
    public AccountActionsDialog(Context context) { 
     super(context); 

     if (context instanceof Activity) { 
      setOwnerActivity((Activity) context); 
     } 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(getLayoutInflater().inflate(R.layout.dialog_account_actions, null)); 
    } 
} 

は私のレイアウトファイルです:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#ff0000" 
     android:padding="16dp" 
     android:text="Delete account" 
     android:textColor="#ffffff" /> 

</LinearLayout> 

その後、私は私の活動に次のコードを使用します。

new AccountActionsDialog(this).show(); 

画面が淡色表示になりますが、ダイアログの内容は表示されません。何が欠けているかもしれないかについての考えはありますか?代わりにAppCompatDialogを使用すると正常に動作します。

+0

あなたは 'setContentView(R.layout.dialog_account_actionsを)'を使用していない特別な理由がありますか? – ianhanniballake

+0

はい。最初はルートビューを最初に展開し、findViewById()を使用して必要なすべてのサブビューを見つけ出し、適切なリスナーを設定しました。私はこの例のコードを単純化し、この部分を変更するのを忘れました。レイアウトはどのように設定されていても問題は残ります。 –

+0

これはサポートライブラリのバグです。b.android.comに小さなサンプルプロジェクトを作成してリンクを張ってください。私はそれをチームに持ち出すことができます。さらにクレイジーな動作のために、 LinearLayout'を〜250dpの高さに設定します。 – ianhanniballake

答えて

2

これはBottomSheetDialogのレイアウトファイルです。

<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:layout_height="match_parent" 
    android:soundEffectsEnabled="false"> 

<FrameLayout 
     android:id="@+id/design_bottom_sheet" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     app:layout_behavior="@string/bottom_sheet_behavior" 
     style="?attr/bottomSheetStyle"/> 

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

あなたのコンテンツビューは、ビューdesign_bottom_sheetの内側に、それはCoordinatorLayoutにより上下中央に配置されている、とBottomSheetBehaviorはそれをオフセットします。

mParentHeight = parent.getHeight(); 
mMinOffset = Math.max(0, mParentHeight - child.getHeight()); 
mMaxOffset = mParentHeight - mPeekHeight; 
if (mState == STATE_EXPANDED) { 
    ViewCompat.offsetTopAndBottom(child, mMinOffset); 
} else if (mHideable && mState == STATE_HIDDEN) { 
    ViewCompat.offsetTopAndBottom(child, mParentHeight); 
} else if (mState == STATE_COLLAPSED) { 
    ViewCompat.offsetTopAndBottom(child, mMaxOffset); 
} 

それはmMaxOffsetdesign_bottom_sheetを位置決幅するintentedが、実際には、子ビューの初期getTopは0ではありませんが、(mParentHeight - childHeight)/2ので、より多くの所望のオフセットよりもオフセットどうかを表示します。

ビューdesign_bottom_sheetを見つけ、重力をGravity.TOP | Gravity.CENTER_HORIZONTALに設定すると修正されます。しかし、childHeightがmPeekHeightより小さい場合は、コンテンツビューの下に空白の領域があります。

ただし、peekHeight > childHeightの場合、mMaxOffsetmMinOffsetより小さくなり、奇妙な動作が発生します。

は、たぶんコードは、私は同じ問題をexpriencingた

mMaxOffset = mParentHeight - child.getHeight(); 
+0

これは私も見ている問題です。コンテンツビューをラップするFramelayoutは、何らかの理由で垂直方向の中央に配置されます。 BottomSheetBehaviorがFramelayoutをレイアウトすると、Y値> 0で始まります。このビヘイビア内のすべての数学は、親のTOPからビューをオフセットすることによって動作し、コンテンツが予想よりもはるかに低くなりますオフスクリーン) –

+0

@Edward Kimmel FrameLayoutは、重力のtop、bottom、またはcenter_verticalを設定するかどうかをチェックします。これらのどれも設定されていない場合、FrameLayoutは子を一番上に配置します。しかし、 'CoordinatorLayout.layoutChild'は' Gravity.apply'を使って子をレイアウトします。 'Gravity.apply'では、これらの重力が設定されていなければ、子ビューを親の中心に配置します。 [Gravity.apply](http://androidxref.com/6.0.1_r10/xref/frameworks/base/core/java/android/view/Gravity.java#231) –

2

mMaxOffset = Math.max((mParentHeight - mPeekHeight), mMinOffset); 

instedに変更する必要があり、背景と内容が表示されていない淡色表示。ここでは、コンテンツビューをsetupDialog()の非表示の方法で設定して回避する方法を示します。

public class CustomBottomSheetDialogFragment extends BottomSheetDialogFragment { 

    private TextView mOffsetText; 
    private TextView mStateText; 
    private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() { 

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

     } 

     @Override 
     public void onSlide(@NonNull View bottomSheet, float slideOffset) { 
      setOffsetText(slideOffset); 
     } 
    }; 
    private LinearLayoutManager mLinearLayoutManager; 
    private ApplicationAdapter mAdapter; 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     return super.onCreateDialog(savedInstanceState); 
    } 

    @Override 
    public void onViewCreated(View contentView, @Nullable Bundle savedInstanceState) { 
     super.onViewCreated(contentView, savedInstanceState); 
    } 

    @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); 
     mBottomSheetBehavior = BottomSheetBehavior.from(((View) contentView.getParent())); 
     if (mBottomSheetBehavior != null) { 
      mBottomSheetBehavior.setBottomSheetCallback(mBottomSheetBehaviorCallback); 
     } 
     mOffsetText = (TextView) contentView.findViewById(R.id.offsetText); 
     mStateText = (TextView) contentView.findViewById(R.id.stateText); 
    } 

} 

とレイアウト:ここで

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


    <TextView 
     android:id="@+id/offsetText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="@android:color/black" /> 

    <TextView 
     android:id="@+id/stateText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="@android:color/black" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 
</LinearLayout> 
+0

の動作を取得するには、BottomSheetBehavior.fromを使用することもできます(View v)例外をスローしますが –

2

https://code.google.com/p/android/issues/detail?id=201793

一部のユーザーが見ている問題を中心にしている私たちのコンテンツビューをラップでframeLayoutに沸くcode.google.com上の問題です垂直にBottomSheetBehaviorは、このビューが上部に位置合わせされている場合にのみ機能します。私はでframeLayoutはまだ上下中央になるために原因を考え出したていないが、ここでの可能な回避策があります:

View contentView = ... 
// You may have to measure your content view first. 
dialog.setContentView(contentView); 

// Change this to a percentage or a constant, whatever you want to do. 
// The default is 1024 - any views smaller than this will be pulled off 
// the bottom of the screen. 
float peekHeight = contentView.getMeasuredHeight(); 

View parent = (View)contentView.getParent(); 
BottomSheetBehavior behavior = BottomSheetBehavior.from(parent); 
behavior.setPeekHeight(peekHeight); 
CoordinatorLayout.LayoutParams layoutParams = 
    (CoordinatorLayout.LayoutParams)parent.getLayoutParams(); 
layoutParams.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL; 
+0

これで何も修正されません。 – 0101100101

0

私は私のTextView(200dp)のための固定の高さを設定するときは、仕事を始め、いくつかの高さの値についてものの、依然として正しく動作しません。明らかにサポートlibの問題です。バグトラッカーにBottomSheetDialogに関連するいくつかの報告がすでにあります

https://code.google.com/p/android/issues/detail?id=201793&sort=-opened&colspec=ID%20Status%20Priority%20Owner%20Summary%20Stars%20Reporter%20Opened

https://code.google.com/p/android/issues/detail?id=201826

関連する問題