我正在嘗試使用RecyclerViews來構建類似Pinterest交錯佈局的東西。但是,儘管LinearLayoutMnager和GridLayoutManager在相同的代碼中都能正常工作,但用StaggeredGridLayoutManager替換它並不顯示任何項目。在代碼中放置斷點表明它獲取了項目數量和項目,但是沒有任何渲染。任何想法我缺少什麼?使用RecyclerView和StaggeredGridLayoutManager沒有顯示任何東西
final RecyclerView rView = (RecyclerView) findViewById(R.id.app_list);
rView.setClickable(true);
rView.setHasFixedSize(true);
rView.setLayoutManager(new StaggeredGridLayoutManager(5, StaggeredGridLayoutManager.HORIZONTAL));
rView.setAdapter(new ItemListAdapter(this, getItems()));
只用GridLayoutManger替換StaggeredGridLayoutManager一切正常。
rView.setLayoutManager(new GridLayoutManager(this, 5));
以下是適配器:
private static class ItemListAdapter extends RecyclerView.Adapter<ItemViewHolder> {
private final LayoutInflater mInflater;
private final ArrayList<ItemDetail> mList;
public ItemListAdapter(Context context, ArrayList<ItemDetail> list) {
mInflater = LayoutInflater.from(context);
mList = list;
}
@Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int position) {
final View view = mInflater.inflate(R.layout.item_blob, parent, false);
return new ItemViewHolder(view);
}
@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
holder.hive.showDrawable(mList.get(position).icon);
}
@Override
public int getItemCount() {
return mList.size();
}
}
和下面是ViewHolder
private static class ItemViewHolder extends RecyclerView.ViewHolder {
public final HiveView hive;
public ItemViewHolder(View view) {
super(view);
this.hive = (HiveView) view;
}
}
最後的項目佈局
<?xml version="1.0" encoding="utf-8"?>
<com.bidyut.app.hivelauncher.widget.HiveView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="@dimen/app_blob_size"
android:layout_height="@dimen/app_blob_size"
android:background="@android:color/holo_red_light"/>
我已經使用您請求的信息編輯了原始問題。 – bidyut 2014-11-06 18:09:48
就是這樣!謝謝! – bidyut 2014-11-10 16:49:11