Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a horizontal RecyclerView inside a list item

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);

        }
    }
}
like image 262
user1273299 Avatar asked May 07 '15 06:05

user1273299


1 Answers

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:

  1. Import the library by adding this to your gradle file:

    compile 'org.solovyev.android.views:linear-layout-manager:0.5@aar'
    
  2. set the LayoutManager like below:

    mRecyclerView.setLayoutManager(new org.solovyev.android.views.llm.LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));`
    
  3. make sure that the view being inflated in the horizontal RecyclerView is RelativeLayout because other LinearLayout doesn't support this.
  4. run your project and witness the magic!
like image 155
Sufian Avatar answered Nov 14 '22 12:11

Sufian