0

I am having a bit trouble with my RecyclerView. If my adapter list have more than 20 items, the recyclerview shows nothing. Anything <20 is ok, and the items are displayed. Also, I can't scroll on the recyclerView.

RecyclerView creation in Fragment:

recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(context);
recyclerView.setLayoutManager(layoutManager);
myAdapter = new MyAdapter();
myAdapter.setList(myList); //ArrayList<MyObject>
recyclerView.setAdapter(myAdapter);

My Adapter:

public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {

    private final ArrayList<MyObject> myList = new ArrayList<MyObject>();

    public MyAdapter() {}

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

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.bind(myList.get(position));
    }

    @Override
    public int getItemCount() {
        return myList.size();
    }

    public void setList(ArrayList<MyObject> newList) {
        myList.clear();
        myList.addAll(newList);
        notifyDataSetChanged();
    }
}

and here is my viewholder:

public class CountryViewHolder extends RecyclerView.ViewHolder {

    private final TextView textView;
    private MyObject object;

    public CountryViewHolder(@NonNull View itemView) {
        super(itemView);
        textView = itemView.findViewById(R.id.my_holder_textview);
    }

    public void bind(@Nullable MyObject object){
        this.object = object;
        if(pbject == null)
            textView.setText("Error. Could not load name.");
        else
            textView.setText(object.getName());
    }
}

myitem.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/my_holder_textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:layout_marginBottom="4dp"
        android:textSize="24sp"
        android:gravity="center"/>
</LinearLayout>

Fragment layout where I have the recyclerView:

<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_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/myRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Any suggestions?

EDIT:

Changing the layout from ConstraintLayout to LinearLayout, the recyclerview will run fine as it should. So the culprit here is the Constraintlayout. I would like to keep the constraintlayout, but I can't figure out how to make it work yet.

CJR
  • 3,174
  • 6
  • 34
  • 78
  • What do you mean with disappear,is it outside the view or non existent? Have you tried using the layout inspector? https://developer.android.com/studio/debug/layout-inspector – Haroun Hajem Mar 25 '19 at 18:37
  • @HarounHajem Yes, confirms its an empty View – CJR Mar 25 '19 at 18:38
  • The layout inspector could give you some clues what is going on. It sound like an layout issue. You need to find the culprit, is it the RecyclerView, ListItem, Fragement? – Haroun Hajem Mar 25 '19 at 18:42
  • @HarounHajem I can see that, when having large lists, my framelayout(this is the fragment container from activity)+constraintlayout(in fragment layout)+recyclerview has not expanded at all horizontally, but vertically. It shows up like "line" in the inspector. Any Idea why? – CJR Mar 25 '19 at 18:46

2 Answers2

0

Instead of:

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

Try:

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/myRecyclerView"
    android:layout_width="0dp"
    android:layout_height="0dp"
MidasLefko
  • 4,499
  • 1
  • 31
  • 44
  • Thanks for your reply. I have tried this, I end up getting an empty recyclerview even if mylist is <20 items big – CJR Mar 25 '19 at 18:42
0

I can see that you have an error in onCreateViewHolder, it should return MyViewHolder and not a MyAdapter.

Also the setHasFixedSize(true) could be an issue here because your children might not change in size if this has to be true(find more information on SO).

Haroun Hajem
  • 5,223
  • 3
  • 26
  • 39