[英]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.起初, onBindViewHolder
只为第一项运行。 Looking at questions and answers on this site indeed revealed that I needed to set the wrapping view's height to wrap_content
.查看该站点上的问题和答案确实表明我需要将包装视图的高度设置为wrap_content
。 This did fix the first issue and now logcat shows that all methods run properly;这确实解决了第一个问题,现在 logcat 显示所有方法都运行正常; However, still, only the first item is displayed.但是,仍然只显示第一项。
Why might this happen, and how might I fix it?为什么会发生这种情况,我该如何解决?
MatchHolder (contained in MatchAdapter): 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)));
}
}
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: 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): 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 (containing activity) 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));
}
}
change in this file holder_matches_sc.xml:
更改此文件holder_matches_sc.xml:
in在
androidx.constraintlayout.widget.ConstraintLayout
make it's android:layout_height="wrap_content"
使它成为android:layout_height="wrap_content"
also make RecyclerView
android:layout_height="match_parent"
还制作RecyclerView
android:layout_height="match_parent"
And you also forget to put recyclerView.setAdapter(Your_adpater_name);
而且你也忘了把recyclerView.setAdapter(Your_adpater_name);
after the recyclerView.setLayoutManager() method
.在recyclerView.setLayoutManager() method
之后。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.