我有一个recyclerview
,我将其设置为两列的gridview
。我为cardview
适配器设置了recyclerview
自定义。现在,我的gridview
不是wrap_content
,其值在cardview
中,并且所有项目的大小均相等。
我如何用gridview
的内容包装cardview
的高度?
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>
答案 0 :(得分:1)
只需使用StaggeredGridLayoutManager
代替GridLayoutManager
StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(staggeredGridLayoutManager);
更多 https://developer.android.com/reference/android/support/v7/widget/StaggeredGridLayoutManager.html