1

enter code hereI'm trying to create a recyclerview to display only 3 items everytime it executes then there is a View More to see more items.

ServicesAdapter 


@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
    TypeProducts str = services.get(myViewHolder.getAdapterPosition());

    myViewHolder.title.setText(str.getName());
    Glide.with(context).load(str.getImageUrl()).into(myViewHolder.imageView);

}

ServiceScreenAdapter

public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
    TypeServices str = typeServices.get(myViewHolder.getAdapterPosition());


    Log.d("getsize",String.valueOf(typeServices.size()));


    ServicesAdapter serviceadapter = new ServicesAdapter(context, str.getProducts());


    myViewHolder.tvTitle.setText(str.getName());
    myViewHolder.rclist.setAdapter(serviceadapter);

    myViewHolder.tvViewMore.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });


}
Allen Oculam
  • 51
  • 1
  • 9

2 Answers2

0

You mean that you want 3 items to be displayed, but each time they run, the items get bigger? You may forget to delete the previous items and add 3 new items. Or edit 3 items with new items.

yourList.set(itemPosition, yourObject);
                                            
 yourRecyclerAdapter.notifyItemChanged(itemPosition);

Or

    for(int i=0 ;i< mRecyclerView.getAdapter().getItemCount();i++)
    {
        msgDtoList.remove(i);   
    }                                    
    yourRecyclerAdapter.notifyItemRangeRemoved(0, 
    msgRecyclerView.getAdapter().getItemCount() - 1);

and then add new items.

EDIT:

add this code to your adapter class:

public Filter getFilter(bool getFirsts)
    {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence charSequence) {
             List<YourObject> filteredList=new ArrayList<>();
            if(getFirsts){
                String s=charSequence.toString();
                YourObjectListFiltered=YourObjectList;
               
                for (YourObject row:YourObjectList)
                {
                     //Write here any condition that separates those 3 items from others.
                    if(row.getItemPosition()>=0 && row.getItemPosition()<=2)
                        filteredList.add(row);
                }
            }else{
                filteredList=YourObjectList;
             }
                YourObjectListFiltered=filteredList;

                FilterResults filterResults=new FilterResults();
                filterResults.values=surveyTitlesFiltered;
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
                YourObjectListFiltered=(ArrayList<YourObject>)filterResults.values;
                notifyDataSetChanged();
            }
        };
    }

call it : yourRecyclerAdapter.getFilter(true).filter("");

Community
  • 1
  • 1
reyhane
  • 183
  • 1
  • 15
  • not to delete the item just to hide the other just to display the first 3 then when you click View more the all the data will show – Allen Oculam Jul 10 '19 at 05:13
0

From what i understand you want to display 3 items and then press a btn to start a new activity where the next 3 items will appear. You can pass to with the intent the position of the new value each time, create a temp list that has the 3 next items and pass that to the recycler view. This could work but i dont think thats a very efficient way because you will be creating a lot new activities. Check this for pagination in android.

---edit---

Then you can have a value to save the position and when the btn is pressed add the next items on your temp list and update the recycler view.

Chris Papantonis
  • 720
  • 7
  • 18