英文:
Nothing shows up in RecyclerView
问题
我正在尝试掌握CardView并使用其RecyclerView。出于某种原因,在启动时没有显示任何内容。日志显示:“E / RecyclerView:adapter未连接;跳过布局”。告诉我问题是什么。受到这个视频的启发:https://www.youtube.com/watch?v=kaf2dCd8Zfs&list=PLrnPJCHvNZuBtTYUuc5Pyo4V7xZ2HNtf4&index=3
片段 Java
```java
public class NotificationsFragment extends Fragment {
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_notifications, container, false);
}
@Override
public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
ArrayList<Fragment_Card_Menu_list> notificationsList = new ArrayList<>();
notificationsList.add(new Fragment_Card_Menu_list("dawdawdaw"));
notificationsList.add(new Fragment_Card_Menu_list("dawdawdaw"));
notificationsList.add(new Fragment_Card_Menu_list("dawdawdaw"));
notificationsList.add(new Fragment_Card_Menu_list("dawdawdaw"));
notificationsList.add(new Fragment_Card_Menu_list("dawdawdaw"));
notificationsList.add(new Fragment_Card_Menu_list("dawdawdaw"));
notificationsList.add(new Fragment_Card_Menu_list("dawdawdaw"));
notificationsList.add(new Fragment_Card_Menu_list("dawdawdaw"));
notificationsList.add(new Fragment_Card_Menu_list("dawdawdaw"));
recyclerView = requireView().findViewById(R.id.recy_1);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
adapter = new NotificationsAdapter(notificationsList);
recyclerView.setAdapter(adapter);
}
}
```
片段 XML
```xml
<androidx.recyclerview.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recy_1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</androidx.recyclerview.widget.RecyclerView>
```
内部部分
```java
public class Fragment_Card_Menu_list {
private String Txv;
public Fragment_Card_Menu_list(String txv_1){
Txv = txv_1;
}
public String getText1(){
return Txv;
}
}
```
适配器
```java
public class NotificationsAdapter extends RecyclerView.Adapter<NotificationsAdapter.NotificationsHolder> {
private ArrayList<Fragment_Card_Menu_list> fragment_card_menu_lists;
public static class NotificationsHolder extends RecyclerView.ViewHolder {
public TextView textView;
public NotificationsHolder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.txv);
}
}
public NotificationsAdapter(ArrayList<Fragment_Card_Menu_list> fcmlList) {
fragment_card_menu_lists = fcmlList;
}
@NonNull
@Override
public NotificationsHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_card_menu_list, parent, false);
NotificationsHolder evh = new NotificationsHolder(view);
return evh;
}
@Override
public void onBindViewHolder(@NonNull NotificationsHolder holder, int position) {
Fragment_Card_Menu_list fragment_card_menu_list = fragment_card_menu_lists.get(position);
holder.textView.setText(fragment_card_menu_list.getText1());
}
@Override
public int getItemCount() {
return fragment_card_menu_lists.size();
}
}
```
英文:
I am trying to master CardView and use its RecyclerView. For some reason, nothing is displayed at startup. The logs say: “E / RecyclerView: adapter not connected; skip layout`. Tell me what the problem is. Inspired by this video https://www.youtube.com/watch?v=kaf2dCd8Zfs&list=PLrnPJCHvNZuBtTYUuc5Pyo4V7xZ2HNtf4&index=3
Fragment Java
public class NotificationsFragment extends Fragment {
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_notifications, container, false);
}
@Override
public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState)
{
ArrayList<Fragment_Card_Menu_list> notificationsList = new ArrayList<>();
notificationsList.add(new Fragment_Card_Menu_list("dawdawdaw"));
notificationsList.add(new Fragment_Card_Menu_list("dawdawdaw"));
notificationsList.add(new Fragment_Card_Menu_list("dawdawdaw"));
notificationsList.add(new Fragment_Card_Menu_list("dawdawdaw"));
notificationsList.add(new Fragment_Card_Menu_list("dawdawdaw"));
notificationsList.add(new Fragment_Card_Menu_list("dawdawdaw"));
notificationsList.add(new Fragment_Card_Menu_list("dawdawdaw"));
notificationsList.add(new Fragment_Card_Menu_list("dawdawdaw"));
notificationsList.add(new Fragment_Card_Menu_list("dawdawdaw"));
recyclerView = requireView().findViewById(R.id.recy_1);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
layoutManager = new LinearLayoutManager(getContext());
adapter = new NotificationsAdapter(notificationsList);
}
}
Fragment xml
<androidx.recyclerview.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recy_1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</androidx.recyclerview.widget.RecyclerView>
Inner part
public class Fragment_Card_Menu_list {
private String Txv;
public Fragment_Card_Menu_list(String txv_1){
Txv = txv_1;
}
public String getText1(){
return Txv;
}
}
Adapter
public class NotificationsAdapter extends RecyclerView.Adapter<NotificationsAdapter.NotificationsHolder> {
private ArrayList<Fragment_Card_Menu_list> fragment_card_menu_lists;
public static class NotificationsHolder extends RecyclerView.ViewHolder {
public TextView textView;
public NotificationsHolder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.txv);
}
}
public NotificationsAdapter(ArrayList<Fragment_Card_Menu_list> fcmlList) {
fragment_card_menu_lists = fcmlList;
}
@NonNull
@Override
public NotificationsHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_card_menu_list, parent, false);
NotificationsHolder evh = new NotificationsHolder(view);
return evh;
}
@Override
public void onBindViewHolder(@NonNull NotificationsHolder holder, int position) {
Fragment_Card_Menu_list fragment_card_menu_list = fragment_card_menu_lists.get(position);
holder.textView.setText(fragment_card_menu_list.getText1());
}
@Override
public int getItemCount() {
return fragment_card_menu_lists.size();
}
}
答案1
得分: 0
你正在在初始化适配器之前设置适配器。
将 recyclerView.setAdapter(adapter);
放在最后,即在 adapter = new NotificationsAdapter(notificationsList);
之后。
recyclerView = requireView().findViewById(R.id.recy_1);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
adapter = new NotificationsAdapter(notificationsList);
recyclerView.setAdapter(adapter); // <--- 这应该放在最后
英文:
You are setting the adapter before initialising it.
put recyclerView.setAdapter(adapter);
at the end,i.e., after adapter = new NotificationsAdapter(notificationsList);
recyclerView = requireView().findViewById(R.id.recy_1);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);
layoutManager = new LinearLayoutManager(getContext());
adapter = new NotificationsAdapter(notificationsList);
recyclerView.setAdapter(adapter); // <--- this should be at last
答案2
得分: 0
你需要设置 layoutManager:
recyclerView = findViewById(R.id.recy_1);
layoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
adapter = new NotificationsAdapter(notificationsList);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
英文:
you need set layoutManager :
recyclerView = findViewById(R.id.recy_1);
layoutManager = new LinearLayoutManager(this,RecyclerView.VERTICAL,false);
adapter = new NotificationsAdapter(notificationsList);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论