我正在尝试制作类似这样的东西。每个项目都有相同的宽度和高度。我将宽度设置为match_parent
并动态更改高度在onBindView()
这是我到目前为止所做的。我试图在cardView
内获得onBindView
(项目的容器)的宽度并将其指定为高度。
但是在记录时总是给-1
activity.java
private void setupRecyclerView() {
categoryRecyclerAdapter = new CategoryRecyclerAdapter(this);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this, 2);
categoryRecyclerView.setLayoutManager(layoutManager);
categoryRecyclerView.setAdapter(categoryRecyclerAdapter);
RecyclerView.ItemDecoration itemDecoration = new ItemDecorationCategory(5, 2);
categoryRecyclerView.addItemDecoration(itemDecoration);
}
adapter.java
@Override
public void onBindViewHolder(CategoryViewHolder holder, int position) {
Category category = categoryList.get(position);
int width = holder.cardViewContainer.getMeasuredWidth(); //returns -1
holder.cardViewContainer.getLayoutParams().height = width;
Timber.d("width --> " + width);
holder.cardViewContainer.requestLayout();
holder.textViewCat.setText(category.getCategory());
Picasso.with(context).load(category.getResId()).into(holder.imageViewCat);
}
public class CategoryViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.cardViewContainer)
CardView cardViewContainer;
@BindView(R.id.cat_imageView)
ImageView imageViewCat;
@BindView(R.id.cat_textView)
TextView textViewCat;
public CategoryViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
}
activity.xml
<android.support.constraint.ConstraintLayout 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:background="@color/grey"
tools:context="com.project.bishoy.lost.category.CategoryActivity">
<android.support.v7.widget.RecyclerView
tools:listitem="@layout/category_item"
android:id="@+id/category_recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
item.xml
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardViewContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
app:cardCornerRadius="5dp"
app:elevation="5dp">
<android.support.constraint.ConstraintLayout 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="200dp"
tools:layout_editor_absoluteY="25dp">
<ImageView
android:id="@+id/cat_imageView"
android:layout_width="72dp"
android:layout_height="72dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/main_background" />
<TextView
android:id="@+id/cat_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:text="@string/mob"
android:textColor="@color/black"
android:textSize="15dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/cat_imageView"
app:layout_constraintVertical_bias="0.241" />
</android.support.constraint.ConstraintLayout>
我尝试了tf.reset_default_graph
,但没有帮助