Home>

Added recyclerView to my layout, wrote an adapter, all data is loaded and displayed normally. there are other items on the page, I was forced to disable scrolling in the recyclerView itself, otherwise it would hide behind other items and scroll on part of the screen. To make the scrolling of the screen look coherent, all the content was placed in the ScrollView, but when scrolling in the recycler was disabled, for some reason it began to display only 2 items from a list with 10 items.

Here is the function to display the 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);
}

Here is the adapter code:

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 ->{
            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,
                "<font color= '# FFFFFF'>" + film.getCat () + "</font>"),
                HtmlCompat.FROM_HTML_MODE_LEGACY));year.setText (HtmlCompat.fromHtml (context.getResources (). getString (R.string.year, "<font color= '# FFFFFF'>" + film.getYear () + "</font>"),
                HtmlCompat.FROM_HTML_MODE_LEGACY));
        String qualitiesString= listToString (film.getQualites ());
        qualities.setText (HtmlCompat.fromHtml (context.getResources (). getString (R.string.qualites,
                "<font color= '# FFFFFF'>" + qualitiesString + "</font>"),
                HtmlCompat.FROM_HTML_MODE_LEGACY));
        String translationsString= listToString (film.getTranslations ());
        translations.setText (HtmlCompat.fromHtml (context.getResources (). getString (R.string.translations,
                "<font color= '# FFFFFF'>" + translationsString + "</font>"),
                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);
}
}

Markup:

<? 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">
    <TextView
        android: id= "@ + id /toolbar_text"
        android: layout_width= "wrap_content"
        android: layout_height= "wrap_content"
        android: fontFamily= "@ font /montserrat_bold"
        android: text= "@ string /toolbar_text"
        android: textAllCaps= "true"
        android: textColor= "@ color /orange"
        android: textSize= "@ dimen /toolbar_text_size" />
</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">
    <androidx.constraintlayout.widget.ConstraintLayout
        android: id= "@ + id /rootView"
        android: layout_width= "match_parent"
        android: layout_height= "0dp">
        <androidx.recyclerview.widget.RecyclerView
            android: id= "@ + id /top_slider"
            android: layout_width= "match_parent"
            android: layout_height= "wrap_content"
            android: layout_margin= "@ dimen /dp20"
            android: background= "@ drawable /rounded_gray_background"
            android: padding= "@ dimen /dp15"
            app: layout_constraintEnd_toEndOf= "@ id /rootView"
            app: layout_constraintStart_toStartOf= "@ id /rootView"
            app: layout_constraintTop_toTopOf= "@ id /rootView" />
        <androidx.constraintlayout.widget.ConstraintLayout
            android: id= "@ + id /series_update"
            android: layout_width= "match_parent"
            android: layout_height= "@ dimen /dp50"
            android: layout_margin= "@ dimen /dp20"
            android: background= "@ drawable /rounded_gray_background"
            android: clickable= "true"
            android: focusable= "true"
            app: layout_constraintEnd_toEndOf= "@ id /rootView"
            app: layout_constraintStart_toStartOf= "@ id /rootView"
            app: layout_constraintTop_toBottomOf= "@ id /top_slider">
            <TextView
                android: id= "@ + id /series_update_text"
                android: layout_width= "wrap_content"
                android: layout_height= "wrap_content"
                android: fontFamily= "@ font /montserrat_bold"android: paddingStart= "@ dimen /dp15" android: text= "@ string /series_update"
                android: textColor= "@ color /white"
                android: textSize= "@ dimen /text_size"
                app: layout_constraintBottom_toBottomOf= "parent"
                app: layout_constraintStart_toStartOf= "parent"
                app: layout_constraintTop_toTopOf= "parent" />
            <ImageView
                android: id= "@ + id /series_update_icon"
                android: layout_width= "wrap_content"
                android: layout_height= "wrap_content"
                android: layout_gravity= "center_vertical"
                android: paddingEnd= "@ dimen /dp15"
                app: layout_constraintBottom_toBottomOf= "parent"
                app: layout_constraintEnd_toEndOf= "parent"
                app: layout_constraintTop_toTopOf= "parent"
                app: tint= "@ color /white" />
        </androidx.constraintlayout.widget.ConstraintLayout>
        <net.cachapa.expandablelayout.ExpandableLayout
            android: id= "@ + id /series_update_expandable"
            android: layout_width= "match_parent"
            android: layout_height= "0dp"
            app: el_duration= "1000"
            app: el_expanded= "false"
            app: el_parallax= "0.5"
            app: layout_constraintEnd_toEndOf= "@ id /rootView"
            app: layout_constraintStart_toStartOf= "@ id /rootView"
            app: layout_constraintTop_toBottomOf= "@ id /series_update">
            <androidx.recyclerview.widget.RecyclerView
                android: id= "@ + id /series_update_list"
                android: layout_width= "match_parent"
                android: layout_height= "match_parent"
                android: layout_margin= "@ dimen /dp20" />
        </net.cachapa.expandablelayout.ExpandableLayout>
        <FrameLayout
            android: id= "@ + id /filter_frame_layout"
            android: layout_width= "match_parent"
            android: layout_height= "wrap_content"
            android: layout_margin= "@ dimen /dp20"
            android: background= "@ drawable /rounded_orange_border"
            android: clickable= "true"
            android: focusable= "true"
            app: layout_constraintBottom_toTopOf= "@ id /orderText"
            app: layout_constraintEnd_toEndOf= "@ id /rootView"
            app: layout_constraintStart_toStartOf= "@ id /rootView"
            app: layout_constraintTop_toBottomOf= "@ id /series_update_expandable">
            <LinearLayout
                android: id= "@ + id /filter_layout"
                android: layout_width= "wrap_content"
                android: layout_height= "wrap_content"
                android: layout_gravity= "center_horizontal"
                android: padding= "@ dimen /dp15">
                <ImageView
                    android: id= "@ + id /filter_icon"
                    android: layout_width= "wrap_content"
                    android: layout_height= "wrap_content"
                    android: src= "@ drawable /filter"
                    app: tint= "@ color /orange" />
                <TextView
                    android: id= "@ + id /filter_text"
                    android: layout_width= "wrap_content"
                    android: layout_height= "wrap_content"
                    android: fontFamily= "@ font /montserrat_bold"
                    android: text= "@ string /filter"
                    android: textColor= "@ color /orange"
                    android: textSize= "@ dimen /text_size" />
            </LinearLayout>
        </FrameLayout>
        <TextView
            android: id= "@ + id /orderText"
            android: layout_width= "wrap_content"
            android: layout_height= "wrap_content"
            android: layout_marginStart= "@ dimen /dp20"
            android: layout_marginBottom= "@ dimen /dp20"
            android: fontFamily= "@ font /montserrat_bold"
            android: text= "@ string /sorted_by"
            android: textColor= "@ color /white"
            android: textSize= "@ dimen /text_size"
            app: layout_constraintBottom_toTopOf= "@ id /order_by_spinner"
            app: layout_constraintStart_toStartOf= "@ id /rootView"
            app: layout_constraintTop_toBottomOf= "@ id /filter_frame_layout" />
        <Spinner
            android: id= "@ + id /order_by_spinner"android: layout_width= "0dp" android: layout_height= "wrap_content"
            android: layout_marginStart= "@ dimen /dp20"
            android: layout_marginTop= "@ dimen /dp10"
            android: layout_marginEnd= "@ dimen /dp20"
            android: background= "@ drawable /spinner_style"
            app: layout_constraintEnd_toStartOf= "@ id /order"
            app: layout_constraintStart_toStartOf= "@ id /rootView"
            app: layout_constraintTop_toBottomOf= "@ id /orderText" />
        <FrameLayout
            android: id= "@ + id /order"
            android: layout_width= "wrap_content"
            android: layout_height= "wrap_content"
            android: clickable= "true"
            android: focusable= "true"
            app: layout_constraintEnd_toEndOf= "@ id /rootView"
            app: layout_constraintTop_toTopOf= "@ id /order_by_spinner"
            app: layout_constraintBottom_toBottomOf= "@ id /order_by_spinner">
            <ImageView
                android: id= "@ + id /order_desc"
                android: layout_width= "wrap_content"
                android: layout_height= "wrap_content"
                android: layout_marginEnd= "@ dimen /dp20"
                android: src= "@ drawable /sort_desc"
                android: padding= "@ dimen /dp10"
                android: background= "@ drawable /rounded_orange_border"
                app: tint= "@ color /orange" />
            <ImageView
                android: id= "@ + id /order_asc"
                android: layout_width= "wrap_content"
                android: layout_height= "wrap_content"
                android: layout_marginEnd= "@ dimen /dp20"
                android: visibility= "gone"
                android: src= "@ drawable /sort_asc"
                android: padding= "@ dimen /dp10"
                android: background= "@ drawable /rounded_orange_border"
                app: tint= "@ color /orange" />
        </FrameLayout>
        <androidx.recyclerview.widget.RecyclerView
            android: id= "@ + id /films_list"
            android: layout_width= "match_parent"
            android: layout_height= "wrap_content"
            android: layout_marginBottom= "@ dimen /dp20"
            app: layout_constraintStart_toStartOf= "@ id /rootView"
            app: layout_constraintEnd_toEndOf= "@ id /rootView"
            app: layout_constraintTop_toBottomOf= "@ id /order_by_spinner"
            app: layout_constraintBottom_toBottomOf= "@ id /rootView" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

Problem in recycler with id films_list.

Tried it, didn't help.

Feelini2021-02-23 18:30:15

Try to put the height of wrap_content in the ConstraintLayout inside the ScrollView, otherwise you have it now at 0, which seems to be wrong.

ЮрийСПб♦2021-02-23 18:30:15

Added markup.

Feelini2021-02-23 18:30:15

So yes, most likely there is trouble in the markup. On the other hand, putting a RecyclewView in a ScrollView is usually a bad idea. what for then in general RecyclewView. Usually it is more correct -everything that is around the RecyclewView needs to be placed in the RecyclewView through the mechanism for displaying different types of cells.

ЮрийСПб♦2021-02-23 18:30:15

I think you need to look at the layout

DrMcSheen2021-02-23 18:30:15
  • Answer # 1
    1. Remove ScrollView, it is not used with recyclerView.
    2. Use different types and load them into the list.

    Thanks for the answer. Can you tell me a good article on various types of data in recyclerview?

    Feelini2021-02-23 18:30:15