Creating RecyclerView and adjusting its margins

 


public class RecylerView_ScannedDocumentAdapter extends RecyclerView.Adapter<RecylerView_ScannedDocumentAdapter.ViewHolder> {
private Context context;
private List<Uri>imageUris;

public RecylerView_ScannedDocumentAdapter(Context context,List<Uri>imageUris) {
this.context=context;
this.imageUris=imageUris;
}

@NonNull

// for adding the layout inflater
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(context).inflate(R.layout.rv_item_scanned_images,parent,false);
return new ViewHolder(view);
}

//adding the data to the files
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Uri imageUri=imageUris.get(position);
holder.iv_scannedImages.setImageURI(imageUri);

}

//for running the recyclerview for specific length
@Override
public int getItemCount() {
return imageUris.size();
}





public static class ViewHolder extends RecyclerView.ViewHolder{
ImageView iv_scannedImages;

public ViewHolder(@NonNull View itemView) {
super(itemView);
iv_scannedImages=itemView.findViewById(R.id.iv_scannedImages);
}
}
}


rv_showScannedDocument = findViewById(R.id.rv_showScannedDocument);
rv_showScannedDocument.setLayoutManager(new LinearLayoutManager(this));
recylerView_scannedDocumentAdapter = new RecylerView_ScannedDocumentAdapter(this, imageUris);
rv_showScannedDocument.setAdapter(recylerView_scannedDocumentAdapter);


add margins to the items of recycler view 

if (position == fileName.size() - 1) {
// Set bottom margin for the last item
ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) holder.itemView.getLayoutParams();
layoutParams.bottomMargin = 250; // Set the margin you want
holder.itemView.setLayoutParams(layoutParams);
} else {
// Remove margin for other items (optional)
ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) holder.itemView.getLayoutParams();
layoutParams.bottomMargin = 0; // Reset margin for non-last items
holder.itemView.setLayoutParams(layoutParams);
}

Comments

Popular posts from this blog

How to add layout inflator (Layout Inflators)