0

Добавил recyclerView в свою верстку, написал адаптер, все данные подгружаются и отображаются нормально, но, т.к. на странице есть другие элементы, я был вынужден отключить прокрутку в самом recyclerView, иначе он скрывался за другими элементами и прокручивался на части экрана. Чтобы прокрутка экрана выглядела целостно, весь контент был помещен в ScrollView, но, когда прокрутка в recycler была отключена, он почему-то стал отображать только 2 элемента из списка, в котором 10 элементов.

Вот функция для отображения recycler:

private void showFilmsList(FilmsList filmsList) {
    filmsListAdapter = new FilmsListAdapter(getContext(), filmsList.getData(), (OnClickListener) getContext(), getBaseUrl());
    filmsListView.setAdapter(filmsListAdapter);
    filmsListView.setLayoutManager(new LinearLayoutManager(getContext(), RecyclerView.VERTICAL, false));
    filmsListView.setNestedScrollingEnabled(false);
}

Вот код адаптера:

package x.x.x.ui.main;

import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView;

import androidx.annotation.NonNull; import androidx.core.text.HtmlCompat; import androidx.recyclerview.widget.RecyclerView;

import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.BindView; import butterknife.ButterKnife; import by.seobility.kinoclub.R; import by.seobility.kinoclub.repo.models.Film; import by.seobility.kinoclub.utils.OnClickListener;

public class FilmsListAdapter extends RecyclerView.Adapter<FilmsListAdapter.FilmsListViewHolder> {

private List<Film> films; private String baseUrl; private Context context; OnClickListener onClickListener;

public FilmsListAdapter(Context context, List<Film> films, OnClickListener onClickListener, String baseUrl) { this.films = films; this.context = context; this.onClickListener = onClickListener; this.baseUrl = baseUrl; }

@NonNull @Override public FilmsListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_films_list, parent, false); return new FilmsListAdapter.FilmsListViewHolder(view); }

@Override public void onBindViewHolder(@NonNull FilmsListAdapter.FilmsListViewHolder holder, int position) { holder.bindData(films.get(position)); }

@Override public int getItemCount() { return films != null ? films.size() : 0; }

public class FilmsListViewHolder extends RecyclerView.ViewHolder { @BindView(R.id.films_list_title) TextView title; @BindView(R.id.films_list_poster) ImageView poster; @BindView(R.id.films_list_rate_text) TextView rateText; @BindView(R.id.films_list_category) TextView category; @BindView(R.id.films_list_year) TextView year; @BindView(R.id.films_list_qualities) TextView qualities; @BindView(R.id.films_list_translations) TextView translations;

public FilmsListViewHolder(@NonNull View itemView) {
    super(itemView);
    ButterKnife.bind(this, itemView);
}

public void bindData(Film film) {
    itemView.setOnClickListener(v -&gt; {
        if (onClickListener != null){
            onClickListener.onFilmClick(film);
        }
    });

    title.setText(film.getTitle());
    String posterUrl = baseUrl + film.getPoster();
    Picasso.get().load(posterUrl).into(poster);
    rateText.setText(film.getRating());
    category.setText(HtmlCompat.fromHtml(context.getResources().getString(R.string.category,
            &quot;&lt;font color='#FFFFFF'&gt;&quot; + film.getCat() + &quot;&lt;/font&gt;&quot;),
            HtmlCompat.FROM_HTML_MODE_LEGACY));
    year.setText(HtmlCompat.fromHtml(context.getResources().getString(R.string.year,
            &quot;&lt;font color='#FFFFFF'&gt;&quot; + film.getYear() + &quot;&lt;/font&gt;&quot;),
            HtmlCompat.FROM_HTML_MODE_LEGACY));
    String qualitiesString = listToString(film.getQualites());
    qualities.setText(HtmlCompat.fromHtml(context.getResources().getString(R.string.qualites,
            &quot;&lt;font color='#FFFFFF'&gt;&quot; + qualitiesString + &quot;&lt;/font&gt;&quot;),
            HtmlCompat.FROM_HTML_MODE_LEGACY));
    String translationsString = listToString(film.getTranslations());
    translations.setText(HtmlCompat.fromHtml(context.getResources().getString(R.string.translations,
            &quot;&lt;font color='#FFFFFF'&gt;&quot; + translationsString + &quot;&lt;/font&gt;&quot;),
            HtmlCompat.FROM_HTML_MODE_LEGACY));
}

}

private String listToString(List<String> list){ if (list.isEmpty()) return "false"; StringBuilder return_string = new StringBuilder(); for (String s : list){ return_string.append(s).append(", "); } return return_string.toString().substring(0, return_string.length() - 2); } }

Разметка:

<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/dark_grey"
tools:context=".ui.main.MainFragment">

<androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="@dimen/dp60" android:background="@color/black" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent">

&lt;TextView
    android:id=&quot;@+id/toolbar_text&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:fontFamily=&quot;@font/montserrat_bold&quot;
    android:text=&quot;@string/toolbar_text&quot;
    android:textAllCaps=&quot;true&quot;
    android:textColor=&quot;@color/orange&quot;
    android:textSize=&quot;@dimen/toolbar_text_size&quot; /&gt;

</androidx.appcompat.widget.Toolbar>

<ScrollView android:id="@+id/scroll" android:layout_width="match_parent" android:layout_height="0dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/toolbar">

&lt;androidx.constraintlayout.widget.ConstraintLayout
    android:id=&quot;@+id/rootView&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;0dp&quot;&gt;

    &lt;androidx.recyclerview.widget.RecyclerView
        android:id=&quot;@+id/top_slider&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_margin=&quot;@dimen/dp20&quot;
        android:background=&quot;@drawable/rounded_gray_background&quot;
        android:padding=&quot;@dimen/dp15&quot;
        app:layout_constraintEnd_toEndOf=&quot;@id/rootView&quot;
        app:layout_constraintStart_toStartOf=&quot;@id/rootView&quot;
        app:layout_constraintTop_toTopOf=&quot;@id/rootView&quot; /&gt;

    &lt;androidx.constraintlayout.widget.ConstraintLayout
        android:id=&quot;@+id/series_update&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;@dimen/dp50&quot;
        android:layout_margin=&quot;@dimen/dp20&quot;
        android:background=&quot;@drawable/rounded_gray_background&quot;
        android:clickable=&quot;true&quot;
        android:focusable=&quot;true&quot;
        app:layout_constraintEnd_toEndOf=&quot;@id/rootView&quot;
        app:layout_constraintStart_toStartOf=&quot;@id/rootView&quot;
        app:layout_constraintTop_toBottomOf=&quot;@id/top_slider&quot;&gt;

        &lt;TextView
            android:id=&quot;@+id/series_update_text&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:fontFamily=&quot;@font/montserrat_bold&quot;
            android:paddingStart=&quot;@dimen/dp15&quot;
            android:text=&quot;@string/series_update&quot;
            android:textColor=&quot;@color/white&quot;
            android:textSize=&quot;@dimen/text_size&quot;
            app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
            app:layout_constraintStart_toStartOf=&quot;parent&quot;
            app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;

        &lt;ImageView
            android:id=&quot;@+id/series_update_icon&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_gravity=&quot;center_vertical&quot;
            android:paddingEnd=&quot;@dimen/dp15&quot;
            app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
            app:layout_constraintEnd_toEndOf=&quot;parent&quot;
            app:layout_constraintTop_toTopOf=&quot;parent&quot;
            app:tint=&quot;@color/white&quot; /&gt;

    &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

    &lt;net.cachapa.expandablelayout.ExpandableLayout
        android:id=&quot;@+id/series_update_expandable&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;0dp&quot;
        app:el_duration=&quot;1000&quot;
        app:el_expanded=&quot;false&quot;
        app:el_parallax=&quot;0.5&quot;
        app:layout_constraintEnd_toEndOf=&quot;@id/rootView&quot;
        app:layout_constraintStart_toStartOf=&quot;@id/rootView&quot;
        app:layout_constraintTop_toBottomOf=&quot;@id/series_update&quot;&gt;

        &lt;androidx.recyclerview.widget.RecyclerView
            android:id=&quot;@+id/series_update_list&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;match_parent&quot;
            android:layout_margin=&quot;@dimen/dp20&quot; /&gt;

    &lt;/net.cachapa.expandablelayout.ExpandableLayout&gt;

    &lt;FrameLayout
        android:id=&quot;@+id/filter_frame_layout&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_margin=&quot;@dimen/dp20&quot;
        android:background=&quot;@drawable/rounded_orange_border&quot;
        android:clickable=&quot;true&quot;
        android:focusable=&quot;true&quot;
        app:layout_constraintBottom_toTopOf=&quot;@id/orderText&quot;
        app:layout_constraintEnd_toEndOf=&quot;@id/rootView&quot;
        app:layout_constraintStart_toStartOf=&quot;@id/rootView&quot;
        app:layout_constraintTop_toBottomOf=&quot;@id/series_update_expandable&quot;&gt;

        &lt;LinearLayout
            android:id=&quot;@+id/filter_layout&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_gravity=&quot;center_horizontal&quot;
            android:padding=&quot;@dimen/dp15&quot;&gt;

            &lt;ImageView
                android:id=&quot;@+id/filter_icon&quot;
                android:layout_width=&quot;wrap_content&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:src=&quot;@drawable/filter&quot;
                app:tint=&quot;@color/orange&quot; /&gt;

            &lt;TextView
                android:id=&quot;@+id/filter_text&quot;
                android:layout_width=&quot;wrap_content&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:fontFamily=&quot;@font/montserrat_bold&quot;
                android:text=&quot;@string/filter&quot;
                android:textColor=&quot;@color/orange&quot;
                android:textSize=&quot;@dimen/text_size&quot; /&gt;

        &lt;/LinearLayout&gt;

    &lt;/FrameLayout&gt;

    &lt;TextView
        android:id=&quot;@+id/orderText&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginStart=&quot;@dimen/dp20&quot;
        android:layout_marginBottom=&quot;@dimen/dp20&quot;
        android:fontFamily=&quot;@font/montserrat_bold&quot;
        android:text=&quot;@string/sorted_by&quot;
        android:textColor=&quot;@color/white&quot;
        android:textSize=&quot;@dimen/text_size&quot;
        app:layout_constraintBottom_toTopOf=&quot;@id/order_by_spinner&quot;
        app:layout_constraintStart_toStartOf=&quot;@id/rootView&quot;
        app:layout_constraintTop_toBottomOf=&quot;@id/filter_frame_layout&quot; /&gt;

    &lt;Spinner
        android:id=&quot;@+id/order_by_spinner&quot;
        android:layout_width=&quot;0dp&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginStart=&quot;@dimen/dp20&quot;
        android:layout_marginTop=&quot;@dimen/dp10&quot;
        android:layout_marginEnd=&quot;@dimen/dp20&quot;
        android:background=&quot;@drawable/spinner_style&quot;
        app:layout_constraintEnd_toStartOf=&quot;@id/order&quot;
        app:layout_constraintStart_toStartOf=&quot;@id/rootView&quot;
        app:layout_constraintTop_toBottomOf=&quot;@id/orderText&quot; /&gt;

    &lt;FrameLayout
        android:id=&quot;@+id/order&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:clickable=&quot;true&quot;
        android:focusable=&quot;true&quot;
        app:layout_constraintEnd_toEndOf=&quot;@id/rootView&quot;
        app:layout_constraintTop_toTopOf=&quot;@id/order_by_spinner&quot;
        app:layout_constraintBottom_toBottomOf=&quot;@id/order_by_spinner&quot;&gt;

        &lt;ImageView
            android:id=&quot;@+id/order_desc&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_marginEnd=&quot;@dimen/dp20&quot;
            android:src=&quot;@drawable/sort_desc&quot;
            android:padding=&quot;@dimen/dp10&quot;
            android:background=&quot;@drawable/rounded_orange_border&quot;
            app:tint=&quot;@color/orange&quot; /&gt;

        &lt;ImageView
            android:id=&quot;@+id/order_asc&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_marginEnd=&quot;@dimen/dp20&quot;
            android:visibility=&quot;gone&quot;
            android:src=&quot;@drawable/sort_asc&quot;
            android:padding=&quot;@dimen/dp10&quot;
            android:background=&quot;@drawable/rounded_orange_border&quot;
            app:tint=&quot;@color/orange&quot; /&gt;

    &lt;/FrameLayout&gt;

    &lt;androidx.recyclerview.widget.RecyclerView
        android:id=&quot;@+id/films_list&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginBottom=&quot;@dimen/dp20&quot;
        app:layout_constraintStart_toStartOf=&quot;@id/rootView&quot;
        app:layout_constraintEnd_toEndOf=&quot;@id/rootView&quot;
        app:layout_constraintTop_toBottomOf=&quot;@id/order_by_spinner&quot;
        app:layout_constraintBottom_toBottomOf=&quot;@id/rootView&quot;/&gt;

&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

</ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

Проблема в recycler с id films_list.

  • 1
    думаю, тут нужно на вёрстку смотреть – DrMcSheen Feb 01 '21 at 12:39
  • 1
    Таки да, скорее всего в разметке беда. С другой стороны - помещать RecyclewView в ScrollView - обычно плохая идея, т.к. нафига тогда вообще RecyclewView. Обычно правильнее - всё что вокруг RecyclewView надо в RecyclewView же и поместить, через механизм отображения разных типов ячеек. – ЮрийСПб Feb 01 '21 at 12:49
  • Добавил разметку. – Feelini Feb 01 '21 at 13:30
  • Попробуйте у ConstraintLayout внутри ScrollView поставить высоту wrap_content, а то она у вас сейчас 0, что представляется неправильным. – ЮрийСПб Feb 01 '21 at 16:10
  • Пробовал, не помогло. – Feelini Feb 01 '21 at 16:15

1 Answers1

0
  1. Уберите ScrollView, он не используется вместе с recyclerView.
  2. Используйте разные типы и подгружайте их в список.
SMD 70
  • 53
  • 4
  • Спасибо за ответ. Не подскажете статью хорошую по различным типам данных в recyclerview? – Feelini Feb 05 '21 at 05:04
  • @Feelini попробуйте посмотреть вот эту тему https://ru.stackoverflow.com/questions/510879/Как-добавить-в-recyclerview-разные-элемент/510887#510887 – SMD 70 Feb 05 '21 at 15:11
  • Спасибо, буду разбираться. – Feelini Feb 05 '21 at 16:36
  • Если кому-то будет полезно, вот еще нашел такую статью: https://ziginsider.github.io/Multiple_Row_Types_In_Recyclerview/ Хотя ссылка может потерять свою актуальность. – Feelini Feb 05 '21 at 16:41
  • Сегодня решил эту проблему. Вместо ScrollView необходимо использовать NestedScrollView, а в recyclerView установить флажок android:nestedScrollingEnabled="false" – Feelini Mar 09 '21 at 12:31