相关疑难解决方法(0)

如何使WRAP_CONTENT在RecyclerView上运行

我有一个DialogFragment包含RecyclerView(卡片列表).

其中RecyclerView一个或多个CardViews可以具有任何高度.

我想DialogFragment根据CardViews其中包含的内容给出正确的高度.

通常情况下,这将是简单的,我会成立wrap_contentRecyclerView这个样子.

<android.support.v7.widget.RecyclerView ...
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"   
    android:clickable="true"   
    android:scrollbars="vertical" >

</android.support.v7.widget.RecyclerView>
Run Code Online (Sandbox Code Playgroud)

因为我使用的RecyclerView这个不起作用,请看:

https://issuetracker.google.com/issues/37001674

嵌套的Recycler视图高度不会包装其内容

在这两个页面上,人们建议扩展LinearLayoutManager和覆盖onMeasure()

我首先使用了第一个链接中提供的LayoutManager:

public static class WrappingLayoutManager extends LinearLayoutManager {

        public WrappingLayoutManager(Context context) {
            super(context);
        }

        private int[] mMeasuredDimension = new int[2];

        @Override
        public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state,
                              int widthSpec, int heightSpec) {
            final int widthMode = View.MeasureSpec.getMode(widthSpec);
            final …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-fragments android-studio android-recyclerview

176
推荐指数
8
解决办法
12万
查看次数