1

i have a simple recycler view which contains card views with only 1 imageView inside, which is loading from resources, but i have strong lags while scrolling anyway, i tried recycler.setHasFixedSize(true) but it doesn't help.

Acitivity code

public class MainActivity extends AppCompatActivity {

    private final int[] drawables = {
            R.drawable.nxt_bluetooth_dongle,
            R.drawable.color_sensor,
            R.drawable.connector_cabels,
            R.drawable.e_motor,
            R.drawable.energy_storage,
            R.drawable.energy_display,
            R.drawable.gyroscopic_sensor,
            R.drawable.intelligent_nxt_brick,
            R.drawable.keyfob_transponder,
            R.drawable.light_sensor,
            R.drawable.motion_sensor,
            R.drawable.nxt_ir_receiver,
            R.drawable.nxt_light_sensor,
            R.drawable.nxt_servo_motor,
            R.drawable.nxt_sound_sensor,
            R.drawable.nxt_touch_sensor,
            R.drawable.nxt_ultrasonic_sensor,
            R.drawable.rf_id_sensor,
            R.drawable.solar_panel,
            R.drawable.tilt_sensor,
            R.drawable.temperature_sensor,
            R.drawable.usb_hub
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RecyclerView recycler = (RecyclerView) findViewById(R.id.recycler);
        MyAdapter adapter = new MyAdapter(drawables);
        recycler.setAdapter(adapter);
        recycler.setLayoutManager(new GridLayoutManager(this, 3));
    }

    private class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> {

        private int[] drawables;

        private MyAdapter(int[] drawables) {
            this.drawables = drawables;
        }

        @Override
        public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.lego_recycler_item, null, false);
            return new MyHolder(view);
        }

        @Override
        public void onBindViewHolder(MyHolder holder, int position) {
            holder.legoImage.setImageResource(drawables[position]);
        }

        @Override
        public int getItemCount() {
            return drawables.length;
        }

        class MyHolder extends RecyclerView.ViewHolder {

            ImageView legoImage;

            MyHolder(View itemView) {
                super(itemView);
                legoImage = (ImageView) itemView.findViewById(R.id.legoImage);
                legoImage.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(MainActivity.this, LegoDetailActivity.class);
                        intent.putExtra(LegoDetailActivity.EXTRA_DRAWABLE_ID, drawables[getAdapterPosition()]);
                        ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(
                                MainActivity.this, legoImage, "legoDetail"
                        );
                        startActivity(intent, options.toBundle());
                    }
                });
            }
        }
    }
}

Activity xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/recycler"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#e0e0e0"
    tools:context="com.lol.legomindstorms.MainActivity">

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

Recycler item xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:cardUseCompatPadding="true"
    app:cardCornerRadius="0dp">

    <ImageView
        android:id="@+id/legoImage"
        android:layout_width="115dp"
        android:layout_height="115dp"
        android:transitionName="legoDetail" />

</android.support.v7.widget.CardView>
Vanya Makhlinets
  • 248
  • 1
  • 4
  • 13
  • 1
    try Picasso or Glide to set image in item. – Divyesh Patel May 20 '17 at 11:54
  • As @DivyeshPatel mentioned it might be the loading of the images.. The two libraries mentioned take some of the work off the UI Thread allowing a smoother experience.. One side-effect is that images will seem to pop into view. I would suggest you check the size/resolution of those images as well. It might be 4kb on disk but it's actually LxWx4 or LxW*(AARRGGBB). – tricknology May 20 '17 at 12:08
  • See my answer [here](http://stackoverflow.com/a/43219866/7292819). It may help with your problem. – Gary99 May 20 '17 at 12:44

1 Answers1

1

Well as Per my Experience i think the problem is in loading multiple Images
As android do have problem with loading lots of images in memory , as android by default does not allow lot of gc memeory size , Check out your logs do you get a gc memory over load error or something similar to that

1.you can Also try chaning the heap size

2.Try using an external library it is always said never reinvent the wheel try glide or piccaso

3.Try clearing bitmap memory by your self

PS: i am not that writter so please dont mind if my wording were bad but i have dealt with these issues a lot so i am very familar with this problem

user49557
  • 132
  • 1
  • 3
  • 11
  • Changing heap size is only a temporary solution to a deeper issue and that's your memory management. If you want to clear the bitmap yourself then you're looking to override `onViewRecycled(View view)`. I would suggest using Glide or Picasso in combination with `onViewRecycled(View view)` – tricknology May 20 '17 at 12:18
  • Yeah I agree to that using Picasso and glide or any other similar library is always great if lot of images are involved – user49557 May 20 '17 at 12:22