Issue
I'm loading images into recyclerview item(imageview) from firebase. All images are of different dimensions.
I want my imageview to scale its height according the image i'm loading.
Main layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.RecyclerView
android:id="@+id/RecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
>
<ImageView
android:id="@+id/ImageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
In java i'm doing.
RecyclerView.setHasFixedSize(true);
RecyclerView.setLayoutManager(new LinearLayoutManager(mctx));
Also when I scroll recyclerview it changes the image height
then I scrolled up and down, and see the height. Scrolled up/down output
third time, again scrolled and worst output
Please help me.. Thanks a lot..
Solution
Set adjustViewBounds to true in Image view that you are using in row.xml like this:
<ImageView
android:id="@+id/ImageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/img"/>
By adding adjustViewBounds and leaving the layout_height as wrap_content we are keeping the aspect ratio of the image while keeping the width to match parent but letting the height change accordingly, so we are still able to control the size of the image (kind of) and maintaining the aspect ratio at the same time.
You can also set staggered grid layout manager in recyler view to have different item size.
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(1, 1)); //First param to set span count and second for orientation
Answered By - Surabhi Choudhary
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.