В RecyclerView ничего не отображается - PullRequest
1 голос
/ 07 августа 2020

Я пытаюсь освоить CardView и использовать его RecyclerView. Почему-то при запуске ничего не отображается. В журналах написано: «E / RecyclerView: адаптер не подключен; пропустить макет`. Подскажите в чем проблема. На основе этого видео https://www.youtube.com/watch?v=kaf2dCd8Zfs&list=PLrnPJCHvNZuBtTYUuc5Pyo4V7xZ2HNtf4&index=3

Фрагмент 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);
    }
}

Фрагмент 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>

Внутренняя часть

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

Адаптер

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

Ответы [ 2 ]

0 голосов
/ 07 августа 2020

необходимо установить 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);
0 голосов
/ 07 августа 2020

Вы настраиваете адаптер перед его инициализацией.

ставьте recyclerView.setAdapter(adapter); в конце, т.е. после 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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...