有没有办法让回收站视图显示固定数量的物品?
问题描述:
我有一个带有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();
}
答
已经解决了这个问题。我所做的是根据屏幕设置一个固定的高度以使对话框变大。由于所有元素都受到限制,因此使Dialog更高,RecyclerView也会更高。
ConstraintLayout popUpLayout = (ConstraintLayout) layout.findViewById(R.id.constraint);
popUpLayout.setMinHeight((int) (mContext.getResources().getDisplayMetrics().heightPixels * 0.75));
答
有没有一种方法,使回收视图显示固定数量的项目?
使用默认AlertDialog是不可能的。为什么你真的需要改变默认行为?
无论如何,您可以创建自己的自定义片段布局,而不是使用透明背景的默认DialogFragment或AlertDialog,以便您可以将其分配给任何需要的高度。然后显示/隐藏该片段,而不是显示警告对话框。
答
如果RecyclerView包含具有固定宽度和高度的子项(项目)。为了使RecyclerView
优化搞清楚精确的高度和基于你的适配器上的整个列表的宽度较好,使用该行:
recyclerView.setHasFixedSize(true)
答
你应该重写你的recycleview本的布局管理器。这样它只会禁用滚动。
public class CustomGridLayoutManager extends LinearLayoutManager {
private boolean isScrollEnabled = true; // create boolean variable here
public CustomGridLayoutManager(Context context) {
super(context);
}
// create method to enable or disable scrolling
public void setScrollEnabled(boolean flag) {
this.isScrollEnabled = flag;
}
@Override
public boolean canScrollVertically() {
//Similarly you can customize "canScrollHorizontally()" for managing
//horizontal scroll
return isScrollEnabled && super.canScrollVertically();
}
}
现在您的自定义布局管理器添加到您的recyclerview 现在你可以禁用和启用滚动您recyclerview功能
linearLayoutManager = new LinearLayoutManager(context) {
@Override
public boolean canScrollVertically() {
return false;
}
};
答
更改:
@Override
public int getItemCount() {
return medication.getTraces().size();
}
到:
@Override
public int getItemCount() {
return 100;
}
或任何你想要的值。
答
您可以将此方法
@Override
public int getItemCount() {
return medication.getTraces().size();
}
返回更改为固定数量
@Override
public int getItemCount() {
return YOUR_FIXED_NUMBER;
}
但如果你的列表的大小比YOUR_FIXED_NUMBER
你会不如你应该照顾有崩溃,所以你应该为此添加一个测试,如下所示:
@Override
public int getItemCount() {
if(medication.getTraces().size() > YOUR_FIXED_NUMBER)
return YOUR_FIXED_NUMBER;
else
return medication.getTraces().size(); //or any lower number depends on your need
}
使'getItemCount()'总是返回相同的数字,假设这就是你的意思 –