android - 如何将 gridview 的高度设置为 wrap_content 到 recyclerview?

标签 android

我有一个 recyclerview,我将其设置为具有两列的 gridview。我为 recyclerview 适配器设置了一个 cardview 自定义。现在我的 gridview 不是 wrap_content,其值在 cardview 并且所有项目都具有相同的大小。 我怎样才能用 cardview 的内容包裹 gridview 的高度?

private void getuserposts() {
    StringRequest request = new StringRequest(
            Config.GET_POST_USER,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String s) {
                        posts = Config.Jsonparserpostuser(s);
                        if(posts.size() != 0){
                            adapter = new PostsAdapterProfile(posts,activity.getApplicationContext());
                            RecyclerView.LayoutManager layoutManager = new GridLayoutManager(activity.getApplicationContext(),2);
                            recyclerView.setLayoutManager(layoutManager);
                            recyclerView.setAdapter(adapter);
                        } else if(posts.size() == 0){
                            noresult.setVisibility(View.VISIBLE);
                            noresult.setImageResource(R.drawable.noresult);
                        }
                    }

            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
                VolleyLog.d("Profile Error ", "Error is " + error.getMessage());

        }
    }){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String,String> param = new HashMap<>();
                param.put("email",email);
            return param;
        }
    };
    RequestQueue queue = Volley.newRequestQueue(activity.getApplicationContext());
    queue.add(request);
}

cardview.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_margin="10dp"
    app:cardCornerRadius="1dp"
    app:cardBackgroundColor="#fff"
    app:cardElevation="0dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >

        <ImageView
            android:id="@+id/img_profile_card"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:scaleType="centerCrop"
            android:layout_margin="1dp"
            android:adjustViewBounds="true"
            android:src="@drawable/loadback"
           />

        <TextView
            android:id="@+id/txt_desc_profile_card"
            android:layout_width="match_parent"
            android:layout_margin="8dp"
            android:layout_height="wrap_content"
            android:hint="Description"/>
        <ImageView
            android:layout_width="25dp"
            android:layout_height="45dp"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"
            android:src="@drawable/yellowheart"
            android:layout_gravity="center"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="1.026 people"
            android:layout_gravity="center"
            android:layout_marginBottom="5dp"/>
    </LinearLayout>

</android.support.v7.widget.CardView>

Activity .xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:orientation="vertical"
    tools:context=".ProfileFragment">

    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:orientation="horizontal"
        android:gravity="center"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_marginRight="15dp"
            >


            <ImageView
                android:src="@drawable/icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />


        </LinearLayout>

    </android.support.v7.widget.Toolbar>

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="37dp">


        <android.support.v7.widget.CardView
            android:id="@+id/cardView"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_alignParentTop="true"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <android.support.constraint.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="0dp">

                <android.support.constraint.ConstraintLayout
                    android:id="@+id/constraintLayout"
                    android:layout_width="match_parent"
                    android:layout_height="88dp"
                    android:layout_marginEnd="8dp"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="8dp"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent">


                    <ImageView
                        android:id="@+id/prfile_img_user"
                        android:layout_width="80dp"
                        android:layout_height="80dp"
                        android:layout_marginBottom="8dp"
                        android:layout_marginTop="8dp"
                        android:background="@drawable/strokeimageview"
                        android:src="@drawable/avatar"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toStartOf="@+id/textView3"
                        app:layout_constraintHorizontal_bias="0.5"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        app:layout_constraintVertical_bias="0.0" />

                    <TextView
                        android:id="@+id/textView3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginEnd="8dp"
                        android:layout_marginLeft="8dp"
                        android:layout_marginRight="8dp"
                        android:layout_marginStart="8dp"
                        android:layout_marginTop="8dp"
                        android:hint="Posts"
                        app:layout_constraintEnd_toStartOf="@+id/textView2"
                        app:layout_constraintHorizontal_bias="0.5"
                        app:layout_constraintStart_toEndOf="@+id/prfile_img_user"
                        app:layout_constraintTop_toTopOf="parent" />

                    <TextView
                        android:id="@+id/profile_txt_postcount"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="36dp"
                        android:layout_marginStart="36dp"
                        android:layout_marginTop="12dp"
                        android:hint="0"
                        android:textStyle="bold"
                        app:layout_constraintStart_toEndOf="@+id/prfile_img_user"
                        app:layout_constraintTop_toBottomOf="@+id/textView3" />

                    <TextView
                        android:id="@+id/textView4"
                        android:layout_width="wrap_content"

                        android:layout_height="wrap_content"
                        android:layout_marginEnd="8dp"
                        android:layout_marginLeft="8dp"
                        android:layout_marginRight="8dp"
                        android:layout_marginStart="8dp"
                        android:layout_marginTop="8dp"
                        android:hint="Folowers"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintHorizontal_bias="0.5"
                        app:layout_constraintStart_toEndOf="@+id/textView2"
                        app:layout_constraintTop_toTopOf="parent" />

                    <TextView
                        android:id="@+id/profile_txt_folowing_count"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="56dp"
                        android:layout_marginStart="56dp"
                        android:layout_marginTop="12dp"
                        android:hint="0"
                        android:textStyle="bold"
                        app:layout_constraintStart_toEndOf="@+id/profile_txt_postcount"
                        app:layout_constraintTop_toBottomOf="@+id/textView2" />

                    <TextView
                        android:id="@+id/textView2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="8dp"
                        android:hint="Folowing"
                        app:layout_constraintEnd_toStartOf="@+id/textView4"
                        app:layout_constraintHorizontal_bias="0.5"
                        app:layout_constraintStart_toEndOf="@+id/textView3"
                        app:layout_constraintTop_toTopOf="parent" />

                    <TextView
                        android:id="@+id/profile_txt_folowers_Count"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginEnd="8dp"
                        android:layout_marginLeft="8dp"
                        android:layout_marginRight="8dp"
                        android:layout_marginStart="8dp"
                        android:layout_marginTop="12dp"
                        android:hint="0"
                        android:textStyle="bold"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintHorizontal_bias="0.64"
                        app:layout_constraintStart_toEndOf="@+id/profile_txt_folowing_count"
                        app:layout_constraintTop_toBottomOf="@+id/textView4" />


                    <Button
                        android:id="@+id/profile_btn_edit"
                        android:layout_width="80dp"
                        android:layout_height="27dp"
                        android:layout_marginEnd="8dp"
                        android:layout_marginLeft="8dp"
                        android:layout_marginRight="8dp"
                        android:layout_marginStart="8dp"
                        android:background="@drawable/roundedbutton"
                        android:text="Edit"
                        android:textColor="#000"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintHorizontal_bias="1.0"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@+id/profile_txt_folowing_count" />

                    <Button
                        android:id="@+id/profile_btn_save"
                        android:layout_width="wrap_content"
                        android:layout_height="30dp"
                        android:layout_marginEnd="8dp"
                        android:layout_marginLeft="8dp"
                        android:layout_marginRight="8dp"
                        android:layout_marginStart="8dp"
                        android:layout_marginTop="16dp"
                        android:background="@drawable/roundedbutton"
                        android:text="Save"
                        android:textColor="#000"
                        android:visibility="gone"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintHorizontal_bias="1.0"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@+id/profile_txt_folowing_count" />

                </android.support.constraint.ConstraintLayout>

                <EditText
                    android:id="@+id/profile_edt_namefamily"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_marginBottom="8dp"
                    android:layout_marginEnd="8dp"
                    android:layout_marginRight="8dp"
                    android:layout_marginTop="8dp"
                    android:background="@drawable/edt_create2"
                    android:enabled="false"
                    android:hint="NAME AND FAMILY"
                    android:padding="5dp"
                    android:textSize="14sp"
                    android:textStyle="bold"
                    app:layout_constraintBottom_toTopOf="@+id/profile_edt_username"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.019"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/constraintLayout"
                    app:layout_constraintVertical_bias="0.0" />

                <EditText
                    android:id="@+id/profile_edt_username"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="8dp"
                    android:layout_marginRight="8dp"
                    android:background="@drawable/edt_create2"
                    android:enabled="false"
                    android:hint="Username"
                    android:padding="5dp"
                    android:singleLine="true"
                    android:textSize="12sp"
                    app:layout_constraintBottom_toTopOf="@+id/profile_edt_bio"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.014"
                    app:layout_constraintStart_toStartOf="parent" />

                <EditText
                    android:id="@+id/profile_edt_bio"
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:layout_marginBottom="4dp"
                    android:layout_marginEnd="8dp"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:layout_marginStart="8dp"
                    android:background="@drawable/edt_create2"
                    android:enabled="false"
                    android:gravity="left"
                    android:hint="Bio..."
                    android:textSize="12sp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.0"
                    app:layout_constraintStart_toStartOf="parent" />

            </android.support.constraint.ConstraintLayout>

        </android.support.v7.widget.CardView>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_profile"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="215dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/cardView"
            app:layout_constraintVertical_bias="0.0">

        </android.support.v7.widget.RecyclerView>

        <ImageView
            android:id="@+id/noresult_profile"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:src="@drawable/noresult"
            android:visibility="gone"
            app:layout_constraintBottom_toBottomOf="@+id/recycler_profile"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="@+id/recycler_profile"
            app:layout_constraintTop_toTopOf="@+id/recycler_profile" />

    </android.support.constraint.ConstraintLayout>
</FrameLayout>

最佳答案

只需使用 StaggeredGridLayoutManager 而不是 GridLayoutManager

StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(staggeredGridLayoutManager);

更多 https://developer.android.com/reference/android/support/v7/widget/StaggeredGridLayoutManager.html

关于android - 如何将 gridview 的高度设置为 wrap_content 到 recyclerview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51587348/

相关文章:

java - 位图在 SurfaceView 上多次绘制

android - 如何在编辑文本事件后将下一个图像设置为数据库中的 ImageView?

android - 刷新 ListPreference

android - 停止 Android 服务中的线程

android - 在 Android 中使用 LibPhoneNumber google lib

android - 我的Android移动应用程序无法在android电视上运行

Android - 如何在滚动时停止在 Recyclerview 中重新加载图像

java - 如何快速地将位图从一个 Activity 传递到另一个 Activity ?

java - Gradle:查找使用 + 导入的依赖项的已解析版本

Android - 我的应用程序中的 Microsoft Office 查看器