1

Вопрос довольно простой, но решить его не получается. Во фрагменте recyclerView с карточками. Фрагмент и recyclerView отображаются, карточки нет. Карточка довольно крупная, под тулбаром прятаться не может.

public class PoemFragment extends Fragment {
        private List<ModelPoem> list;
        private RecyclerView recyclerView;
        private RecyclerView.Adapter adapter;


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootViewPoem = inflater.inflate(R.layout.fragment_poem, container, false);

            list = new ArrayList<>();
            recyclerView = (RecyclerView) rootViewPoem.findViewById(R.id.rvPoem); //       
    mRecyclerView.setHasFixedSize(true);

            LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
            mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            recyclerView.setLayoutManager(mLayoutManager);
            adapter = new MyAdapter(list);
            recyclerView.setAdapter(adapter);
            return rootViewPoem;

        } }



  public class MyAdapter extends RecyclerView.Adapter<MyAdapter. MyViewHolder> {
    private List<ModelPoem> list;

    public MyAdapter(List<ModelPoem> list) {

        this.list = list;
    }

    @NonNull
    @Override
    public MyAdapter. MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyAdapter. MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.model_poem, parent, false));

    }

    @Override
    public void onBindViewHolder(@NonNull MyAdapter. MyViewHolder holder, int position) {
        ModelPoem modelPoem = list.get(position);
        holder.title.setText(modelPoem.getTitle());
        holder.poem.setText(modelPoem.getPoem());
        holder.years.setText(modelPoem.getYears());
        holder.subject.setText(modelPoem.getSubject());


    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView title;
        TextView poem;
        CardView cv;
        TextView subject;
        TextView years;
        ImageView subject_image;
        ImageView years_image;

        MyViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.tv_title);
            poem = (TextView) itemView.findViewById(R.id.tv_poem);
            cv = (CardView) itemView.findViewById(R.id.cv);
            subject = (TextView) itemView.findViewById(R.id.tv_subject);
            years = (TextView) itemView.findViewById(R.id.tv_years);
            subject_image = (ImageView) itemView.findViewById(R.id.iv_subject);
            years_image = (ImageView) itemView.findViewById(R.id.iv_years);

разметка (текстовые данные заполнены лишь для проверки)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="8dp"
    android:paddingRight="8dp"
    android:paddingTop="8dp">

    <android.support.v7.widget.CardView
        android:id="@+id/cv"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        app:cardCornerRadius="15dp">

        <LinearLayout
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:background="?android:attr/selectableItemBackground"
            android:orientation="vertical">

            <FrameLayout
                android:layout_height="42dp"
                android:layout_width="match_parent"
                android:background="@color/colorAccent"
                android:elevation="10dp"
                android:paddingStart="30dp"
                android:paddingEnd="20dp">

                <TextView
                    android:id="@+id/tv_title"
                    android:layout_height="wrap_content"
                    android:layout_width="match_parent"
                    android:layout_gravity="center"
                    android:singleLine="true"
                    android:textSize="17sp"
                    android:text="title"/>
            </FrameLayout>

            <LinearLayout
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:background="@color/white"
                android:orientation="vertical"
                android:gravity="center_horizontal"
                android:paddingTop="5dp"
                android:paddingLeft="5dp"
                android:paddingBottom="5dp"
                android:paddingRight="5dp">

                <TextView
                    android:id="@+id/tv_poem"
                    android:layout_height="wrap_content"
                    android:layout_width="match_parent"
                    android:gravity="center"
                    android:paddingBottom="10dp"
                    android:paddingTop="10dp"
                    android:maxLines="4"
                    android:textSize="15sp"
                    android:text="test....................
.........................................................."/>

            </LinearLayout>

            <View
                android:layout_height="1dp"
                android:layout_width="fill_parent"
                android:background="@color/separator" />

            <LinearLayout
                android:layout_height="42dp"
                android:layout_width="match_parent"
                android:background="@color/cardview_dark_background"
                android:divider="@drawable/separator"
                android:dividerPadding="3dp"
                android:gravity="center_vertical"
                android:orientation="horizontal"
                android:showDividers="middle">

                <LinearLayout
                    android:layout_height="wrap_content"
                    android:layout_width="match_parent"
                    android:layout_weight="1"
                    android:gravity="center_vertical|right"
                    android:orientation="horizontal"
                    android:paddingEnd="30dp">

                    <ImageView
                        android:id="@+id/iv_subject"
                        android:layout_width="18dp"
                        android:layout_height="18dp"
                        android:layout_gravity="center_vertical"
                        android:layout_marginEnd="5dp"
                        android:src="@drawable/pound" />

                    <TextView
                        android:id="@+id/tv_subject"
                        android:layout_height="wrap_content"
                        android:layout_width="match_parent"
                        android:layout_gravity="center_vertical"
                        android:textColor="@color/black"
                        android:textSize="15sp"
                        android:text="subject"/>

                </LinearLayout>

                <LinearLayout
                    android:layout_height="wrap_content"
                    android:layout_width="match_parent"
                    android:layout_weight="1"
                    android:gravity="center_vertical"
                    android:orientation="horizontal"
                    android:paddingStart="30dp"
                    android:background="@color/colorAccent">

                    <ImageView
                        android:id="@+id/iv_years"
                        android:layout_width="18dp"
                        android:layout_height="18dp"
                        android:layout_gravity="center_vertical"
                        android:layout_marginEnd="5dp"
                        android:cropToPadding="false"
                        android:src="@drawable/calendar_range" />

                    <TextView
                        android:id="@+id/tv_years"
                        android:layout_height="wrap_content"
                        android:layout_width="match_parent"
                        android:layout_gravity="center_vertical"
                        android:textColor="@color/black"
                        android:textSize="15sp"
                        android:text="years"/>

                </LinearLayout>


            </LinearLayout>
        </LinearLayout>
    </android.support.v7.widget.CardView>
</RelativeLayout>

разметка фрагмента

   <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".fragment.PoemFragment">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/rvPoem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:clipToPadding="false"
        android:paddingBottom="88dp">

    </android.support.v7.widget.RecyclerView>

</RelativeLayout>
8
  • Может вы её просто не замечаете? Попробуйте цвет фона у неё поменять. Ну или деталей добавьте в вопрос - пока не ясно где эта карточка у вас в разметке должна быть и самой разметки также просто не видно в вопросе.
    – ЮрийСПб
    27 окт 2018 в 21:25
  • @ЮрийСПб добавила разметку. Цвета там разные и есть иконки
    – GregorSa
    27 окт 2018 в 21:41
  • Кажется очень странным android:layout_height="match_parent" во многих вьюхах. Особенно в первом потоке самой карточки. Попробуйте wrap_content проставить.
    – ЮрийСПб
    27 окт 2018 в 21:43
  • @ЮрийСПб Поменяла и обновила разметку в вопросе. Не помогло.
    – GregorSa
    27 окт 2018 в 21:57
  • 4
    Если вы показали весь код, то данных в адаптере у вас нет и потому и не отображается ничего. Вы в адаптер передаёте list = new ArrayList<>() - т.е. пустой список. Или вы его потом где-то в другом месте наполняете, но не вызываете метод адаптера для его перерисовки. Сама по себе карточка не появится, если никаких данных для адаптера нет.
    – ЮрийСПб
    27 окт 2018 в 22:55

1 ответ 1

1

Данных в адаптере у вас нет и потому и не отображается ничего. Вы в адаптер передаёте list = new ArrayList<>() - т.е. пустой список. Или вы его потом где-то в другом месте наполняете, но не вызываете метод адаптера для его перерисовки (notifyDataSetChanged()). Сама по себе карточка не появится, если никаких данных для адаптера нет.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.