Error using the RecyclerView: The specified child already has a parent

前端 未结 6 482
眼角桃花
眼角桃花 2020-12-08 13:16

I am trying to create a listView with a new RecyvlerView Adapter. I have followed the exact guide present on android developer resources. But this is giving me a strange err

6条回答
  •  生来不讨喜
    2020-12-08 13:38

    
    
    
    
    
    

    RelativeLayout this is parent of your TextView with id item_title. Then when RecyclerView is trying to add TextView that has parent it throw exception.

    Try this code:

    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                       int viewType) {
            // create a new view
            Log.d(TAG, "onCreateViewHolder");
            RelativeLayout v = (RelativeLayout)LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.list_item, parent, false);
            // set the view's size, margins, paddings and layout parameters
            View view = v.findViewById(R.id.text1);
            v.removeView(view);
            ViewHolder vh = new ViewHolder(view);
            return vh;
        }
    

提交回复
热议问题