CopyPastor

Detecting plagiarism made easy.

Score: 0.7594611048698425; Reported for: String similarity Open both answers

Possible Plagiarism

Plagiarized on 2019-07-06
by ORBIT

Original Post

Original - Posted on 2017-05-01
by Sakiboy



            
Present in both answers; Present only in the new answer; Present only in the old answer;


> RecyclerView support library:
1. **Replace a complex layout** (nested views, RelativeLayout) with the new optimized ConstraintLayout. Activate it in Android Studio: Go to SDK Manager -> SDK Tools tab -> Support Repository -> check ConstraintLayout for Android & Solver for ConstraintLayout. Add to the dependencies:
compile 'com.android.support.constraint:constraint-layout:1.0.2'
2. If possible, make all elements of the RecyclerView with the **same height**. And add:
recyclerView.setHasFixedSize(true);
3. Use the default RecyclerView **drawing cache** methods and tweak them
recyclerView.setItemViewCacheSize(20); recyclerView.setDrawingCacheEnabled(true); recyclerView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
4. If you use many **images**, make sure their **size and compression are optimal**. Scaling images may also affect the performance. There are two sides of the problem - the source image used and the decoded Bitmap. The following example gives you a hint how to decode аn image, downloaded from the web:
InputStream is = (InputStream) url.getContent(); BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.RGB_565; Bitmap image = BitmapFactory.decodeStream(is, null, options);
The most important part is specifying `inPreferredConfig` - it defines how many bytes will be used for each pixel of the image. Keep in mind that this is a *preferred* option. If the source image has more colors, it will still be decoded with a different config.
5. Make sure **onBindViewHolder()** is as **cheap** as possible. You can set OnClickListener once in `onCreateViewHolder()` and call through an interface a listener outside of the Adapter, passing the clicked item. This way you don't create extra objects all the time. Also check flags and states, before making any changes to the view here.
viewHolder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Item item = getItem(getAdapterPosition()); outsideClickListener.onItemClicked(item); } });
6. When data gets changed, try to **update only the affected items**. For example instead of invalidating the whole data set with `notifyDataSetChanged()`, when adding / loading more items, just use:
adapter.notifyItemRangeInserted(rangeStart, rangeEnd); adapter.notifyItemRemoved(position); adapter.notifyItemChanged(position); adapter.notifyItemInserted(position);
7. From [Android Developer Web Site][1] :
> Rely on notifyDataSetChanged() as a last resort.
But if you need to use it, maintain your items with **unique ids**:
adapter.setHasStableIds(true);
> RecyclerView will attempt to synthesize visible structural change > events for adapters that report that they have stable IDs when this > method is used. This can help for the purposes of animation and visual > object persistence but individual item views will still need to be > rebound and relaid out.
Even if you do everything right, chances are that the RecyclerView is still not performing as smoothly as you would like.

[1]: https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html
For those using the [Glide][1] image loading library, who are still running into these `OutOfMemory Exception` issues, there're many things you can do to make `Glide` use less memory and hopefully fix your problem. Here are a few of them:
- Don't use `android:scaleType="fitXY"` inside of your `ImageView`. So if you're `ImageView` looks like this: <ImageView android:id="@android:id/icon" android:layout_width="@dimen/width" android:layout_height="@dimen/height" android:adjustViewBounds="true" android:scaleType="fitXY" <!-- DON'T USE "fitXY"! --> /> Change the `ImageView` to use a different `android:scaleType`, preferably: `fitCenter` or `centerCrop`. - Don't use `wrap_content` in your `ImageView`, instead use `match_parent` or specify the `width`/`height` explicitly using a size in `dp`. If you *really* insist on using `wrap_content` in your `ImageView`, at least set a `android:maxHeight`/`android:maxWidth`. - Turn off animations with: `dontAnimate()` on your `Glide.with()...` request. - If you're loading lots of potentially large images (as you would in a list/grid), specify a `thumbnail(float sizeMultiplier)` load in your request. Ex:
Glide.with(context) .load(imageUri) .thumbnail(0.5f) .dontAnimate() .into(iconImageView);
- Temporarily lower `Glide`'s memory footprint during certain phases of your app by using: `Glide.get(context).setMemoryCategory(MemoryCategory.LOW)`. - Only cache in memory if you need to, you can turn it off with: `skipMemoryCache(true)` on your `Glide.with()...` request. This will still cache the images to disk, which you'll probably want since you're foregoing the in-memory cache. - If you're loading a `Drawable` from your local resources, make sure that the image you're trying to load ISN'T SUPER HUGE. There are plenty of image compression tools available online. These tools will shrink the sizes of your images while also maintaining their appearance quality. - If loading from local resources use `.diskCacheStrategy(DiskCacheStrategy.NONE)`. - Hook into the `onTrimMemory(int level)` callback that Android provides to trim the `Glide` cache as needed. Ex. @Override public void onTrimMemory(int level) { super.onTrimMemory(level); Glide.get(this).trimMemory(level); } - If displaying images in a `RecyclerView` you can explicitly clear `Glide` when views are recycled, like so:
@Override public void onViewRecycled(MyAdapter.MyViewHolder holder) { super.onViewRecycled(holder); Glide.clear(holder.imageView); } - If this is *still* occurring, even after you've "tried everything", the problem might be **your application** (GASP!), and `Glide` is just the one thing that's pushing it to the `OutOfMemory Exception` zone... So be sure you don't have any memory leaks in your application. `Android Studio` provides tools for identifying memory consumption issues in you app. - Lastly check the [issue page on Glide's GitHub][2], for similar issues that may provide insight into fixing your problem(s). The repo is managed really well and they're very helpful.


[1]: https://github.com/bumptech/glide [2]: https://github.com/bumptech/glide/issues

        
Present in both answers; Present only in the new answer; Present only in the old answer;