0

I've tried to implement a RecyclerViewAdapter which contains a list of text and image stored in a CardView, however when I try it, it isn't running smoothly neither on the emulator nor on a real device.

The adapter class :

public class MainRecyclerAdapter extends RecyclerView.Adapter<MainRecyclerAdapter.MainRecyclerViewHolder>{
Context context;
ArrayList<MainItem> itemsList;
LayoutInflater inflater;


public MainRecyclerAdapter(Context context) {
    this.context = context;
}

public MainRecyclerAdapter(Context context, ArrayList<MainItem> itemsList) {
    this.context = context;
    this.itemsList = itemsList;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public void add(MainItem item){
    itemsList.add(item);
    notifyDataSetChanged();
}

public void addAll(Collection<MainItem> itemCollection){
    for (MainItem item : itemCollection) itemsList.add(item);
    notifyDataSetChanged();
}

public void removeItem(int position){
    itemsList.remove(position);
    notifyDataSetChanged();
}

public void clear(){
    itemsList.clear();
    notifyDataSetChanged();
}

@Override
public MainRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    return new MainRecyclerViewHolder(inflater.inflate(R.layout.main_menu_item, parent, false));
}

@Override
public void onBindViewHolder(MainRecyclerViewHolder holder, int position) {

    YoYo.with(Techniques.ZoomIn).playOn(holder.cardView);
    MainItem item = itemsList.get(position);

    holder.textView.setText(item.getText());
    Picasso.with(context)
            .load(item.getImageResourceID())
            .into(holder.imageView);

    if (item.getColor() != null)
        holder.cardView.setCardBackgroundColor(item.getColor());
}

@Override
public int getItemCount() {
    return itemsList.size();
}

public class MainRecyclerViewHolder extends RecyclerView.ViewHolder{

    CardView cardView;
    TextView textView;
    ImageView imageView;

    public MainRecyclerViewHolder(View itemView) {
        super(itemView);
        cardView = itemView.findViewById(R.id.mainItemCardView);
        textView = itemView.findViewById(R.id.mainCardTextView);
        imageView = itemView.findViewById(R.id.mainCardImageView);
    }
}
}

My main activity layout :

<LinearLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/main_wallpaper"
tools:context="com.r3tr0.weekplanner.MainActivity">

    <android.support.v7.widget.CardView
        android:id="@+id/headMainCardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        app:cardCornerRadius="20dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="12dp"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="16dp"
                android:layout_marginTop="16dp"
                android:gravity="center"
                android:text="Welcome to the week planner app"
                android:textAlignment="center"
                android:textSize="24sp"
                android:textStyle="bold|italic" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="30dp"
                android:layout_marginRight="30dp"
                android:text="Let's start by making your life a little more organized!"
                android:textSize="18sp" />
        </LinearLayout>

    </android.support.v7.widget.CardView>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/mainRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="16dp"
        />

My OnCreate method :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Palette palette = Palette.from(BitmapFactory.decodeResource(getResources(), R.drawable.main_wallpaper)).generate();
    ((CardView) findViewById(R.id.headMainCardView)).setCardBackgroundColor(palette.getDominantColor(Color.parseColor("#FFFFFF")));
    initializeRecyclerView();
}

void initializeRecyclerView(){
    RecyclerView recyclerView = findViewById(R.id.mainRecyclerView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setItemViewCacheSize(20);
    recyclerView.setDrawingCacheEnabled(true);
    recyclerView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
    adapter = new MainRecyclerAdapter(this, new ArrayList<MainItem>());
    adapter.add(new MainItem("Coming soon", R.drawable.soon_transparent, Color.parseColor("#91677D")));
    adapter.add(new MainItem("Week's plan", R.drawable.schedule_transparent, Color.parseColor("#95A6C4")));
    adapter.add(new MainItem("My vault", R.drawable.vault_transparent, Color.parseColor("#CCD9E9")));
    adapter.add(new MainItem("My achievements", R.drawable.soon_transparent, Color.parseColor("#E65D6D")));
    adapter.add(new MainItem("Coming soon!", R.drawable.soon_transparent, Color.parseColor("#FC9D94")));
    adapter.add(new MainItem("Exit", R.drawable.x_transparent, Color.parseColor("#BDCCD3")));

    recyclerView.setLayoutManager(new GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false));
    recyclerView.setAdapter(adapter);
}

P.S : I 've seen solutions like this one and this but none have helped much.

Thanks in advance

Tarek Mohamed
  • 81
  • 1
  • 8
  • Why are you using `setDrawingCacheQuality()` and `setItemViewCacheSize()` any specific reason? and also post your whole adapters code . – ADM Dec 24 '17 at 14:22
  • Scrolling painfully means that there lots of views that prevent smooth scrolling... I've also experienced such type of problem, I just used Glide to set Images and my problem solved, please try to decrease the number of items or the quality of images which are on the screen... – Tarlan Ahad Dec 24 '17 at 14:22
  • @ADM I was just following one of the answers, and I 've edited the post with the whole adapter class code. – Tarek Mohamed Dec 24 '17 at 14:24
  • @TarlanAhad when I run it the view holds 4-6 items with 546X546 png images. – Tarek Mohamed Dec 24 '17 at 14:25
  • It is just anticipation: Please try to remove CardView's and use other stuff... – Tarlan Ahad Dec 24 '17 at 14:29
  • 1
    Add your layout code – Abubakker Moallim Dec 24 '17 at 17:01

3 Answers3

0

Initialize your adapter like below.

void initializeRecyclerView(){
    RecyclerView recyclerView = findViewById(R.id.mainRecyclerView);
    recyclerView.setLayoutManager(new GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false));
    recyclerView.setHasFixedSize(true);
    ArrayList list=new ArrayList<MainItem>();
    list.add(new MainItem("Coming soon", R.drawable.soon_transparent, Color.parseColor("#91677D")));
    list.add(new MainItem("Week's plan", R.drawable.schedule_transparent, Color.parseColor("#95A6C4")));
    list.add(new MainItem("My vault", R.drawable.vault_transparent, Color.parseColor("#CCD9E9")));
    list.add(new MainItem("My achievements", R.drawable.soon_transparent, Color.parseColor("#E65D6D")));
    list.add(new MainItem("Coming soon!", R.drawable.soon_transparent, Color.parseColor("#FC9D94")));
    list.add(new MainItem("Exit", R.drawable.x_transparent, Color.parseColor("#BDCCD3")));
    adapter = new MainRecyclerAdapter(this, list);
    recyclerView.setAdapter(adapter);
}

As per Your adapter's code . Your add and remove method should be as below .

public void add(MainItem item){
    itemsList.add(item);
    notifyItemInserted(itemsList.size()-1);
  }

public void removeItem(int position){
itemsList.remove(position);
notifyItemRemoved(position);
}

Read RecyclerView.Adapter to more of its view holder pattern.

ADM
  • 20,406
  • 11
  • 52
  • 83
  • If still not work hen post your whole adapter code. Need to have a look on adapter . – ADM Dec 24 '17 at 14:31
  • Thanks for reply, but it is still slow, I 've edited my question with the adapter class. – Tarek Mohamed Dec 24 '17 at 14:36
  • What does this code does ? YoYo.with(Techniques.ZoomIn).playOn(holder.cardView); try after commenting it . – ADM Dec 24 '17 at 14:38
  • just plays an animation on the view, tried removing it but didn't help much – Tarek Mohamed Dec 24 '17 at 14:40
  • Well if you used above answer and all code is in question . then i think it should work . You sure you are not modifying adapter anywhere else other than `initializeRecyclerView()` method. – ADM Dec 24 '17 at 14:45
  • Yes, the only place where the adapter is modified is in the OnCreate and InitializeRecyclerView methods – Tarek Mohamed Dec 24 '17 at 14:50
0

Try adding this line to your code

 recyclerView.setNestedScrollingEnabled(false);

Because may be your recyclerView will be instead NestedScrollView

<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycle2"
    android:nestedScrollingEnabled="false"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
Abubakker Moallim
  • 1,610
  • 10
  • 20
  • 1
    Thanks for commenting, my main activity doesn't contain nested scroll view, also I 've posted the main activity layout in the question description above – Tarek Mohamed Dec 26 '17 at 08:40
0

Now this is interesting ... I have tried Picasso library instead of Glide and reduced the images' resolution to 256X256 and now the RecyclerView is running smoothly, I will update the adapter class with the new code in case anyone needed it.

Tarek Mohamed
  • 81
  • 1
  • 8