如何添加一个水平RecyclerView列表项内 [英] How do I add a horizontal RecyclerView inside a list item

查看:334
本文介绍了如何添加一个水平RecyclerView列表项内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

执行的任务,是我需要创建一个无限滚动列表将滚动横向和纵向。

有关这就是我想要做的是,我已经集成水平 RecyclerView 内的垂直的ListView 项目。这是正确的方式去了解?我曾尝试过其他解决方案,但滚动有一些问题。

现在的问题是,当我尝试实施 RecyclerView 列表项里面的物品没有得到填充。

 公共类Horizo​​ntalAdapter扩展RecyclerView.Adapter< Horizo​​ntalAdapter.ViewHolder> {

名单< ImageMedia> imageMedia;
保护Constants.IMAGE_SIZE IMAGE_SIZE = Constants.IMAGE_SIZE.Small;
私人上下文的背景下;


    公共Horizo​​ntalAdapter(上下文的背景下){
        超();
        setHasStableIds(真正的);
        this.context =背景;
    }

    @覆盖
    公共ViewHolder onCreateViewHolder(ViewGroup中的ViewGroup,INT viewType){
        返回新ViewHolder(LayoutInflater.from(viewGroup.getContext())膨胀(R.layout.horizo​​ntal_item,ViewGroup中,FALSE));
    }

    保护ImageMedia的getItem(INT位置){
        返回imageMedia.get(位置);
    }

    @覆盖
    公共无效onBindViewHolder(Horizo​​ntalAdapter.ViewHolder viewHolder,int i)以{
        viewHolder.setMedia(的getItem(一));
    }

    @覆盖
    公众诠释getItemCount(){
        返程(imageMedia = NULL imageMedia.size():?0);
    }

    @覆盖
    公共无效onViewRecycled(ViewHolder持有者){
        super.onViewRecycled(保持器);
    }

    @覆盖
    众长getItemId(INT位置){
        返回imageMedia.get(位置).getId();
    }

    //数据送过来更新
    公共无效setMovies(名单<电影>电影){

            如果(电影!= NULL){
                imageMedia =(名单< ImageMedia>)(名单<>)电影;
            } 其他 {

            }
            notifyDataSetChanged();
    }


    公共类ViewHolder扩展RecyclerView.ViewHolder实现View.OnClickListener {
        ImageCar​​dView ImageView的;
        ImageMedia imageMedia;

        公共ViewHolder(查看ItemView控件){
            超(ItemView控件);

            ImageView的=(ImageCar​​dView)itemView.findViewById(R.id.imageView);
            // TODO:删除此的onclick

            itemView.setOnClickListener(新View.OnClickListener(){
                @覆盖
                公共无效的onClick(视图v){
                    //意图去相应的活动
                }
            });
        }

        公共无效setMedia(ImageMedia imageMedia){
            this.imageMedia = imageMedia;

            imageView.loadImage(imageMedia,IMAGE_SIZE);

        }
    }
}
 

解决方案

据我的理解,当系统措施清单项目出现问题。

速战速决是要给水平 RecyclerView A硬codeD的高度,但因为它不是一个好办法,我继续狩猎,发现了另一个方法。

这个答案是有帮助的,我使用的库贴在那里。

按照以下步骤操作:

  1. 通过添加到您摇篮文件中导入库:

     编译org.solovyev.android.views:线性布局管理器:0.5@aar
     

  2. 设置布局管理象下面这样:

      mRecyclerView.setLayoutManager(新org.solovyev.android.views.llm.LinearLayoutManager(背景下,LinearLayoutManager.HORIZONTAL,FALSE));`
     

  3. 确保该观点被夸大水平 RecyclerView RelativeLayout的,因为其他的LinearLayout 不支持这一点。
  4. 运行项目,见证神奇!

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:

  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!

这篇关于如何添加一个水平RecyclerView列表项内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆