RecyclerView horizontal wrap_content max size - android

I have a RecyclerView (image below) and I have some content (a picture and some text) loading from an API. I'd like all the rows to be exactly the same height, but the text could vary wildly in length. So the yellow box should be bigger to match the green box which has more text. If that makes sense?
Currently, I'm using a LinearLayoutManager set to Horizontal, but I've also tested with a GridLayoutManager with 1 row which produces the same effect.
I'm also using setAutoMeasureEnabled to true.
Is there a simple way to do this with a RecyclerView?
If not, how would be best to calculate the height and send it down to each ViewHolder?
Thank you,
As requested:
my initialisation which is in the MainActivity onCreate:
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new GridLayoutManager(MainActivity.this, 1, GridLayoutManager.HORIZONTAL, false);
mLayoutManager.setAutoMeasureEnabled(true);
mRecyclerView.setLayoutManager(mLayoutManager);
setUpSomeData();
TopFilmsCard card1 = (TopFilmsCard)cards.get(0);
mAdapter = new FilmsAdapter(card1.getFilms());
mRecyclerView.setAdapter(mAdapter);
FilmsAdapter
private class FilmsAdapter
extends RecyclerView.Adapter<FilmsCardHolder> {
private List<Film> films;
public FilmsAdapter(List<Film> films) {
this.films = films;
}
#Override
public int getItemViewType(int position) {
return R.layout.holder_top_films;
}
#Override
public FilmsCardHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new FilmsCardHolder(LayoutInflater.from(MainActivity.this).inflate(R.layout.holder_film, parent, false));
}
#Override
public void onBindViewHolder(FilmsCardHolder holder, int position) {
holder.bindCard(films.get(position));
}
#Override
public int getItemCount() {
return films.size();
}
}
FilmsCardHolder
public class FilmsCardHolder extends RecyclerView.ViewHolder {
public FilmsCardHolder(View itemView) {
super(itemView);
}
public void bindCard(Film film) {
ViewGroup layout = (ViewGroup)itemView.findViewById(R.id.layoutFilm);
TextView lblFilm = (TextView)itemView.findViewById(R.id.lblFilmName);
ImageView imgPicture = (ImageView)itemView.findViewById(R.id.imgPicture);
layout.setBackgroundResource(film.getColor());
lblFilm.setText(film.getName());
imgPicture.setImageResource(film.getPicture());
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#CCD1D9"/>
holder_film
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutFilm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#5D9CEC">
<TextView
android:id="#+id/lblFilmName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="10dp"
android:textSize="22sp"/>
<ImageView
android:id="#+id/imgPicture"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"
/>

Related

How to make large scrolling screens with images/buttons?

I have the following code:
<ScrollView
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:fillViewport="false"
tools:context=".bakers">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
...
in an activity. I have a few images on the activity and know I won't be able to fit them all on one screen, so I added a ScrollView with a height that is much larger than the screen. However, all this does is simply scale the existing images I have so that they are larger and take up more of the screen. I've tried fixing the fillViewport and clipToPadding settings, but this doesn't help anything.
Essentially what I'm asking is: Is there a way to add an image "below" the preview on screen using a ScrollView, so you can fit more on the screen than you normally would be able to? If I make the phone screen larger or the ScrollView larger, the images simply scale up.
Thanks
First you need use an recyclerview RecyclerView documentation
your activity view will have this code
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/buttonAction"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button action" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:scrollingCache="true" />
</LinearLayout>
you need create another layout in this case the layout that will display your image, example item_image.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/image"
android:layout_marginTop="4dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:src="#drawable/image_1"/>
</LinearLayout>
well now you have your views.
the next is create a new class thats implement an adapter: CardAdapter.java
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {
Context context;
public void setContext(Context context) {
this.context = context;
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView imgCover;
public ViewHolder(final View itemView, int type) {
super(itemView);
imgCover = itemView.findViewById(R.id.image);
}
}
#Override
public int getItemViewType(int position) {
return 1;
}
#NonNull
#Override
public CardAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = null;
RecyclerView.LayoutParams lp;
switch (viewType) {
case 1:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_card_payment, null, false);
lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(lp);
break;
}
return new CardAdapter.ViewHolder(view, viewType);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
}
#Override
public int getItemCount() {
return 10;
}
public Context getContext() {
return context;
}
}
and in your activity you will implement the recycler and the adapter
public class CardsActivity extends AppCompatActivity {
RecyclerView recyclerView;
CardAdapter cardAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cards);
initToolbar();
recyclerView = findViewById(R.id.recyclerView);
cardAdapter = new CardAdapter();
cardAdapter.setContext(this);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setAdapter(cardAdapter);
}
}
and its all.

RecyclerView does not scroll & Won't display large numbers

I am having a bit trouble with my RecyclerView. If my adapter list have more than 20 items, the recyclerview shows nothing. Anything <20 is ok, and the items are displayed. Also, I can't scroll on the recyclerView.
RecyclerView creation in Fragment:
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(context);
recyclerView.setLayoutManager(layoutManager);
myAdapter = new MyAdapter();
myAdapter.setList(myList); //ArrayList<MyObject>
recyclerView.setAdapter(myAdapter);
My Adapter:
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
private final ArrayList<MyObject> myList = new ArrayList<MyObject>();
public MyAdapter() {}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_item, parent, false);
return new MyAdapter(v);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
holder.bind(myList.get(position));
}
#Override
public int getItemCount() {
return myList.size();
}
public void setList(ArrayList<MyObject> newList) {
myList.clear();
myList.addAll(newList);
notifyDataSetChanged();
}
}
and here is my viewholder:
public class CountryViewHolder extends RecyclerView.ViewHolder {
private final TextView textView;
private MyObject object;
public CountryViewHolder(#NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.my_holder_textview);
}
public void bind(#Nullable MyObject object){
this.object = object;
if(pbject == null)
textView.setText("Error. Could not load name.");
else
textView.setText(object.getName());
}
}
myitem.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/my_holder_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:layout_marginBottom="4dp"
android:textSize="24sp"
android:gravity="center"/>
</LinearLayout>
Fragment layout where I have the recyclerView:
<androidx.constraintlayout.widget.ConstraintLayout 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">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/myRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Any suggestions?
EDIT:
Changing the layout from ConstraintLayout to LinearLayout, the recyclerview will run fine as it should. So the culprit here is the Constraintlayout. I would like to keep the constraintlayout, but I can't figure out how to make it work yet.
Instead of:
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/myRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
Try:
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/myRecyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
I can see that you have an error in onCreateViewHolder, it should return MyViewHolder and not a MyAdapter.
Also the setHasFixedSize(true) could be an issue here because your children might not change in size if this has to be true(find more information on SO).

Horizontal recyclerview into vertical recyclerview at specific position

I want to insert/show horizontal recyclerview into specific position (at this example position:1)
Vertical recyclerview - displays TextView + shape (rectange)
Horizontal recyclerview - displays ImageViews (next to each other)
.XML's
horizontal_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/horizontal_recyclerview"
android:layout_width="368dp"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="2dp"
android:padding="0dp"/>
<android.support.constraint.ConstraintLayout 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:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mkalejs.training.MainActivity">
vertical_layout.xml
<android.support.v7.widget.RecyclerView
android:id="#+id/vertical_recyclerview"
android:layout_width="368dp"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="2dp"
android:padding="0dp"/>
</android.support.constraint.ConstraintLayout>
vertical_layout_item.xml
<EditText
android:id="#+id/lbl_url_text"
android:layout_width="100dp"
android:layout_height="50dp"
android:background="#android:color/transparent"
android:inputType="textPersonName"
android:padding="5dp"
android:layout_gravity="center"
android:text="Name" />
<View
android:id="#+id/rectangle"
android:layout_width="195dp"
android:layout_height="100dp"
android:background="#drawable/rectangle" />
horizontal_layout_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:visibility="visible"
android:weightSum="1">
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/img_horizontal_slider"
android:layout_width="33dp"
android:layout_height="33dp"
android:adjustViewBounds="true"
android:visibility="visible" />
</LinearLayout>
There might be a small misunderstanding in recyclerview concept. I'm not sure where I can call horizontal_recyclerview" to not return it as a null (as I am now) and populate it with data. Is the XML structure wrong? Feel free to update code parts.
THIS SHOULD BE OKAY
MainActivity.class
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.vertical_layout);
ArrayList<PictureData> bundleOfData = prepareData();
RecyclerView rv = (RecyclerView) findViewById(R.id.vertical_recyclerview);
VerticalAdapter adapter = new VerticalAdapter(bundleOfData, this);
rv.setAdapter(adapter);
//rv.setNestedScrollingEnabled(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
rv.setLayoutManager(layoutManager);
}
VerticalAdapter.class
public class VerticalAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private ArrayList<PictureData> pictures;
Context context;
public VerticalAdapter(ArrayList<PictureData> pictures, Context context)
{
this.pictures = pictures;
this.context = context;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView;
switch (viewType) {
case 1:
itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.horizontal_layout_item, parent, false);
return new HorizontalViewHolder(itemView);
default:
itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.vertical_layout_item, parent, false);
return new VerticalViewHolder(itemView);
}
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
switch (holder.getItemViewType()){
case 1:
HorizontalViewHolder hVh = (HorizontalViewHolder) holder;
HorizontalAdapter horizontal_adapter = new HorizontalAdapter(pictures, context);
hVh.horizontal_rec.setAdapter(horizontal_adapter);
break;
default:
VerticalViewHolder vVh = (VerticalViewHolder) holder;
vVh.textView.setText(pictures.get(position).getPictureUrl());
break;
}
}
#Override
public int getItemCount() {return pictures.size();}
#Override
public int getItemViewType(int position) {
if(position == 1){
return 1;
}else{
return 0;
}
}
}
HorizontalViewHolder.class
I GET A ERROR HERE. OBVIOUSLY, I DON'T HAVE (img_horizontal_slider) IN HORIZONTAL_LAYOUT, but putting ImageView + RecyclerView in one layout file and call here would feel wrong (?)
public class HorizontalViewHolder extends RecyclerView.ViewHolder{
public ImageView imagePositionInLayout;
public RecyclerView horizontal_rec;
public HorizontalViewHolder(View itemView) {
super(itemView);
LinearLayoutManager layoutManager = new LinearLayoutManager(itemView.getContext(), LinearLayoutManager.HORIZONTAL, false);
horizontal_rec = itemView.findViewById(R.id.vertical_recyclerview);
imagePositionInLayout = itemView.findViewById(R.id.img_horizontal_slider);
horizontal_rec.setLayoutManager(layoutManager);
}
public ImageView getView() {
return imagePositionInLayout;
}
}
HorizontalAdapter.class
ublic class HorizontalAdapter extends RecyclerView.Adapter<HorizontalViewHolder> {
private ArrayList<PictureData> pictures;
Context context;
public HorizontalAdapter(ArrayList<PictureData> pictures, Context context) {
this.pictures = pictures;
this.context = context;
}
#Override
public HorizontalViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.horizontal_layout_item, parent, false);
return new HorizontalViewHolder(view);
}
#Override
public void onBindViewHolder(HorizontalViewHolder holder, int position)
{
Picasso.Builder builder = new Picasso.Builder(context);
Picasso picasso = builder.build();
picasso
.load("http://nuclearpixel.com/content/icons/2010-02-09_stellar_icons_from_space_from_2005/earth_128.png")
.placeholder(R.mipmap.ic_launcher_round)
.into(holder.imagePositionInLayout);
}
#Override
public int getItemCount() {
return pictures.size();
}
}
I'm new to Android so I hope people will give some good explanation on what is wrong and how to fix it. Every suggestion will be gladly accepted. I will highly appreciate if someone will actually look into this and explain, because I feel pretty lost right now.

big space between imagesview ( RecyclerView )

i'm trying to create an wallpaper application but i have this problem with recyclerView There is a big space between photos qnd spaced apart like this
photo
and i don't why Appears in this format Width larger than Height
java :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
List<String> movieList = new ArrayList<>();
movieList.add("https://lh3.ggpht.com/-yKfQjOKJq9HcsOqzdCfpP-POQgEtJb43RZAQjEn10XiOEgM_CBgndV7stWe67wXiCq5=h900");
movieList.add("http://www.botherland.com/wp-content/uploads/2017/04/Samsung-Galaxy-S4-Black-Edition-Wallpaper-16.jpg");
movieList.add("http://www.botherland.com/wp-content/uploads/2017/04/Samsung-Galaxy-S4-Black-Edition-Wallpaper-5.jpg");
movieList.add("http://www.androidhive.info/wp-content/uploads/2016/01/android-recycler-view-example.png");
movieList.add("https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcTygBxBtnlvqcQR5GmFCvD1rvPUJJeHLm6lH2zckeYQ6XScoutwN0NgaG0");
movieList.add("http://www.botherland.com/wp-content/uploads/2017/04/Samsung-Galaxy-S4-Black-Edition-Wallpaper-14.jpg");
View v = inflater.inflate(R.layout.fragment_art, container, false);
recyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);
mAdapter = new MoviesAdapter(movieList,getContext());
final StaggeredGridLayoutManager stg= new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(stg);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
return v;
}
public class MoviesAdapter extends RecyclerView.Adapter<MoviesAdapter.MyViewHolder> {
private List<String> moviesList;
class MyViewHolder extends RecyclerView.ViewHolder {
public ImageView pic ;
public MyViewHolder(View view) {
super(view);
pic = (ImageView)view.findViewById(R.id.imageView);
}
}
public Context context;
int screenWidth,screenHeight;
MoviesAdapter(List<String> moviesList, Context context) {
this.moviesList = moviesList;
this.context=context;
WindowManager wm = (WindowManager) getActivity().getSystemService(getActivity().WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
screenWidth = size.x;
screenHeight = size.y;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.picture_item, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
String pic = moviesList.get(position);
Glide.with(context).load(pic).placeholder(R.drawable.j).override(screenWidth/2,screenHeight/4).centerCrop().into(holder.pic);
}
#Override
public int getItemCount() {
return moviesList.size();
}
}
xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#fff"
tools:context="maa.a4kandhdwallpapers.Art">
<!-- TODO: Update blank fragment layout -->
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recycler_view"/>
</LinearLayout>
picture_item.xml :
<?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:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/imageView" />
</LinearLayout>
Change your picture_item.xml as:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/imageView" />
</LinearLayout>
But if your image height is large, you will have same problem. I suggest you to fix the height if that does not create problem.

android:staggergridlayout while moving up shuffles

I am using staggered grid layout. the following is the code:
StaggeredGridLayoutManager glm= new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
glm.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
I am using GAP_HANDLING_NONE to avoid images swapping from one column to other.
When I start the app, the beginning of the screen is:
After scrolling down to bottom and when I return to the top. randomly the following three images show the layout (it keeps varying)
Staggered grids are likely to have gaps at the edges of the layout. To avoid these gaps, StaggeredGridLayoutManager can offset spans independently or move items between spans. You can control this behavior via setGapStrategy(int):
StaggeredGridLayoutManager sGrid = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
sGrid.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);
I hope this will help a buddy.
Try below code.
Add this Override methode in your Adapter
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemViewType(int position) {
return position;
}
If this solution is not work for you!
Try below solution
I am using StaggeredGridLayout for getting all gallery images and it's working fine for me.
- Main Activity
/*Init Gallery Photos RecyclerView*/
gallerySglm = new StaggeredGridLayoutManager(2, RecyclerView.VERTICAL);
gallerySglm.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);
binding.rvGalleryImg.setLayoutManager(gallerySglm);
galleryPhotosAdapter = new GalleryPhotosAdapter(context, galleryPhotoArrayList);
binding.rvGalleryImg.setAdapter(galleryPhotosAdapter);
galleryPhotosAdapter.notifyDataSetChanged();
/*End Init Gallery Photos RecyclerView*/
- Item Layout
Add ImageView inside ConstraintLayout
<?xml version="1.0" encoding="utf-8"?>
<layout>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/mainCard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="#color/main_bg"
app:cardCornerRadius="#dimen/_5sdp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="false"
android:layout_margin="#dimen/_2sdp"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/clMain"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/img"
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="fitXY"
android:visibility="visible"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</layout>
- Adapter
public class GalleryPhotosAdapter extends RecyclerView.Adapter<GalleryPhotosAdapter.ViewHolder> {
private Context context;
private ArrayList<String> arrayList;
private ConstraintSet set = new ConstraintSet();
public GalleryPhotosAdapter(Context context, ArrayList<String> arrayList) {
this.context = context;
this.arrayList = arrayList;
}
#NonNull
#Override
public GalleryPhotosAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
ItemGalleryPhotosBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),
R.layout.item_gallery_photos, parent, false);
return new ViewHolder(binding);
}
#Override
public void onBindViewHolder(#NonNull final GalleryPhotosAdapter.ViewHolder holder, final int position) {
//Set size
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(arrayList.get(position), options);
String ratio = String.format(Locale.getDefault(), "%d:%d", options.outWidth, options.outHeight);
set.clone(holder.binding.clMain);
set.setDimensionRatio(holder.binding.img.getId(), ratio);
set.applyTo(holder.binding.clMain);
//End set size
Glide.with(context)
.load(arrayList.get(position))
.into(holder.binding.img);
}
#Override
public int getItemCount() {
return arrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ItemGalleryPhotosBinding binding;
public ViewHolder(ItemGalleryPhotosBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
}
}
I hope this can help You!
Thank You.
Use this simple solustion for your problem.
StaggeredGridLayoutManager straggerGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
straggerGridLayoutManager .setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);
recyclerView.setLayoutManager(straggerGridLayoutManager);
dataList = YourDataList (Your Code for Arraylist);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerAdapter = new DataAdapter(dataList, recyclerView);
recyclerView.setAdapter(recyclerAdapter);
recyclerView.addOnScrollListener(new ScrollListener());
Create class for Custom RecyclerView Scroll Listener.
private class ScrollListener extends RecyclerView.OnScrollListener {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
recyclerView.invalidateSpanAssignments();
}
}
Notice the sample code I provided for you
Images are shown in different proportions based on their dimensions and even some apps show a mixed view of images and videos like Instagram. In the coming tutorial, we will be experiencing such a view where videos are loaded and played with autoplay features when we strolled onto the video view.
When it comes to the implementation technically there is no much difference in regular style of showing images to this staggering view.
Activity_main.Xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context="staggered.android.com.staggeredview.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/stagRecycleView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.constraint.ConstraintLayout>
Recyclerview_row.Xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp">
<ImageView
android:id="#+id/imgView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"/>
</RelativeLayout>
MainActivity.Java
public class MainActivity extends AppCompatActivity {
RecyclerViewAdapter adapter;
ArrayList<Integer> Image;
RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Image = new ArrayList<>(Arrays.asList(
R.drawable.img1,R.drawable.img2,
R.drawable.img3,R.drawable.img4,
R.drawable.img5,R.drawable.img6,
R.drawable.img7,R.drawable.img8,
R.drawable.img9,R.drawable.img10,
R.drawable.img1,R.drawable.img2,
R.drawable.img3,R.drawable.img4,
R.drawable.img5,R.drawable.img6,
R.drawable.img7,R.drawable.img8,
R.drawable.img9)
);
recyclerView = findViewById(R.id.stagRecycleView);
layoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
adapter = new RecyclerViewAdapter(this, Image);
recyclerView.setAdapter(adapter);
}
}
And finally
RecyclerViewAdapter.Java
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
ArrayList<Integer> Image;
Context context;
public RecyclerViewAdapter(Context context, ArrayList<Integer> Image) {
super();
this.context = context;
this.Image = Image;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.recyclerview_row, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
viewHolder.imgview.setImageResource(Image.get(i));
}
#Override
public int getItemCount() {
return Image.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imgview;
public ViewHolder(View itemView) {
super(itemView);
imgview = (ImageView) itemView.findViewById(R.id.imgView);
}
}
}
I hope this will help you ;)

Categories

Resources