使用GridView闪烁的BottomSheetBehavior

时间:2016-04-19 12:14:30

标签: android android-layout listview gridview

我尝试在bottomSheetBehavior中使用gridView 我将数据设置为异步,并在用户单击我的按钮时调用setAdapter。 问题是我看到了数据的闪烁。我该怎么做才能消除这种闪烁?

在我的onCreate:

    final ViewGroup gridViewContainer = (ViewGroup) findViewById(R.id.bottomSheetContainer);
    if (gridViewContainer != null) {
    gridView = (GridView) gridViewContainer.findViewById(R.id.actionGridView);
    bottomSheetBehavior = BottomSheetBehavior.from(gridViewContainer);
    bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
        @Override
        public void onStateChanged(@NonNull View bottomSheet, int newState) {
            switch (newState) {
                case BottomSheetBehavior.STATE_EXPANDED:
                    isBottomSheetQuickActionOpen = true;
                    gridViewContainer.requestLayout();
                    break;
                case BottomSheetBehavior.STATE_COLLAPSED:
                    isBottomSheetQuickActionOpen = false;
                    break;
            }
        }

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

        }
    });

public void onBottomBarIconClicked(View v){
    switch(v.getId()){
        case R.id.right_bottom_action:
            if (bottomSheetBehavior != null) {
                if (!isBottomSheetQuickActionOpen) {
                    setActionsForQuickDialog();
                    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
                } else {
                    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
                }
            }
            break;
    }
}

private void setupQuickActionDialog() {
    ActionsManager actionManager = ActionsManager.get();
    List<WidgetMetaData> list = getMyList();
    if (CollectionUtils.isNotEmpty(list)) {
        if (gridView != null) {
            adapterGridView = new AdapterGridView(ContactDetailsActivity.this, list, contact);
            gridView.setAdapter(adapterGridView);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我找到了解决办法:
1.保存可运行的成员

    private final Runnable runnable = new Runnable() {
        @Override
        public void run() {
                if (!isBottomSheetQuickActionOpen) {
                    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
                } else {
                    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
                }
        }
    };
  1. 保存主线程:

    Handler mainHandler = new Handler(context.getMainLooper());

  2. 单击按钮时,首先创建gridview图标,然后使用mainthread发布runnable。如果我们想要折叠bottomsheetbehaviour,我们应该从队列中删除runnable,然后通过

    崩溃

    setBottomSheetState(BottomSheetBehavior.STATE_COLLAPSED);

  3. 改变的功能:

    public void onBottomBarIconClicked(View v) {
        switch (v.getId()) {
            case R.id.open_menu_btn:
                if (bottomSheetBehavior != null) {
                    if (!isBottomSheetQuickActionOpen) {
                        setActionsForQuickDialog();
                        mainHandler.post(runnable);
                    } else {
                        handler.removeCallbacks(runnable);
                        collapseBottomSheet();
                    }
                }
                break;
        }
    }