使用带有StaggeredGridLayoutManager的RecyclerView不显示任何内容

bid*_*yut 1 android staggered-gridview android-recyclerview

我正在尝试使用RecyclerViews来构建像Pinterest交错排列的东西.但是,虽然LinearLayoutMnager和GridLayoutManager在同一代码中运行良好,但用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()));
Run Code Online (Sandbox Code Playgroud)

用GridLayoutManger替换StaggeredGridLayoutManager一切正常.

rView.setLayoutManager(new GridLayoutManager(this, 5));
Run Code Online (Sandbox Code Playgroud)

以下是适配器:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

以下是ViewHolder

private static class ItemViewHolder extends RecyclerView.ViewHolder {
    public final HiveView hive;

    public ItemViewHolder(View view) {
        super(view);
        this.hive = (HiveView) view;
    }
}
Run Code Online (Sandbox Code Playgroud)

最后是项目布局

<?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"/>
Run Code Online (Sandbox Code Playgroud)

yig*_*git 6

在您的示例中,您将StaggeredGrid设置为水平但网格垂直.如果在给定指定的HEIGHT和变量WIDTH时,您的项目视图无法正确测量自身,则可能会出现问题.

如果您可以为适配器和项目视图添加代码,我们可以找出问题所在.

编辑更新的问题

StaggeredGridLM(水平时)当前测量具有精确高度和无限宽度的子项.你设置的布局参数在这里没有帮助,因为它们被忽略了.在将来的版本中,它将开始读取滚动方向的值,但在我们引入重力支持之前,其他方向将保持不变.

您可以通过在HiveView中修复onMeasure方法来修复错误.