Skip to content
Advertisement

Recycler view shows only first item, even though onBindViewHolder and onCreateViewHolder run for all of them

As the title says;

At first, onBindViewHolder only ran for the first item. Looking at questions and answers on this site indeed revealed that I needed to set the wrapping view’s height to wrap_content. This did fix the first issue and now logcat shows that all methods run properly; However, still, only the first item is displayed.

Why might this happen, and how might I fix it?

MatchHolder (contained in MatchAdapter):

    public static class MatchHolder extends RecyclerView.ViewHolder {

        private final TextView m_textTime;
        private final TextView m_textDay;
        private final TextView m_textUsers;

        public MatchHolder(@NonNull View itemView) {
            super(itemView);
            Log.d("MATCH RECYCLER", "View: " + itemView.toString());

            m_textDay = itemView.findViewById(R.id.sc_sched_text_match_holder_day);
            Log.d("MATCH RECYCLER", "Day View: " + m_textDay.toString());

            m_textTime = itemView.findViewById(R.id.sc_sched_text_match_holder_time);
            Log.d("MATCH RECYCLER", "Time View: " + m_textTime.toString());

            m_textTeams = itemView.findViewById(R.id.sc_sched_text_match_holder_users);
            Log.d("MATCH RECYCLER", "Users View: " + m_textUsers.toString());
        }

        public void setMatch(Context context, ScoutingMatch match) {

            LocalDateTime time = match.getMatchTIme();
            m_textDay.setText(time.format(DateTimeFormatter.ofPattern("dd/MM")));
            m_textTime.setText(time.format(DateTimeFormatter.ofPattern("HH:mm")));

            m_textTeams.setText(String.join(", ", match.getTeams().stream().map(MatchTeam::getTeamNumber).map(String::valueOf).toArray(String[]::new)));

        }
    }

MatchAdapter:

public class MatchAdapter extends RecyclerView.Adapter<MatchAdapter.MatchHolder> {

    List<ScoutingMatch> m_data = new ArrayList<>();
    Context m_context;

    public MatchAdapter(Context context, ScoutingMatch... data) {
        m_context = context;
        add(data);
    }

    @NonNull
    @Override
    public MatchHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        Log.i("UI_EVENT", "Initialized Match holder");
        return new MatchHolder(
                LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.holder_matches_sc, parent, false
                )
        );

    }

    @Override
    public void onBindViewHolder(@NonNull MatchHolder holder, int position) {
        holder.setMatch(m_context, m_data.get(position));
        Log.i("UI_EVENT", "Initializing Match list item: " + m_data.get(position).toString());
    }

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

    public void add(ScoutingMatch... matches) {

        int init = getItemCount();

        m_data.addAll(Arrays.asList(matches));

        for (int i = init; i < getItemCount(); i++) {
            notifyItemInserted(getItemCount());
        }
    }

holder_matches_sc.xml:

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

    <com.evergreen.treetop.ui.custom.text.CircleTextView
        android:id="@+id/sc_sched_text_match_holderid"
        android:layout_width="56dp"
        android:layout_height="58dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="28dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.evergreen.treetop.ui.custom.text.OvalTextView
        android:id="@+id/sc_sched_text_match_holder_time"
        android:layout_width="85dp"
        android:layout_height="31dp"
        android:layout_marginTop="28dp"
        android:layout_marginEnd="224dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.evergreen.treetop.ui.custom.text.OvalTextView
        android:id="@+id/sc_sched_text_match_holder_day"
        android:layout_width="85dp"
        android:layout_height="31dp"
        android:layout_marginTop="68dp"
        android:layout_marginEnd="224dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/sc_sched_text_match_holder_users"
        android:layout_width="151dp"
        android:layout_height="76dp"
        android:layout_marginTop="28dp"
        android:layout_marginEnd="36dp"
        android:background="@drawable/round_rectangle_10"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.783"
        app:layout_constraintStart_toEndOf="@+id/sc_sched_text_match_holder_time"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

activity_schedule_sc.xml (containing activity):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
    tools:context=".activities.scouts.schedule.SC_ScheduleActivity"
    android:orientation="vertical"
>

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

SC_ScheduleActivity.java (containing activity)

public class SC_ScheduleActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_schedule_sc);

        RecyclerView matchListView = findViewById(R.id.sc_rec_sched_main_list);
        matchListView.setAdapter(
                new MatchAdapter(
                        this,

                        new ScoutingMatch(
                                new MatchID(MatchID.MatchType.QUAL, 1),
                                Arrays.asList(
                                        new MatchTeam(1, "First FIRST", true, "abc1"),
                                        new MatchTeam(2, "second FIRST", true, "abc2"),
                                        new MatchTeam(3, "third FIRST", true, "abc3"),
                                        new MatchTeam(4, "fourth FIRST", false, "abc4"),
                                        new MatchTeam(5, "fifth FIRST", false, "abc5"),
                                        new MatchTeam(6, "sixth FIRST", false, "abc6")
                                )
                        ),

                        new ScoutingMatch(
                                new MatchID(MatchID.MatchType.QUAL, 2),
                                Arrays.asList(
                                        new MatchTeam(1, "First FIRST", true, "abc1"),
                                        new MatchTeam(5, "Fifth FIRST", true, "abc2"),
                                        new MatchTeam(9, "Ninth FIRST", true, "abc3"),
                                        new MatchTeam(4, "fourth FIRST", false, "abc4"),
                                        new MatchTeam(10, "Tenth FIRST", false, "abc5"),
                                        new MatchTeam(11, "Eleventh FIRST", false, "abc6")
                                )
                        ),

                        new ScoutingMatch(
                                new MatchID(MatchID.MatchType.PLAYOFF, 1),
                                Arrays.asList(
                                        new MatchTeam(1, "First", true, "abc1"),
                                        new MatchTeam(23, "TWENTY THREE", true, "abc2"),
                                        new MatchTeam(30, "NAMES", true, "abc3"),
                                        new MatchTeam(34, "fourth Thirtieth", false, "abc4"),
                                        new MatchTeam(54, "fifth Fourth", false, "abc5"),
                                        new MatchTeam(69, "Nice.", false, "abc6")
                                )
                        )
                )
        );


        matchListView.setLayoutManager(new LinearLayoutManager(this));


    }
}

Advertisement

Answer

change in this file holder_matches_sc.xml:

in

androidx.constraintlayout.widget.ConstraintLayout

make it’s android:layout_height="wrap_content"

also make RecyclerView android:layout_height="match_parent"

And you also forget to put recyclerView.setAdapter(Your_adpater_name); after the recyclerView.setLayoutManager() method.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement