当前位置:  开发笔记 > 编程语言 > 正文

垂直RecyclerView内部的水平RecyclerView

如何解决《垂直RecyclerView内部的水平RecyclerView》经验,为你挑选了1个好方法。

我正在尝试做这样的事情。

因此,我的想法是我拥有带有通道的垂直recyclerview,而在通道的第二个位置上,我应该拥有带有重播的水平recyclerview。

我不确定该怎么做,我尝试弄乱了Viewholder,并猜想我应该只在自己的channel_details布局中制作一个recyclerview,并在item_channel_details中将另一个作成一个item,但是我无法使其工作。

这是我的代码。

ChannelDetailsActivity:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_channel_details);

    ImageView coverPhoto = (ImageView) findViewById(R.id.image_cover_details);
    final HexagonImageView avatarPhoto = (HexagonImageView) findViewById(R.id.img_hex);
    TextView toolbarText = (TextView) findViewById(R.id.txt_toolbar_title);

    final Bundle b = getIntent().getExtras();
    final MNetworkChannel parcelChannel =
            b.getParcelable(Const.IntentData.H_CHANNEL_LIST);

    final MVideosForChannel parcelVideosForChannel = b.getParcelable(Const.IntentData.D_VIDEOS_LIST);




    setChannelsView();
    setVideosView();


}

private void setChannelsView() {

    rvRelive = (RecyclerView) findViewById(R.id.rv_relive_details);
    rvRelive.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
    adapterRelives = new ReliveAdapter();
    rvRelive.setAdapter(adapterRelives);

    if (getIntent() != null && getIntent().getParcelableExtra(Const.IntentData.D_RELIVE_LIST) != null) {
        adapterRelives.setData(((ReliveMainPojo) getIntent().getParcelableExtra(Const.IntentData.D_RELIVE_LIST)).relives);
    }
}

private void setVideosView() {

    rvVideos = (RecyclerView) findViewById(R.id.rv_videos);
    rvVideos.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
    adapterVideos = new ChannelVideosAdapter();
    rvVideos.setAdapter(adapterVideos);

    if (getIntent() != null && getIntent().getParcelableExtra(Const.IntentData.D_VIDEOS_LIST) != null) {
        adapterVideos.setData(((MVideosForChannel) getIntent().getParcelableExtra(Const.IntentData.D_VIDEOS_LIST)).experience);
    }
}

ChannelDetails适配器:

public final class ChannelVideosAdapter extends RecyclerView.Adapter {

private List data = new ArrayList<>();

public ChannelVideosAdapter() {
}

public void setData(List newData) {

    if (newData != null && !newData.isEmpty()) {

        data = newData;
        notifyDataSetChanged();
    }
}

public void clearData() {

    data.clear();
    notifyDataSetChanged();
}

@Override
public final ChannelVideosAdapter.ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
    return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_video_recycle_tile, parent, false));
}

@Override
public final void onBindViewHolder(final ChannelVideosAdapter.ViewHolder holder, final int position) {

    final MVideo video = data.get(position);

    final String videoBackgroundImageUrl = video.asset.frame;
    final String videoName = video.name;

    ImageLoader.getInstance().displayImage(videoBackgroundImageUrl, holder.coverPhoto, new ImageLoadingListener() {
        @Override
        public void onLoadingStarted(String imageUri, View view) {
            holder.videoLoading.setVisibility(View.VISIBLE);
        }

        @Override
        public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
            holder.videoLoading.setVisibility(View.GONE);
        }

        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
            holder.videoLoading.setVisibility(View.GONE);
        }

        @Override
        public void onLoadingCancelled(String imageUri, View view) {
            holder.videoLoading.setVisibility(View.GONE);
        }
    });

    holder.videoName.setText(videoName);
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            VideoPlayerActivity.StartNewVideoPlayerActivity((ChannelDetailsActivity) holder.itemView.getContext(), video, true);
        }
    });
}

@Override
public final int getItemCount() {
    return data.size();
}

final class ViewHolder extends RecyclerView.ViewHolder {

    private final ImageView coverPhoto;
    private final TextView videoName;
    private final ProgressBar videoLoading;

    ViewHolder(final View itemView) {
        super(itemView);

        coverPhoto = (ImageView) itemView.findViewById(R.id.img_thumbnail_background_video);
        videoName = (TextView) itemView.findViewById(R.id.txt_video_name);
        videoLoading = (ProgressBar) itemView.findViewById(R.id.pb_video_loading);
    }
}
}

重新启动适配器:

public final class ReliveAdapter extends RecyclerView.Adapter {

private List data = new ArrayList<>();

public ReliveAdapter() {
}

public void setData(List newData) {

    if (newData != null && !newData.isEmpty()) {

        data = newData;
        notifyDataSetChanged();
    }
}

public void clearData() {

    data.clear();
    notifyDataSetChanged();
}

@Override
public final ReliveAdapter.ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
    return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_relive_recycle_tile, parent, false));
}

@Override
public void onBindViewHolder(final ReliveAdapter.ViewHolder holder, final int position) {

    final Relive relive = data.get(position);

    final String reliveOwnerIconUrl = relive.owner.asset.large;
    final String reliveCoverPhotoUrl = relive.asset.stream.thumbnail;
    final String reliveDescription = relive.owner.name;

    ImageLoader.getInstance().displayImage(reliveCoverPhotoUrl, holder.backgroundImage, new ImageLoadingListener() {
        @Override
        public void onLoadingStarted(String imageUri, View view) {
            holder.imageLoading.setVisibility(View.VISIBLE);
        }

        @Override
        public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
            holder.imageLoading.setVisibility(View.GONE);
        }

        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
            holder.imageLoading.setVisibility(View.GONE);

            ImageLoader.getInstance().displayImage(reliveOwnerIconUrl, holder.profilePicture, new ImageLoadingListener() {
                @Override
                public void onLoadingStarted(String imageUri, View view) {
                    holder.imageLoading.setVisibility(View.VISIBLE);
                }

                @Override
                public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                    holder.imageLoading.setVisibility(View.GONE);
                }

                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                    holder.imageLoading.setVisibility(View.GONE);
                }

                @Override
                public void onLoadingCancelled(String imageUri, View view) {
                    holder.imageLoading.setVisibility(View.GONE);
                }
            });

            holder.eyeIcon.setImageResource(R.drawable.relive);
        }

        @Override
        public void onLoadingCancelled(String imageUri, View view) {
            holder.imageLoading.setVisibility(View.GONE);
        }
    });

    holder.reliveDescription.setText(reliveDescription);

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            RelivePlayerActivity.StartReliveReviewActivity((ChannelDetailsActivity) holder.itemView.getContext(), relive.asset.stream.url, relive.experienceGuid, relive.guid, holder.getAdapterPosition());
        }
    });
}

@Override
public final int getItemCount() {
    return data.size();
}

final class ViewHolder extends RecyclerView.ViewHolder {

    private final CircleImageView profilePicture;
    private final ImageView eyeIcon;
    private final ImageView backgroundImage;
    private final TextView reliveDescription;
    private final ProgressBar imageLoading;

    ViewHolder(View itemView) {
        super(itemView);

        profilePicture = (CircleImageView) itemView.findViewById(R.id.profile_circle_image);
        eyeIcon = (ImageView) itemView.findViewById(R.id.icon_circle_image);
        backgroundImage = (ImageView) itemView.findViewById(R.id.thumbnail_image);
        reliveDescription = (TextView) itemView.findViewById(R.id.description_textview);
        imageLoading = (ProgressBar) itemView.findViewById(R.id.image_loading);
    }
}

}

这是我的布局:

activity_channel_details

 




    

    





    


        

            

                

                

                    

                

            

            

                
            

            

        
    


item_video_recycle_tile

 






    

    




    

        

        
    



Jay Patel.. 5

您应该使用一个recyclerView(垂直)作为父对象,并在适配器在位置1处绑定视图时,返回一个包含recyclerView(水平)的视图,并为该recyclerView加载其他适配器。请参考图以正确理解。

Main RecyclerView (vertical):   
    ---------------------------
    +   Item 1
    ---------------------------
    +   Second RecyclerView (Horizontal)
    ---------------------------
    +   Item 2
    ---------------------------

父级RecyclerView适配器代码:

    @Override
        public int getItemViewType(int position) {

            if (position == 1)
                return 0;
            else 
                return 1;
        }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == 0) {
            View v = LayoutInflater.from(context).inflate(R.layout.your_second_recylerView_layout, parent, false);
            return new ViewHolder1(v);
        }
        else{
            View v = LayoutInflater.from(context).inflate(R.layout.your_item_layout, parent, false);
            return new ViewHolder2(v);
        }

    }

现在,您需要为水平recycleView实现第二个适配器。



1> Jay Patel..:

您应该使用一个recyclerView(垂直)作为父对象,并在适配器在位置1处绑定视图时,返回一个包含recyclerView(水平)的视图,并为该recyclerView加载其他适配器。请参考图以正确理解。

Main RecyclerView (vertical):   
    ---------------------------
    +   Item 1
    ---------------------------
    +   Second RecyclerView (Horizontal)
    ---------------------------
    +   Item 2
    ---------------------------

父级RecyclerView适配器代码:

    @Override
        public int getItemViewType(int position) {

            if (position == 1)
                return 0;
            else 
                return 1;
        }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == 0) {
            View v = LayoutInflater.from(context).inflate(R.layout.your_second_recylerView_layout, parent, false);
            return new ViewHolder1(v);
        }
        else{
            View v = LayoutInflater.from(context).inflate(R.layout.your_item_layout, parent, false);
            return new ViewHolder2(v);
        }

    }

现在,您需要为水平recycleView实现第二个适配器。

推荐阅读
周扒pi
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有