如何添加一个水平RecyclerView列表项内 [英] How do I add a horizontal RecyclerView inside a list item
问题描述
执行的任务,是我需要创建一个无限滚动列表将滚动横向和纵向。
有关这就是我想要做的是,我已经集成水平 RecyclerView
内的垂直的ListView
项目。这是正确的方式去了解?我曾尝试过其他解决方案,但滚动有一些问题。
现在的问题是,当我尝试实施 RecyclerView
列表项里面的物品没有得到填充。
公共类HorizontalAdapter扩展RecyclerView.Adapter< HorizontalAdapter.ViewHolder> {
名单< ImageMedia> imageMedia;
保护Constants.IMAGE_SIZE IMAGE_SIZE = Constants.IMAGE_SIZE.Small;
私人上下文的背景下;
公共HorizontalAdapter(上下文的背景下){
超();
setHasStableIds(真正的);
this.context =背景;
}
@覆盖
公共ViewHolder onCreateViewHolder(ViewGroup中的ViewGroup,INT viewType){
返回新ViewHolder(LayoutInflater.from(viewGroup.getContext())膨胀(R.layout.horizontal_item,ViewGroup中,FALSE));
}
保护ImageMedia的getItem(INT位置){
返回imageMedia.get(位置);
}
@覆盖
公共无效onBindViewHolder(HorizontalAdapter.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 {
ImageCardView ImageView的;
ImageMedia imageMedia;
公共ViewHolder(查看ItemView控件){
超(ItemView控件);
ImageView的=(ImageCardView)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的高度,但因为它不是一个好办法,我继续狩猎,发现了另一个方法。
这个答案是有帮助的,我使用的库贴在那里。
按照以下步骤操作:
-
通过添加到您摇篮文件中导入库:
编译org.solovyev.android.views:线性布局管理器:0.5@aar
-
设置
布局管理
象下面这样:mRecyclerView.setLayoutManager(新org.solovyev.android.views.llm.LinearLayoutManager(背景下,LinearLayoutManager.HORIZONTAL,FALSE));`
- 确保该观点被夸大水平
RecyclerView
是RelativeLayout的
,因为其他的LinearLayout
不支持这一点。 - 运行项目,见证神奇!
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));`
- make sure that the view being inflated in the horizontal
RecyclerView
isRelativeLayout
because otherLinearLayout
doesn't support this. - run your project and witness the magic!
这篇关于如何添加一个水平RecyclerView列表项内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!