具有 GridLayoutManager 的 RecyclerView 和具有不同 viewHolder 的第一个元素
RecyclerView with GridLayoutManager and first element with different viewHolder
我需要在两行上创建一个带有 GridLayoutManager 的 recyclerView,并且第一个元素比其余元素大。结果应如下所示:
我设法做到了这一点,但是以一种非常规的方式。在我的 recyclerView 适配器中,我为第一个元素使用了不同的 viewHolder,一个更大的元素。那将是一个很好的解决方案,但第二个元素会低于第一个元素。所以我做了一个技巧,给 recyclerView 一个与第一个元素相同的固定高度,这样第一个元素和第二个元素就会重叠,我只会让第二个元素的可见性消失。
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case 0:
final View view = inflater.inflate(R.layout.big_item, parent, false);
return new BigViewHolder(view);
case 2:
final View view2 = inflater.inflate(R.layout.normal_item, parent, false);
return new NormalViewHolder(view2);
default:
return null;
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (position == 1) {
holder.itemView.setVisibility(View.GONE);
}}
@Override
public int getItemViewType(int position) {
if (position == 0) {
return 0;
} else return 2;
}
但我不是特别喜欢这种方法。有人对此有更好的想法吗?
经过一些挖掘和咨询,我得出了一个相当不错的结果。我会 post 下面:
首先,适配器应该像我在问题中提到的那样,没有隐藏第二个元素:
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public static final int TYPE_FIRST_ITEM = 0;
public static final int TYPE_ITEM = 1;
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case TYPE_FIRST_ITEM:
final View view = inflater.inflate(R.layout.big_item, parent, false);
return new BigViewHolder(view);
case TYPE_ITEM:
final View view2 = inflater.inflate(R.layout.normal_item, parent, false);
return new NormalViewHolder(view2);
default:
return null;
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
switch (holder.getItemViewType()) {
case TYPE_FIRST_ITEM:
BigViewHolder bigViewHolder = (BigViewHolder) holder;
// Do what you need for the first item
break;
case TYPE_ITEM:
NormalViewHolder normalViewHolder = (NormalViewHolder) holder;
// Do what you for the other items
break;
}
}
@Override
public int getItemViewType(int position) {
if (position == 0) {
return TYPE_FIRST_ITEM;
} else return TYPE_ITEM;
}
final class NormalViewHolder extends RecyclerView.ViewHolder {
public NormalViewHolder(View itemView) {
super(itemView);
}
// find your views here
}
final class BigViewHolder extends RecyclerView.ViewHolder {
public BigViewHolder(View itemView) {
super(itemView);
}
// find your views here
}
}
其次,布局管理器应该实现一个监听器来指定在特定位置应该有多少跨度(行):
RecyclerView mRecyclerView = view.findViewById(R.id.my_recycler_view);
MyAdapter mAdapter = new MyAdapter();
GridLayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 2, LinearLayoutManager.HORIZONTAL, false);
mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
switch(mAdapter.getItemViewType(position)){
case MyAdapter.TYPE_FIRST_ITEM:
return 2;
case MyAdapter.TYPE_ITEM:
return 1;
default:
return -1;
}
}
});
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setLayoutManager(mLayoutManager);
希望它也能帮助到其他人。感谢您Mike M.的支持。
我需要在两行上创建一个带有 GridLayoutManager 的 recyclerView,并且第一个元素比其余元素大。结果应如下所示:
我设法做到了这一点,但是以一种非常规的方式。在我的 recyclerView 适配器中,我为第一个元素使用了不同的 viewHolder,一个更大的元素。那将是一个很好的解决方案,但第二个元素会低于第一个元素。所以我做了一个技巧,给 recyclerView 一个与第一个元素相同的固定高度,这样第一个元素和第二个元素就会重叠,我只会让第二个元素的可见性消失。
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case 0:
final View view = inflater.inflate(R.layout.big_item, parent, false);
return new BigViewHolder(view);
case 2:
final View view2 = inflater.inflate(R.layout.normal_item, parent, false);
return new NormalViewHolder(view2);
default:
return null;
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (position == 1) {
holder.itemView.setVisibility(View.GONE);
}}
@Override
public int getItemViewType(int position) {
if (position == 0) {
return 0;
} else return 2;
}
但我不是特别喜欢这种方法。有人对此有更好的想法吗?
经过一些挖掘和咨询,我得出了一个相当不错的结果。我会 post 下面:
首先,适配器应该像我在问题中提到的那样,没有隐藏第二个元素:
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public static final int TYPE_FIRST_ITEM = 0;
public static final int TYPE_ITEM = 1;
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case TYPE_FIRST_ITEM:
final View view = inflater.inflate(R.layout.big_item, parent, false);
return new BigViewHolder(view);
case TYPE_ITEM:
final View view2 = inflater.inflate(R.layout.normal_item, parent, false);
return new NormalViewHolder(view2);
default:
return null;
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
switch (holder.getItemViewType()) {
case TYPE_FIRST_ITEM:
BigViewHolder bigViewHolder = (BigViewHolder) holder;
// Do what you need for the first item
break;
case TYPE_ITEM:
NormalViewHolder normalViewHolder = (NormalViewHolder) holder;
// Do what you for the other items
break;
}
}
@Override
public int getItemViewType(int position) {
if (position == 0) {
return TYPE_FIRST_ITEM;
} else return TYPE_ITEM;
}
final class NormalViewHolder extends RecyclerView.ViewHolder {
public NormalViewHolder(View itemView) {
super(itemView);
}
// find your views here
}
final class BigViewHolder extends RecyclerView.ViewHolder {
public BigViewHolder(View itemView) {
super(itemView);
}
// find your views here
}
}
其次,布局管理器应该实现一个监听器来指定在特定位置应该有多少跨度(行):
RecyclerView mRecyclerView = view.findViewById(R.id.my_recycler_view);
MyAdapter mAdapter = new MyAdapter();
GridLayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 2, LinearLayoutManager.HORIZONTAL, false);
mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
switch(mAdapter.getItemViewType(position)){
case MyAdapter.TYPE_FIRST_ITEM:
return 2;
case MyAdapter.TYPE_ITEM:
return 1;
default:
return -1;
}
}
});
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setLayoutManager(mLayoutManager);
希望它也能帮助到其他人。感谢您Mike M.的支持。