Solution 1 :
Well I have achieved this in one of my projects, so I am here sharing you some code snippet over here. Try it and let me know.
class DemoActivity : ActivityBase() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_demo_activity)
val layoutmanager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
layoutmanager.gapStrategy = StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS
rcv_staggered.layoutManager = layoutmanager
rcv_staggered.setHasFixedSize(true)
val listItem = ArrayList<DemoModel>()
// This is dummy url for reference and in this image url I was
// getting image with different width and height
val demoModel = DemoModel("https://i.picsum.photos/id/237/200/400.jpg", "Title 1")
val demoModel1 = DemoModel("https://i.picsum.photos/id/237/750/250.jpg", "Title 2")
val demoModel2 = DemoModel("https://i.picsum.photos/id/237/500/250.jpg", "Title 3")
val demoModel3 = DemoModel("https://i.picsum.photos/id/237/100/200.jpg", "Title 4")
listItem.add(demoModel)
listItem.add(demoModel1)
listItem.add(demoModel2)
listItem.add(demoModel3)
rcv_staggered.adapter = DemoAdapter(listItem)
}
}
Create recyclerView adapter same as we all do.
Load images using below snippet:
Glide.with(holder.img.context)
.load(listItem[holder.adapterPosition].color)
.placeholder(R.color.black_alpha_10)
.into(holder.img)
And this is my item layout for recyclerview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns_android="http://schemas.android.com/apk/res/android"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_layout_margin="10dp"
android_orientation="vertical">
<androidx.appcompat.widget.AppCompatImageView
android_id="@+id/image"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_adjustViewBounds="true"/>
</LinearLayout>
StaggeredGridLayoutManager Screenshot
Solution 2 :
Wrap your ImageView
inside RelativeLayout
, it should be Ok
<RelativeLayout xmlns_android="http://schemas.android.com/apk/res/android"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_padding="5dp">
<ImageView
android_id="@+id/UserProfile_photoThumb"
android_layout_width="match_parent"
android_layout_height="wrap_content" />
</RelativeLayout>
Solution 3 :
I think their issues in glide code which you have used
Use this
Glide.with(mContext).load(imageList.get(i)).into(viewHolder.image);
Instead of this
Glide.with(getActivity()).load(ImageList.get(i)).apply(new RequestOptions().centerCrop()).into(viewHolder.image);
Also I have made some other changes in your code please check below example
Find the full code here
MainActivity2 code
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import android.os.Bundle;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.flexbox.JustifyContent;
import java.util.ArrayList;
public class MainActivity2 extends AppCompatActivity {
RecyclerView photosRecycler;
ArrayList<String> imageList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
photosRecycler = findViewById(R.id.userPhotos_recycler);
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);
photosRecycler.setLayoutManager(layoutManager);
photosRecycler.setHasFixedSize(true);
imageList.add("https://i.stack.imgur.com/K8FFo.jpg?s=328&g=1");
imageList.add("https://i.stack.imgur.com/Bpdap.jpg?s=328&g=1");
imageList.add("https://i.stack.imgur.com/73QY4.jpg");
imageList.add("https://i.stack.imgur.com/Bpdap.jpg?s=328&g=1");
imageList.add("https://i.stack.imgur.com/K8FFo.jpg?s=328&g=1");
imageList.add("https://i.stack.imgur.com/Bpdap.jpg?s=328&g=1");
imageList.add("https://i.stack.imgur.com/K8FFo.jpg?s=328&g=1");
fetchPhoto_Adapter adapter = new fetchPhoto_Adapter(imageList, this);
photosRecycler.setAdapter(adapter);
}
}
activity_main2 of layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns_android="http://schemas.android.com/apk/res/android"
xmlns_app="http://schemas.android.com/apk/res-auto"
android_layout_width="match_parent"
android_orientation="vertical"
android_layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android_id="@+id/userPhotos_recycler"
android_layout_width="match_parent"
android_layout_height="match_parent" />
</LinearLayout>
fetchPhoto_Adapter
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import java.util.ArrayList;
public class fetchPhoto_Adapter extends RecyclerView.Adapter<fetchPhoto_Adapter.ViewHolder> {
ArrayList<String> imageList = new ArrayList<>();
Context mContext;
public fetchPhoto_Adapter(ArrayList<String> imageList, Context mContext) {
this.imageList = imageList;
this.mContext = mContext;
}
@NonNull
@Override
public fetchPhoto_Adapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View v = inflater.inflate(R.layout.photogallery, viewGroup, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(fetchPhoto_Adapter.ViewHolder viewHolder, int i) {
Glide.with(mContext).load(imageList.get(i)).into(viewHolder.image);
}
@Override
public int getItemCount() {
return imageList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView image;
public ViewHolder(View itemView) {
super(itemView);
image = itemView.findViewById(R.id.UserProfile_photoThumb);
}
}
}
photogallery layout
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns_android="http://schemas.android.com/apk/res/android"
android_id="@+id/UserProfile_photoThumb"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_adjustViewBounds="true"
android_layout_margin="2dp"
android_scaleType="fitXY" />
OUTPUT
Problem :
I am trying to create StaggeredGrid
with this code.
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.userprofile_photos,container,false);
setRetainInstance(true);
photosRecycler=view.findViewById(R.id.userPhotos_recycler);
layoutManager= new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);
photosRecycler.setLayoutManager(layoutManager);
photosRecycler.setHasFixedSize(true);
adapter=new fetchPhoto_Adapter();
photosRecycler.setAdapter(adapter);
return view;
}
Fragment Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns_android="http://schemas.android.com/apk/res/android"
xmlns_app="http://schemas.android.com/apk/res-auto"
android_layout_width="match_parent"
android_layout_height="match_parent"
app_layout_behavior="@string/appbar_scrolling_view_behavior">
<androidx.recyclerview.widget.RecyclerView
android_id="@+id/userPhotos_recycler"
android_layout_width="match_parent"
android_layout_height="match_parent"
app_layout_scrollFlags="scroll" />
</RelativeLayout>
Adapter
public class fetchPhoto_Adapter extends RecyclerView.Adapter<fetchPhoto_Adapter.ViewHolder>{
@NonNull
@Override
public fetchPhoto_Adapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
LayoutInflater inflater= (LayoutInflater) viewGroup.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.photogallery,viewGroup,false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(fetchPhoto_Adapter.ViewHolder viewHolder, int i) {
Glide.with(getActivity()).load(ImageList.get(i)).apply(new RequestOptions().centerCrop()).into(viewHolder.image);
}
@Override
public int getItemCount() {
return ImageList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView image;
public ViewHolder(View itemView) {
super(itemView);
image = itemView.findViewById(R.id.UserProfile_photoThumb);
}
}
}
Adapter Item Layout XML
<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns_android="http://schemas.android.com/apk/res/android"
android_id="@+id/UserProfile_photoThumb"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_adjustViewBounds="true"
/>
I tried to make it StaggeredGrid
but with above code and with lots of modification like
setting image scaletype to fitxy,centercrop,center and changing height to wrap_content and modifying Glide image loading code all showing same output. I wanted to make my StaggeredGrid
Like below required output. Please help me out.
Comments
Comment posted by Nitin Tej
could you show me where you have created an instance of the adapter ?
Comment posted by androidXP
@NitinTej
Comment posted by Nitin Tej
are you not going to pass any list to the adapter ?
Comment posted by Hai Hack
@Nitin Tej he got the static data from ImageList inside the adapter
Comment posted by androidXP
@HaiHack Exactly. I am setting static date to adapter
Comment posted by androidXP
Your code giving me same output as i posted above.I also don’t see any difference in my and your code. My code has some bug and it’s a minor one which i can’t catch
Comment posted by Bhavnik
Okay I see, are you getting images in your URL with different height and width?
Comment posted by Bhavnik
@androidXP: I have modified my code and added some more image urls for your reference, as I said I am getting images with different height and width in url.
Comment posted by androidXP
I am setting static images to adapter and images are bit large in dimension but each image has different dimension from static web URL
Comment posted by Bhavnik
Try removing this line apply(new RequestOptions().centerCrop()) in your adapter
Comment posted by Hai Hack
Did you make it? I reproduce what you did and just changes as above.
Comment posted by androidXP
Tried but no luck
Comment posted by AskNilesh
@androidXP can you share you whole code as GitHub repository so I can try to reproduce the issue
Comment posted by androidXP
Thanks for help but this is whole code if you want me to share
Comment posted by AskNilesh
@androidXP I’m asking to share as a separate project on Github so I can reproduce the issue