I have been working on an android studios app in which I have a fragmentin which I want to show an ImageView and below it a RecyclerView. The Problem is that the RecyclewView doesn't show up in the actual app. If only the RecyclerView is in the XML file and not the ImageView then the RecyclerView can be seen.
Here is the 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"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/deliveryMapImageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/baseline_map_24"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<androidx.recyclerview.widget.RecyclerView
xmlns:tools="http://schemas.android.com/tools"
android:name="com.example.hci_shop.DeliveryFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layoutManager="LinearLayoutManager"
tools:context=".ui.deliveries.DeliveriesFragment"
tools:listitem="@layout/fragment_delivery"
app:layout_constraintTop_toBottomOf="@+id/deliveryMapImageView" />
</androidx.constraintlayout.widget.ConstraintLayout>
```[enter image description here](https://i.stack.imgur.com/dsnYC.png)
this is what it looks like in the editor:
Here is the java file for the fragment:
package com.example.hci_shop.ui.deliveries;
import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;
import androidx.fragment.app.Fragment; import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView;
import com.example.hci_shop.R; import com.example.hci_shop.ui.placeholder.PlaceholderContent;
/**
A fragment representing a list of Items. */ public class DeliveriesFragment extends Fragment {
// TODO: Customize parameter argument names private static final String ARG_COLUMN_COUNT = "column-count"; // TODO: Customize parameters private int mColumnCount = 1;
/**
- Mandatory empty constructor for the fragment manager to instantiate the
- fragment (e.g. upon screen orientation changes). */
// TODO: Customize parameter initialization @SuppressWarnings("unused") public static DeliveriesFragment newInstance(int columnCount) { DeliveriesFragment fragment = new DeliveriesFragment(); Bundle args = new Bundle(); args.putInt(ARG_COLUMN_COUNT, columnCount); fragment.setArguments(args); return fragment; }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
if (getArguments() != null) { mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT); }}
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_delivery_list, container, false);
// Set the adapter if (view instanceof RecyclerView) { Context context = view.getContext(); RecyclerView recyclerView = (RecyclerView) view; if (mColumnCount <= 1) { recyclerView.setLayoutManager(new LinearLayoutManager(context)); } else { recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount)); } recyclerView.setAdapter(new MyDeliveriesRecyclerViewAdapter(PlaceholderContent.ITEMS)); } return view;} }
Here is the RecyclerViewAdapter:
package com.example.hci_shop.ui.deliveries;
import android.view.LayoutInflater; import android.view.ViewGroup; import android.widget.TextView;
import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView;
import com.example.hci_shop.databinding.FragmentDeliveryBinding; import com.example.hci_shop.models.Order; import com.example.hci_shop.ui.interfaces.OnItemClickListener; import com.example.hci_shop.ui.placeholder.PlaceholderContent.PlaceholderItem;
import java.util.List;
/**
{@link RecyclerView.Adapter} that can display a {@link PlaceholderItem}.
TODO: Replace the implementation with code for your data type. */ public class MyDeliveriesRecyclerViewAdapter extends RecyclerView.Adapter<MyDeliveriesRecyclerViewAdapter.ViewHolder> {
private final List mValues;
public MyDeliveriesRecyclerViewAdapter(List items) { this.mValues = items; }
@NonNull @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(FragmentDeliveryBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));}
@Override public void onBindViewHolder(final ViewHolder holder, int position) { holder.mItem = mValues.get(position); holder.textViewOrderID.setText(mValues.get(position).id); holder.textViewOrderAddress.setText(mValues.get(position).content); }
@Override public int getItemCount() { return mValues.size(); }
public static class ViewHolder extends RecyclerView.ViewHolder { public final TextView textViewOrderID; public final TextView textViewOrderAddress; public final TextView textViewBoughtAt; public PlaceholderItem mItem;
public ViewHolder(FragmentDeliveryBinding binding) { super(binding.getRoot()); textViewOrderID = binding.textViewOrderID; textViewOrderAddress = binding.textViewOrderAddress; textViewBoughtAt = binding.textViewOrderBoughtAt; } @Override public String toString() { return super.toString() + " '" + textViewOrderAddress.getText() + "'"; }} }
and here is the other xml file that is displayed above the fragment:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/profileBar"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@color/blueBlack"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" >
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/profilePictureImageView"
android:layout_width="50dp"
android:layout_height="50dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:backgroundTint="@color/tanAccent"
android:tint="@color/tanAccent"
android:src="@drawable/profile_picture_placeholder"
app:civ_border_width="3dp"
app:civ_border_color="@color/tanAccent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<SearchView
android:id="@+id/searchbarShopActivity"
android:layout_width="match_parent"
android:layout_height="70dp"
android:queryHint="Suche ..."
android:iconifiedByDefault="false"
app:layout_constraintTop_toBottomOf="@id/profileBar"
app:layout_constraintStart_toStartOf="parent" />
<fragment
android:id="@+id/nav_host_fragment_activity_shop"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/searchbarShopActivity"
app:layout_constraintVertical_bias="1.0"
app:navGraph="@navigation/mobile_navigation" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
and here is what the final result looks like:[enter image description here](https://i.stack.imgur.com/uCILd.png)
If somebody here could please tell me what my error is it would be very helpful. Thank you in advance
I have tried to see if I can find a solution in the documentation, on stackoverflow (if somebody my have had the same problem) and I have even stooped so low as to ask chatgpt. None of which could give me a solution that worked for me. I have come here in the hopes that maybe someone here could spot my error.