当我添加视图时,RecyclerView会自动刷新(使之无效)

时间:2018-08-27 21:36:54

标签: android

我的情况有点特殊,因为我使用windowManager来drawOverLay而不是常规方法。我遇到一个问题,我在framlayout中有一个recyclerView(称为rootlayout),当我向rootlayout添加一个新视图时,其中的recyclerView将闪烁一次并滚动回到顶部(似乎使自身无效) )。没人对这种情况有任何想法吗?谢谢

这是我添加底页(新布局)的代码

private void showSelectionLayout(String title, List<String> options){
    selectionLayout = (RelativeLayout) layoutInflater.inflate(R.layout.layout_bottom_sheet, rootLayout, false);
    sheetContent = (LinearLayout)selectionLayout.findViewById(R.id.sheet_content);
    RecyclerView sheetList = (RecyclerView) selectionLayout.findViewById(R.id.sheet_recycleView);
    sheetCancel = (HeadlineView)selectionLayout.findViewById(R.id.sheet_cancel);
    HeadlineView sheetTitle = (HeadlineView) selectionLayout.findViewById(R.id.sheet_title);

    ScreenUtils.applyDim(selectionLayout);
    sheetContent.setVisibility(View.GONE);
    sheetCancel.setVisibility(View.GONE);
    selectionLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            selectionLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            final Animation slideUp = AnimationUtils.loadAnimation(
                    getContext(), R.anim.abc_slide_in_bottom
            );
            slideUp.setDuration(SHEET_EXPAND_DURATION);
            sheetContent.setVisibility(View.VISIBLE);
            sheetCancel.setVisibility(View.VISIBLE);
            sheetContent.startAnimation(slideUp);
            sheetCancel.startAnimation(slideUp);
        }
    });

    selectionLayout.setOnClickListener(view -> dismissSelectionLayout());
    sheetCancel.setOnClickListener(view -> dismissSelectionLayout());
    sheetTitle.setText(String.format(SharedConstants.CONTACT_TITLE, title));

    SheetAdapter adapter = new SheetAdapter(options);
    adapter.setItemClickListener(option -> {
        dismissSelectionLayout();
    });
    LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
    sheetList.setAdapter(adapter);
    sheetList.setLayoutManager(layoutManager);
    TransitionManager.beginDelayedTransition(rootLayout);
    rootLayout.addView(selectionLayout);
}

1 个答案:

答案 0 :(得分:0)

看来我找到了原因。

因为我需要获取用户单击的项目的itemView,所以我获得了视图并在Click侦听器中为其父项调用removeAllViews(),这将导致recyclerView本身刷新(无效)

holder.binding.item.setOnClickListener(view -> {
        if (mItemListener != null) {
            int[] margin = new int[2];
            margin[0] = holder.binding.suggestionCardLayout.getLeft() + ((ViewGroup) holder.itemView.getParent()).getLeft();
            margin[1] = holder.binding.suggestionCardLayout.getTop() + ((ViewGroup) holder.itemView.getParent()).getTop();
            TransitionManager.beginDelayedTransition((ViewGroup) holder.binding.suggestionCardLayout.getParent());
            ((ViewGroup) holder.binding.suggestionCardLayout.getParent()).removeAllViews();
            mItemListener.onShortcutClicked(card.getShortcut(), holder.binding.suggestionCardLayout, margin);
        }
    });

所以,只需删除removeAllViews()函数就可以了

holder.binding.item.setOnClickListener(view -> {
        if (mItemListener != null) {
            int[] margin = new int[2];
            margin[0] = holder.binding.suggestionCardLayout.getLeft() + ((ViewGroup) holder.itemView.getParent()).getLeft();
            margin[1] = holder.binding.suggestionCardLayout.getTop() + ((ViewGroup) holder.itemView.getParent()).getTop();
            mItemListener.onShortcutClicked(card.getShortcut(), holder.binding.suggestionCardLayout, margin);
        }
    });