底部薄片没有按预期扩展

时间:2017-11-20 11:03:22

标签: java android android-layout

我正在使用Bottom Sheet和Recyclerview。

我的期望是将RecyclerView的布局管理器从List转换为Grid。

然而,当我更改LayoutManager时,底部工作表不再显示全屏。

这是Gif:

https://media.giphy.com/media/26FeSME9RKKfIQIPS/giphy.gif

主要活动:

private BottomSheetBehavior bottomSheetBehavior;


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

    ListHorizontalFragment fragment = new ListHorizontalFragment();
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.add(R.id.containerFragment, fragment).commit();

    View bottomSheet = findViewById(R.id.bottom_sheet);

    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    int height = displayMetrics.heightPixels;


    bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
    bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
        @Override
        public void onStateChanged(@NonNull View bottomSheet, int newState) {
            if (newState == BottomSheetBehavior.STATE_COLLAPSED) {
                final float scale = getResources().getDisplayMetrics().density;
                int pixels = (int) (200 * scale + 0.5f);
                bottomSheetBehavior.setPeekHeight(pixels);
                fragment.changeLayoutManagerToHorizontal();
            }
            if (newState == BottomSheetBehavior.STATE_EXPANDED) {
                fragment.changeLayoutManagerToGrid();
                bottomSheet.setMinimumHeight(height);
            }

        }

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

        }
    });


}

显示项目的片段:

 private RecyclerView recyclerView;
private RecyclerViewAdapter adapter;
private ArrayList<Integer> colors = new ArrayList<>();

public ListHorizontalFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_list_horizontal, container, false);
    recyclerView = (RecyclerView) view.findViewById(R.id.horizontalRecyclerView);


    List<Integer> lstColor = new ArrayList<>();
    lstColor.add(R.color.actionbar_blue);
    lstColor.add(R.color.black);
    lstColor.add(R.color.dark_blue);
    lstColor.add(R.color.common_action_bar_splitter);
    lstColor.add(R.color.red);
    lstColor.add(R.color.yellow);
    lstColor.add(R.color.green);
    lstColor.add(R.color.pitch_blue);

    colors.addAll(lstColor);

    changeLayoutManagerToHorizontal();
    return view;
}

public void changeLayoutManagerToHorizontal() {
    recyclerView.setAdapter(null);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
    recyclerView.setLayoutManager(linearLayoutManager);
    adapter = new RecyclerViewAdapter(colors, getActivity());
    recyclerView.setAdapter(adapter);
}

public void changeLayoutManagerToGrid() {
    recyclerView.setAdapter(null);
    recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
    adapter = new RecyclerViewAdapter(colors, getActivity());
    recyclerView.setAdapter(adapter);
}

底页的布局:

 <RelativeLayout
    android:orientation="vertical"
    android:id="@+id/bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:behavior_peekHeight="300dp"
    android:background="@android:color/holo_orange_light"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
    >
    <FrameLayout
        android:layout_centerInParent="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/containerFragment"/>
</RelativeLayout>

当我花费底部纸张时,它应该是全屏的。但不知怎的,它并没有像我预期的那样工作

1 个答案:

答案 0 :(得分:1)

您应该创建This is the plunkr for your problem 的新片段并覆盖其方法

extends BottomSheetDialogFragment

并通过此回调调用您的操作

    @Override
        public void setupDialog(final Dialog dialog, int style) {
          //initialize your view here
}

然后从您的活动中初始化此底部工作表片段。

有关详细信息,请查看本教程http://www.truiton.com/2016/07/android-bottom-sheet-example/ 希望这有帮助。