我有一个带有RecyclerView的ConstraintLayout内置的警告对话框。 我的问题是,回收视图没有固定的大小,它只显示几个项目(它可滚动,但我需要它更大)。有没有办法让回收站视图显示固定数量的物品?
这是我的布局:
<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:orientation="vertical">
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:fontFamily="sans-serif-light"
android:text="@string/traces_dialog_title"
android:textColor="@color/black_color"
android:textSize="26sp"
app:layout_constraintBottom_toBottomOf="@+id/view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/view"
android:layout_width="0dp"
android:layout_height="0.5dp"
android:layout_marginBottom="45dp"
android:layout_marginTop="53dp"
android:background="@color/graySeparatorBarColor"
app:layout_constraintBottom_toBottomOf="@+id/linearLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="4dp"
android:fontFamily="sans-serif-light"
android:text="@string/order_number_label"
android:textColor="@color/black_color"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<TextView
android:id="@+id/order_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:fontFamily="sans-serif-light"
android:text="#12345"
android:textColor="@color/black_color"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/medication_name"
app:layout_constraintStart_toEndOf="@+id/linearLayout"
app:layout_constraintTop_toTopOf="@+id/linearLayout" />
<TextView
android:id="@+id/medication_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="4dp"
android:fontFamily="sans-serif"
android:text="Ciclofosfamida 500mg"
android:textColor="@color/black_color"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout" />
<android.support.v7.widget.RecyclerView
android:id="@+id/traces"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:nestedScrollingEnabled="false"
android:overScrollMode="never"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/medication_name"
app:layout_constraintVertical_chainStyle="spread_inside"
app:layout_constraintBottom_toTopOf="@+id/view3"
android:layout_marginBottom="16dp">
</android.support.v7.widget.RecyclerView>
<View
android:id="@+id/view3"
android:layout_width="0dp"
android:layout_height="0.5dp"
android:layout_marginBottom="8dp"
android:background="@color/graySeparatorBarColor"
app:layout_constraintBottom_toTopOf="@+id/close"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:fontFamily="sans-serif-medium"
android:paddingBottom="@dimen/spacingTiny"
android:paddingTop="@dimen/spacingTiny"
android:text="@string/accept_delivery"
android:textColor="@color/buttonColor"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.ConstraintLayout>
这就是我如何打开和处理AlertDialog:
private void openTracesDialog(final Medication medication) {
AlertDialog.Builder builder;
final AlertDialog alertDialog;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.show_traces_dialog, null);
RecyclerView traceList = (RecyclerView) layout.findViewById(R.id.traces);
TextView orderNumber = (TextView) layout.findViewById(R.id.order_number);
TextView medicationName = (TextView) layout.findViewById(R.id.medication_name);
medicationName.setText(medication.getName());
orderNumber.setText("#" + order.getOrderNumber());
traceList.setLayoutManager(new LinearLayoutManager(mContext));
traceList.setAdapter(new RecyclerView.Adapter<TraceHolder>() {
@Override
public TraceHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.item_trace, null);
v.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT));
return new MedicationExtendedAdapter.TraceHolder(v);
}
@Override
public void onBindViewHolder(TraceHolder holder, int position) {
int tracePosition = position+1;
holder.mTraceLabel.setText("TRAZA "+tracePosition);
holder.mTraceNumber.setText(medication.getTraces().get(position));
}
@Override
public int getItemCount() {
return medication.getTraces().size();
}
});
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
TextView closeButton = (TextView) layout.findViewById(R.id.close);
closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
alertDialog.show();
}
使'getItemCount()'总是返回相同的数字,假设这就是你的意思 –