The task of implementation is I need to create an infinite scrollable list which will scroll both horizontally and vertically.
For this what I'm trying to do is that I have integrated a horizontal RecyclerView
inside a vertical ListView
item. Is this the right way to go about? I have tried other solutions but the scrolling has some issues.
The problem is that when I try to implement the RecyclerView
inside a list item the items do not get populated.
public class HorizontalAdapter extends RecyclerView.Adapter<HorizontalAdapter.ViewHolder> {
List<ImageMedia> imageMedia;
protected Constants.IMAGE_SIZE image_size = Constants.IMAGE_SIZE.Small;
private Context context;
public HorizontalAdapter(Context context) {
super();
setHasStableIds(true);
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
return new ViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.horizontal_item, viewGroup, false));
}
protected ImageMedia getItem(int position) {
return imageMedia.get(position);
}
@Override
public void onBindViewHolder(HorizontalAdapter.ViewHolder viewHolder, int i) {
viewHolder.setMedia(getItem(i));
}
@Override
public int getItemCount() {
return (imageMedia != null ? imageMedia.size() : 0);
}
@Override
public void onViewRecycled(ViewHolder holder) {
super.onViewRecycled(holder);
}
@Override
public long getItemId(int position) {
return imageMedia.get(position).getId();
}
//Data gets updated here
public void setMovies(List<Movie> movie) {
if (movie != null) {
imageMedia = (List<ImageMedia>) (List<?>) movie;
} else {
}
notifyDataSetChanged();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageCardView imageView;
ImageMedia imageMedia;
public ViewHolder(View itemView) {
super(itemView);
imageView = (ImageCardView) itemView.findViewById(R.id.imageView);
//TODO: remove this onclick
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Intent to go to the corresponding activity
}
});
}
public void setMedia(ImageMedia imageMedia) {
this.imageMedia = imageMedia;
imageView.loadImage(imageMedia, image_size);
}
}
}
According to my understanding, the problem occurs when the system measures the list item.
The quick fix was to give horizontal RecyclerView
a hard-coded height, but since it isn't a good way, I continued the hunt and found another way.
This answer was helpful and I used the library posted there.
Follow these steps:
Import the library by adding this to your gradle file:
compile 'org.solovyev.android.views:linear-layout-manager:0.5@aar'
set the LayoutManager
like below:
mRecyclerView.setLayoutManager(new org.solovyev.android.views.llm.LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));`
RecyclerView
is RelativeLayout
because other LinearLayout
doesn't support this.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With