'RecyclerView list shows 4 item only(have 200 items) and cant scroll the list

This is my Recycler View adapter class:

public  class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.viewHolder> implements Filterable {

    Context context;
    OnBoardingFirstScreen activity;

    public static ArrayList<CountryCodeModel> arrayList,arrayListFiltered;


    public CustomAdapter(Context context, ArrayList<CountryCodeModel> arrayList, OnBoardingFirstScreen activity) {
        this.context = context;
        this.arrayList = arrayList;
        this.arrayListFiltered = arrayList;
        this.activity = activity;

    }

    @Override
    public  viewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.country_code_item, viewGroup, false);
        /*view.measure(
                View.MeasureSpec.makeMeasureSpec(200, View.MeasureSpec.EXACTLY),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));*/
        return new viewHolder(view);
    }

    @Override
    public  void onBindViewHolder(viewHolder viewHolder, int position) {


//        viewHolder.name.setText(arrayListFiltered.get(position).getCountry()+ " " +
//                "(" + arrayListFiltered.get(position).getCode() + ")");
        String textdotted = arrayListFiltered.get(position).getCountry();

        if (textdotted.length() >= 30) {
            textdotted= textdotted.substring(0, 30)+ "...";
        } else {
            textdotted= textdotted;

        }
        viewHolder.name.setText(textdotted+ " (" + arrayListFiltered.get(position).getCode() + ")");

        int imagee =arrayListFiltered.get(position).getFlag();

        Picasso.get().load(imagee).resize(300, 300).centerCrop().into(viewHolder.image);
        Picasso.get().load(imagee).fit();
        //Picasso.get().load(imagee).fit().resize(300, 300).centerCrop().into(viewHolder.image);



        viewHolder.layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((OnBoardingFirstScreen)activity).onClickCountryItem(position, arrayListFiltered);

            }
        });


    }

    @Override
    public int getItemCount() {
        int limit = 4;
        if(arrayListFiltered.size()> limit){
            return limit;
        }
        else {
            return arrayListFiltered.size();
        }

    }

    public class viewHolder extends RecyclerView.ViewHolder {
        TextView name;
        ShapeableImageView image;
        LinearLayout layout;

        public viewHolder(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.country_name);
            image = (ShapeableImageView) itemView.findViewById(R.id.flagimg);
            layout = (LinearLayout) itemView.findViewById(R.id.layout);

//            itemView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//
//
//                    Toast.makeText(context,arrayListFiltered.get(getAdapterPosition()).getCountry(),Toast.LENGTH_LONG).show();
//                }
//            });
        }


    }

    @Override
    public Filter getFilter() {
        Filter filter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults results = new FilterResults();

                ArrayList<CountryCodeModel> arrayListFilter = new ArrayList<CountryCodeModel>();

                if(constraint == null|| constraint.length() == 0) {
                    results.count = arrayList.size();
                    results.values = arrayList;

                } else {
                    for (CountryCodeModel itemModel : arrayList) {
                        if(itemModel.getCountry().toLowerCase().contains(constraint.toString().toLowerCase())) {
                            arrayListFilter.add(itemModel);
                        }else if(itemModel.getCode().contains(constraint.toString())) {
                            arrayListFilter.add(itemModel);
                        }
                    }
                    results.count = arrayListFilter.size();
                    results.values = arrayListFilter;

                }

                return results;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                arrayListFiltered = (ArrayList<CountryCodeModel>) results.values;
                if (results.count == 0) {
                    ((OnBoardingFirstScreen)activity).NoResultfound(results.count);
                    arrayListFiltered.clear();
                    notifyDataSetChanged();
                }
                else {
                    ((OnBoardingFirstScreen)activity).NoResultfound(results.count);
                    arrayListFiltered = (ArrayList<CountryCodeModel>)results.values;
                    notifyDataSetChanged();
                }

            }
        };
        return filter;
    }

}

This is my recyclerview XML part:

 <androidx.recyclerview.widget.RecyclerView
  android:layout_width="@dimen/_200sdp"
  android:layout_height="wrap_content"
  android:fitsSystemWindows="true"
  android:layout_marginBottom="@dimen/_4sdp"
  android:layout_marginStart="@dimen/_2sdp"
  android:id="@+id/country_codes_rv"/>

in custom adapter class if i set getItemCount() to return the size instead of limit it loads all 200 items and in xml it is wrap content lists gets out of the screen.

I want to load 4 items at a time in my list and want my recyclerview list scrollable. i dont want to give it a specific height. it will distort my view.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source