1

i am using a recylcer view to display 5 predifined items. each item contains a text and an image. this is how i am loading them. i am using Picasso also to load the images but the images does not even exceed 500kb but when i fast scroll down, there is a lag. any help will be appreciated.

in the MainActivity:

List<ItemObject> rowListItem = getAllItemList();
lLayout = new LinearLayoutManager(MainActivity.this);
RecyclerView rView = (RecyclerView)findViewById(R.id.recycler_view);
rView.setLayoutManager(lLayout);
RecyclerViewAdapter rcAdapter = new RecyclerViewAdapter(MainActivity.this, rowListItem);
rView.setAdapter(rcAdapter);

 private List<ItemObject> getAllItemList(){
        List<ItemObject> allItems = new ArrayList<ItemObject>();
        allItems.add(new ItemObject("Add a Task", R.drawable.addtask));
        allItems.add(new ItemObject("Submit Complaint", R.drawable.noelec));
        allItems.add(new ItemObject("View Consumption",R.drawable.statistiw));
        allItems.add(new ItemObject("About Us", R.drawable.aboutus));
        allItems.add(new ItemObject("Track Field Team", R.drawable.ft_track));
        return allItems;
    }

itemObject:

private String name;
    private int photo;

    public ItemObject(String name, int photo) {
        this.name = name;
        this.photo = photo;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPhoto() {
        return photo;
    }

Holder:

public class RecyclerViewHolders extends RecyclerView.ViewHolder {

    public TextView Name;
    public ImageView Photo;

    public RecyclerViewHolders(View itemView) {
        super(itemView);
        Name = (TextView)itemView.findViewById(R.id.name);
        Photo = (ImageView)itemView.findViewById(R.id.photo);
    }
}

adapter:

public class RecyclerViewAdapter  extends RecyclerView.Adapter<RecyclerViewHolders> {

    private List<ItemObject> itemList;
    private Context context;



    public RecyclerViewAdapter(Context context, List<ItemObject> itemList) {
        this.itemList = itemList;
        this.context = context;
    }

    @Override
    public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {

        View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_list, null);
        RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView);
        return rcv;
    }

    @Override
    public void onBindViewHolder(RecyclerViewHolders holder, int position) {
        holder.Name.setText(itemList.get(position).getName());
Picasso.with(context).load(itemList.get(position).getPhoto()).into(holder.Photo);

    }

    @Override
    public int getItemCount() {
        return this.itemList.size();
    }
}
Rashad.Z
  • 2,494
  • 2
  • 27
  • 58

2 Answers2

2

Call this method setHasStableIds(true);

witted_coder
  • 161
  • 1
  • 14
0

its all about the image height and width.... i changed the image height to 700 and width to 400 and it scrolled smoothly

Rashad.Z
  • 2,494
  • 2
  • 27
  • 58