java – Inflate 2 layouts in one Fragment – android

Exception or error:

I am trying to update a TextView with text in a method. The primary function of the method is to inflate a layout with a RecyclerView and to update these views with values from firestore. My RecyclerView also has an ItemDecoration attached which has its own layout. In this method I also want to update the textview of this layout. I tried creating a seperate View and inflating the ItemDecoration layout, finding the TextView and updating the text but the text doesnt update when run.

OnCreate Method

 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        v = inflater.inflate(R.layout.galway_fragment, container, false);

        recyclerView = (RecyclerView) v.findViewById(R.id.galway_recycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setHasFixedSize(true);
        MyAdapter adapter = new MyAdapter(placeNames);

        recyclerView.addItemDecoration(HeaderDecoration.with(recyclerView)
                .inflate(R.layout.stamp_header)
                .parallax(0.2f)
                .dropShadowDp(4)
                .build());

        recyclerView.setAdapter(adapter);

        db.collection("galway")
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        for (QueryDocumentSnapshot document : task.getResult()) {
                            placeNames.add(document.getData().get("Name").toString());
                        }
                        adapter.notifyDataSetChanged();
                    }
                });
        View v2 = inflater.inflate(R.layout.stamp_header, container, false);
        TextView header = (TextView) v2.findViewById(R.id.header);
        Log.i("HEADER", header.toString());
        header.setText("GALWAY");

        return v;
    }

galway_fragment is the RecyclerView layout which defines the layout for the fireStore data, placeNames. The ItemDecoration layout is defined by stamp_header which I am trying to set “GALWAY” in a TextView as a test.

galway_fragment

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/galway_recycler"/>
</LinearLayout>

stamp_header

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:background="@color/red"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:padding="16dp"
        android:textSize="30sp"
        android:text="HI THERE" />


</RelativeLayout>
How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *