简体   繁体   中英

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

    @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. I Suggest -create 1 more variable(isClicked) in your model and set it true when you click it. -In your onBind method of adapter you have to again write the code for creating this child view.

Or you can use ExpandableListView this will handle the state more gracefully.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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