从底部工作表对话框调用警报对话框或另一个底部工作表会导致背景闪烁

时间:2020-03-15 08:28:04

标签: android bottom-sheet android-bottomsheetdialog

https://imgur.com/gallery/xntpREw

有人遇到这个问题还是我做错了什么

更新:我在android 8和9版本上测试了同一个应用程序,但它没有出现这种故障。因此,这可能特定于android版本10

编辑:

这是对话框片段类,其中在itemview click上调用警报对话框-

public class ItemListDialogFragment extends BottomSheetDialogFragment 
{
  private static final String ARG_ITEM_COUNT = "item_count";

  public static ItemListDialogFragment newInstance(int itemCount) 
  {
    final ItemListDialogFragment fragment = 
          new ItemListDialogFragment();

    final Bundle args = new Bundle();
    args.putInt(ARG_ITEM_COUNT, itemCount);

    fragment.setArguments(args);

    return fragment;
  }

  @Nullable
  @Override
  public View onCreateView(
         LayoutInflater inflater, 
         @Nullable ViewGroup container,
         @Nullable Bundle savedInstanceState) 
  {
    return inflater.inflate( 
        R.layout. 
        fragment_item_list_dialog_list_dialog, 
        container, 
        false );
   }

@Override
public void onViewCreated(
  @NonNull View view, 
  @Nullable Bundle savedInstanceState ) 
{
    final RecyclerView recyclerView = 
       (RecyclerView) view;
    recyclerView.setLayoutManager(
      new LinearLayoutManager( getContext() ) );

    recyclerView.setAdapter( 
      new ItemAdapter( getArguments().getInt( ARG_ITEM_COUNT )  ) );

    recyclerView.setOnClickListener( 
      new View.OnClickListener() 
      {
        @Override
        public void onClick(View view) 
        {
          alert();
        }
       } );
}

private class ViewHolder extends RecyclerView.ViewHolder 
{
  final TextView text;

  ViewHolder( 
     LayoutInflater inflater, 
     ViewGroup parent ) 
  {
    // TODO: Customize the item layout
    super(
      inflater.inflate( 
        R.layout.
          fragment_item_list_dialog_list_dialog_item, 
        parent, 
        false ) );

     text = itemView.findViewById(R.id.text);

     itemView.setOnClickListener(
       new View.OnClickListener() 
       {
          @Override
          public void onClick( View view ) 
          {
             alert();
          }
        } );
  }
}

private class ItemAdapter extends RecyclerView.Adapter<ViewHolder> 
{
  private final int mItemCount;
  ItemAdapter( int itemCount ) 
  {
    mItemCount = itemCount;
  }

  @NonNull
  @Override
  public ViewHolder onCreateViewHolder(
    @NonNull ViewGroup parent, 
    int viewType) 
  {
    return new ViewHolder(
      LayoutInflater.from( parent.getContext( ) ), 
      parent );
  }

  @Override
  public void onBindViewHolder( ViewHolder holder, int position ) 
  {
    holder.text.setText( String.valueOf( position ) );
  }

  @Override
  public int getItemCount() 
  {
    return mItemCount;
  }
}

private void alert()
{
  AlertDialog.Builder builder = 
    new AlertDialog.Builder( getActivity() );

  builder.setTitle( "Alert dialog" );
  builder.setMessage( "This is a alert dialog" );
  builder.setPositiveButton(
    "ok", 
    new DialogInterface.OnClickListener() 
    {
      @Override
      public void onClick( DialogInterface dialogInterface, int i ) 
      {
      }
    } );
  builder.setNegativeButton(
    "close", 
    new DialogInterface.OnClickListener() 
    {
      @Override
      public void onClick( DialogInterface dialogInterface, int i ) 
      {
      }
    } );

  builder.show();
} 

这就是底页的称呼方式-

public class MainActivity extends AppCompatActivity 
{

  @Override
  protected void onCreate( Bundle savedInstanceState ) 
  {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_main );

    ItemListDialogFragment.
      newInstance(10).
      show( getSupportFragmentManager(), "df" );
  }
}

1 个答案:

答案 0 :(得分:0)

通过设置嵌套对话框的主题,将android:backgroundDimEnabled设置为false,我找到了解决方法。

<style name="Theme.MyApp.BottomSheetDialog.Nested" parent="@style/Theme.Design.Light.BottomSheetDialog">
        <item name="android:backgroundDimEnabled">false</item>
</style>

在我的onCreateDialog内:

 val bottomSheet =
        if (isNested()) {
            BottomSheetDialog(
                requireContext(),
                R.style.Theme_MyApp_BottomSheetDialog_Nested
            )
        } else {
            BottomSheetDialog(
                requireContext(),
                R.style.Theme_Design_BottomSheetDialog
            )
        }

将主题传递到AlertDialog.Builder时,其工作原理应相同。