簡體   English   中英

回收站視圖僅顯示第一項,即使 onBindViewHolder 和 onCreateViewHolder 為所有項目運行

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

正如標題所說;

起初, onBindViewHolder只為第一項運行。 查看該站點上的問題和答案確實表明我需要將包裝視圖的高度設置為wrap_content 這確實解決了第一個問題,現在 logcat 顯示所有方法都運行正常; 但是,仍然只顯示第一項。

為什么會發生這種情況,我該如何解決?

MatchHolder(包含在 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)));

        }
    }

匹配適配器:

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(包含活動):

<?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(包含活動)

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));


    }
}

更改此文件holder_matches_sc.xml:

androidx.constraintlayout.widget.ConstraintLayout

使它成為android:layout_height="wrap_content"

還制作RecyclerView android:layout_height="match_parent"

而且你也忘了把recyclerView.setAdapter(Your_adpater_name); recyclerView.setLayoutManager() method之后。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM