目前,我正在探索更多关于recyclerview和gridlayoutmanager的内容,并试图为gallery显示实现一个漂亮的ui,但是gridlayoutmanager的跨度大小有一些问题。正如documentation所说:
默认情况下,每个项目占用1个跨度。您可以通过设置pansizelookup(spansizelookup)提供自定义gridlayoutmanager.spansizelookup实例来更改它。
有了这个,我希望如果我将项目的跨距设置为2,位置为0,那么colspan和rowspan也将设置为2,但似乎不是这样工作的。我设置我的列计数为2为我的GridLayoutManager为纵向和4为横向。它对肖像画效果很好,但对风景画效果不好,因为它在第二和第三个项目下面创建了两个空单元格,我猜第一个项目没有获得第二和第三个项目的行宽,否则它真的会留下一个空单元格。
这是我的代码:
gallery = new ArrayList<HashMap<String, String>>();
adapter = new GalleryAdapter(getBaseContext(), gallery);
// using grid layout manager
GridLayoutManager mLayoutManager = new GridLayoutManager(this, getResources().getInteger(R.integer.col_count));
mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
switch(adapter.getItemViewType(position)){
case 2:
return 2;
case 1:
return 1;
default:
return -1;
}
}
});
list_news.setLayoutManager(mLayoutManager);
list_news.setAdapter(adapter);
Adapter:
public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.ViewHolder> {
Context context;
ArrayList<HashMap<String, String>> article;
ImageLoader mImageLoader;
@Override
public int getItemCount() {
return article.size();
}
public GalleryAdapter(Context context, ArrayList<HashMap<String, String>> article){
this.context = context;
this.article = article;
}
// Create new views (invoked by the layout manager)
@Override
public GalleryAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.articles_item, parent, false);
// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public int getItemViewType(int position) {
// Just as an example, return 0 or 2 depending on position
// Note that unlike in ListView adapters, types don't have to be contiguous
if(position==0){
return 2;
}else{
return 1;
}
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
mImageLoader = VolleyQueue.getInstance(context).getImageLoader();
holder.image.setImageUrl(article.get(position).get("image"), mImageLoader);
holder.image.setDefaultImageResId(R.drawable.test);
}
static class ViewHolder extends RecyclerView.ViewHolder {
@Bind(R.id.image) NetworkImageView image;
public ViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
}
}
}
下面是横向和纵向的结果截图:
有什么办法可以避免空白单元格吗?
更新:
为了更好地想象我正在努力实现的目标,这里有一个我想要的景观模式的图像。数字是基于预期位置的项目。
最佳答案
将SpanSizeLookup
代码更改为
mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
return adapter.getItemViewType(position);
}
});
适配器的
getItemViewType
代码@Override
public int getItemViewType(int position) {
if (position == 0) {
return context.getResources().getInteger(R.integer.col_count);
} else {
return 1;
}
}