Reputation: 19
I have an alert dialog built in a ConstraintLayout with a RecyclerView in it. My problem is that recycler view has not a fixed size and it only displays a few items (it's scrollable though, but I need it to be bigger).
This is my layout:
<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>
And this is how I open and handle the 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();
}
Upvotes: 1
Views: 2615
Reputation: 97
you can change the return of this method
@Override
public int getItemCount() {
return medication.getTraces().size();
}
to a fixed number
@Override
public int getItemCount() {
return YOUR_FIXED_NUMBER;
}
but you should take care if the size of your list is inferior than YOUR_FIXED_NUMBER
you will have crashes so you should add a test for this, like that:
@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
}
Upvotes: 1
Reputation: 19
Already fixed this. What I did was to set a fixed height depending on the screen to make the Dialog bigger. As all elements are constrainted, making Dialog higher makes the RecyclerView higher also.
ConstraintLayout popUpLayout = (ConstraintLayout) layout.findViewById(R.id.constraint);
popUpLayout.setMinHeight((int) (mContext.getResources().getDisplayMetrics().heightPixels * 0.75));
Upvotes: 1
Reputation: 651
Change your:
@Override
public int getItemCount() {
return medication.getTraces().size();
}
to:
@Override
public int getItemCount() {
return 100;
}
or whatever value you want.
Upvotes: 1
Reputation: 113
You should override the layoutmanager of your recycleview for this. This way it will only disable scrolling.
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();
}
}
now add your custom layout manager to your recyclerview now you can disable and enable scrolling functionality of your recyclerview
linearLayoutManager = new LinearLayoutManager(context) {
@Override
public boolean canScrollVertically() {
return false;
}
};
Upvotes: 0
Reputation: 645
If RecyclerView has children (items) that has fixed width and height. For allowing the RecyclerView
to optimize better by figuring out the exact height and width of the entire list based on the your adapter, use this line:
recyclerView.setHasFixedSize(true)
Upvotes: 0
Reputation: 11491
Is there a way to make a recycler view show a fixed number of items?
Using the default AlertDialog, it is not possible. Why you really need to change the default behaviour?
Anyway you can create your own custom fragment layout instead of the default DialogFragment or AlertDialog with transparent background so that you can assign it to any height you want. Then show/hide that fragment whenever you want instead of showing an alert dialog.
Upvotes: 1