Home > Software design >  How to inflate RecyclerView from constraintLayout in fragment?
How to inflate RecyclerView from constraintLayout in fragment?

Time:08-31

My app was working completely fine , when I just had the recyclerview in fragment's xml , but I need to wrap it in constraintlayout for some reason but my app starts crashing after that.

error message :

*java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.*

Fragment code

    public class AdminPostFragment extends Fragment  {

        RecyclerView recyclerView;
        DatabaseReference database;
        MyAdapter myAdapter;
        ArrayList<Updates> list;
        private FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View view = inflater.inflate(R.layout.fragment_admin_post, container, false);
            recyclerView = view.findViewById(R.id.updates_recycler);
            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

             if (FirebaseAuth.getInstance().getCurrentUser() != null) {
            FirebaseRecyclerOptions<Updates> options =
                    new FirebaseRecyclerOptions.Builder<Updates>()
                            .setQuery(FirebaseDatabase.getInstance().getReference().child("updates"), Updates.class)
                            .build();


            myAdapter = new MyAdapter(options);

            recyclerView.setAdapter(myAdapter);
            myAdapter.notifyDataSetChanged();
        }
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleCallback);
        itemTouchHelper.attachToRecyclerView(recyclerView);


            return recyclerView;
        }

Fragment XML file

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:id="@ id/parent">

        <androidx.recyclerview.widget.RecyclerView

            android:id="@ id/updates_recycler"
            android:layout_width="match_parent"

            android:layout_height="match_parent"
            android:scrollbars="vertical"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="1.0">


        </androidx.recyclerview.widget.RecyclerView>


    </androidx.constraintlayout.widget.ConstraintLayout>

My Adapter code

package com.project.adminapp.adapter;




import java.util.ArrayList;
.....

public class MyAdapter extends FirebaseRecyclerAdapter< Updates ,MyAdapter.MyViewHolder> {


    public MyAdapter(@NonNull FirebaseRecyclerOptions<Updates> options)
    {
        super(options);
    }


    @Override
    protected void onBindViewHolder(@NonNull  final MyViewHolder holder,  final int position, @NonNull  final Updates model) {
        holder.updates_text.setText(model.getUpdate());




    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.updates,parent,false);
        return new MyViewHolder(v);
    }



    public  class MyViewHolder extends RecyclerView.ViewHolder
    {
        TextView  updates_text ;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);

            updates_text = (TextView) itemView.findViewById(R.id.updates_text);

        }
    }

}

when I am using the code like:

recyclerview = 
(RecyclerView) inflater.inflate(R.layout.fragment_admin_post, container, false); 

getting the ClassCastException saying ConstraintLayout cannot be cast to recyclerview.

CodePudding user response:

You need fix return in onCreateView

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_admin_post, container, false);
    recyclerView = view.findViewById(R.id.updates_recycler);
    ...
    return view;
}

CodePudding user response:

As the ClassCastException is printing the message, you cannot cast the ConstraintLayout to RecyclerView. its because earlier, when you had only recyclerview in your fragment_admin_post.xml file the root view in that file was a recycler view that could be type casted to a RecyclerView. now that you have wrapped it in a ConstraintLayout, you may inflate the view like this:

wrappinggConstraintLayout = (ConstraintLayout) inflater.inflate(R.layout.fragment_admin_post, container, false);

if you can show more detailed code sections, it would be more understandable.

  • Related