简体   繁体   English

当我单击同一行的子视图时,为什么该行上的Recycler视图数据消失了?

[英]Why is Recycler view data on the row is gone when i click on the same row's child view?

I have created a recycler view view which will have cardView rows,in the card view i have put a layout,on click of it a childview will open, when i click on the layout, child view is coming but the data which is already populated on the card is dissappearing.And when I scroll down the child view itself disappears and the data on the card is populated again.I am not able to find a solution for this from a long time, please help me with this.This is how I am adding the child view in the adapter 我创建了一个回收者视图视图,该视图将具有cardView行,在Card View中,我放置了一个布局,单击它会打开一个childview,当我单击布局时,将出现child view,但是已经填充的数据向下滚动时,子视图本身消失,并且再次填充了卡上的数据。很长时间以来我一直无法找到解决方案,请为此提供帮助。我在适配器中添加了子视图

    @Override
        public void onClick(View view) {

            mAdapter = new ViewOrderAdapter(orderList);
            LayoutInflater inflater = (LayoutInflater) view.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//            view = inflater.inflate(R.layout.cancelledcardview, null);
            final RelativeLayout parent = (RelativeLayout) inflater.inflate(R.layout.cancelledcardview, null);

            Log.i("TAG", "onclickmethod" + parent);
            final View child = inflater.inflate(R.layout.vieworder_layout, null);
            recyclerView = (RecyclerView)child. findViewById(R.id.recycler_view);
            Log.i("TAG", "onclickmethod" + recyclerView);
            RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(view.getContext());
            recyclerView.setLayoutManager(mLayoutManager);
            recyclerView.setAdapter(mAdapter);
            parent.addView(child);
            ImageView cancelButton= (ImageView)child.findViewById(R.id.cancelbutton);
            cancelButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Log.i("TAG", "onclickmethodcancel button" );
                    parent.removeView(child);

                }
            });
            prepareMovieData();
            cv.addView(parent);
        }

在此处输入图片说明 在此处输入图片说明

You have to maintain the click state. 您必须保持点击状态。 when you scroll the list the onBind method is called again and it populates the old card again. 滚动列表时,将再次调用onBind方法,并再次填充旧卡。 I Suggest -create 1 more variable(isClicked) in your model and set it true when you click it. 我建议-在模型中再创建1个变量(isClicked),并在单击时将其设置为true。 -In your onBind method of adapter you have to again write the code for creating this child view. -在适配器的onBind方法中,您必须再次编写用于创建此子视图的代码。

Or you can use ExpandableListView this will handle the state more gracefully. 或者您可以使用ExpandableListView,这将更优雅地处理状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM