RecyclerView中没有显示任何内容。

huangapple go评论57阅读模式
英文:

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&amp;list=PLrnPJCHvNZuBtTYUuc5Pyo4V7xZ2HNtf4&amp;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&lt;Fragment_Card_Menu_list&gt; notificationsList = new ArrayList&lt;&gt;();
        notificationsList.add(new Fragment_Card_Menu_list(&quot;dawdawdaw&quot;));
        notificationsList.add(new Fragment_Card_Menu_list(&quot;dawdawdaw&quot;));
        notificationsList.add(new Fragment_Card_Menu_list(&quot;dawdawdaw&quot;));
        notificationsList.add(new Fragment_Card_Menu_list(&quot;dawdawdaw&quot;));
        notificationsList.add(new Fragment_Card_Menu_list(&quot;dawdawdaw&quot;));
        notificationsList.add(new Fragment_Card_Menu_list(&quot;dawdawdaw&quot;));
        notificationsList.add(new Fragment_Card_Menu_list(&quot;dawdawdaw&quot;));
        notificationsList.add(new Fragment_Card_Menu_list(&quot;dawdawdaw&quot;));
        notificationsList.add(new Fragment_Card_Menu_list(&quot;dawdawdaw&quot;));


        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

&lt;androidx.recyclerview.widget.RecyclerView 
    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:id=&quot;@+id/recy_1&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;wrap_content&quot;&gt;
&lt;/androidx.recyclerview.widget.RecyclerView&gt;

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&lt;NotificationsAdapter.NotificationsHolder&gt; {
    private ArrayList&lt;Fragment_Card_Menu_list&gt; 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&lt;Fragment_Card_Menu_list&gt; 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); // &lt;--- 这应该放在最后
英文:

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); // &lt;--- 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);

huangapple
  • 本文由 发表于 2020年8月7日 12:11:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63295071.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定