how to create recycler view list?
ReportQuestion
3 years ago 746 views
We can start creating Row (Item repeatedly holder use to create view) xml file then create adapter For Recycler View.
Row.xml
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_margin="@dimen/dim_3dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="@dimen/dim_80dp"
android:layout_height="@dimen/dim_80dp"
android:layout_margin="@dimen/dim_5dp"
android:src="@drawable/logo"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:id="@+id/row_service_vertical_image"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginStart="@dimen/dim_85dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="@dimen/dim_5dp"
android:layout_marginEnd="@dimen/dim_5dp"
android:layout_marginBottom="@dimen/dim_5dp"
app:layout_constraintStart_toEndOf="@+id/row_service_vertical_image">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
>
<TextView
android:id="@+id/row_service_vertical_name_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginStart="@dimen/dim_5dp"
android:layout_marginEnd="@dimen/dim_5dp"
android:gravity="bottom"
android:paddingTop="@dimen/dim_5dp"
android:text="@string/app_name"
android:textStyle="bold"
android:maxLines="2"
android:textColor="@color/black"
android:textSize="@dimen/dim_16dp" />
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/category"
android:textSize="@dimen/dim_14dp"
android:id="@+id/row_service_vertical_category_text_view"
android:layout_marginTop="@dimen/dim_3dp"
android:layout_marginStart="@dimen/dim_5dp"
android:layout_marginEnd="@dimen/dim_5dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Business Name"
android:textSize="@dimen/dim_14dp"
android:id="@+id/row_service_vertical_business_name_text_view"
android:layout_marginTop="@dimen/dim_3dp"
android:layout_marginStart="@dimen/dim_5dp"
android:layout_marginEnd="@dimen/dim_5dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/row_service_horizontal_category_text_view"
android:orientation="horizontal">
<TextView
android:id="@+id/row_service_horizontal_discounted_price_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/dim_5dp"
android:layout_marginTop="@dimen/dim_2dp"
android:layout_marginEnd="@dimen/dim_5dp"
android:layout_marginBottom="@dimen/dim_2dp"
android:textColor="@color/black"
android:text="₹1200"
android:textSize="@dimen/dim_14dp"
android:textStyle="bold" />
<TextView
android:id="@+id/row_service_horizontal_actual_price_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/dim_2dp"
android:layout_marginTop="@dimen/dim_2dp"
android:layout_marginEnd="@dimen/dim_5dp"
android:text="1300"
android:textSize="@dimen/dim_14dp"
android:layout_marginBottom="@dimen/dim_2dp"
android:textColor="@color/text_color_black_low"
/>
</LinearLayout>
</LinearLayout>
<TextView
android:id="@+id/product_discount_text_view"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginEnd="@dimen/dim_8dp"
android:background="@color/theme_color"
android:elevation="@dimen/dim_2dp"
android:paddingStart="@dimen/dim_5dp"
android:paddingTop="@dimen/dim_3dp"
android:paddingEnd="@dimen/dim_5dp"
android:textAllCaps="true"
android:text="10% OFF"
android:textSize="@dimen/dim_12dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textColor="@color/white"
android:paddingBottom="@dimen/dim_3dp"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
================================================
MainActivity.java
VerticalServiceAdapter verticalServiceAdapter = new VerticalServiceAdapter(this,listItems);
LinearLayoutManager layoutManager = new LinearLayoutManager(AllSearchActivity.this, LinearLayoutManager.VERTICAL, false); binding.allItemRecyclerView.setLayoutManager(layoutManager); binding.allItemRecyclerView.setAdapter(verticalServiceAdapter);
===================================================
Adapter class -> AnyNameAdapter.java
public class VerticalServiceAdapter extends RecyclerView.Adapter<VerticalServiceAdapter.VerticalServiceHolder> {
private Context context;
private List<ProductServiceSearchResponse.Result.DataItem> serviceList;
DecimalFormat formatter ;
public VerticalServiceAdapter(Context context, List<ProductServiceSearchResponse.Result.DataItem> serviceList) {
this.context = context;
this.serviceList = serviceList;
formatter = new DecimalFormat("##,##,##,##,##,##,##0");
}
@NonNull
@NotNull
@Override
public VerticalServiceHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
RowVerticalServiceListSearchBinding binding = RowVerticalServiceListSearchBinding.inflate(LayoutInflater.from(context),parent,false);
return new VerticalServiceHolder(binding);
}
@Override
public void onBindViewHolder(@NonNull @NotNull VerticalServiceHolder holder, int position) {
ProductServiceSearchResponse.Result.DataItem data = serviceList.get(position);
Glide.with(context).load(data.getThumbnailImg()).into(holder.binding.rowServiceVerticalImage);
holder.binding.rowServiceVerticalNameTextView.setText(data.getName());
holder.binding.rowServiceVerticalCategoryTextView.setText(data.getCategoryName());
holder.binding.rowServiceVerticalBusinessNameTextView.setText(data.getBusinessName());
holder.binding.rowServiceHorizontalDiscountedPriceTextView.setText("₹"+formatter.format(data.getDiscountedPrice()));
holder.binding.rowServiceHorizontalActualPriceTextView.setText(String.valueOf(data.getUnitPrice()));
holder.binding.rowServiceHorizontalActualPriceTextView.setPaintFlags(holder.binding.rowServiceHorizontalActualPriceTextView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
if(data.getDiscountType()!=null) {
if (data.getDiscountType().equalsIgnoreCase("percent") && data.getDiscount() != 0) {
CustomMethods.setTextData(holder.binding.productDiscountTextView, data.getDiscount() + "% OFF");
} else if (data.getDiscountType().equalsIgnoreCase("amount") && data.getDiscount() != 0) {
CustomMethods.setTextData(holder.binding.productDiscountTextView, "₹" + formatter.format(data.getDiscount()) + " OFF");
} else {
holder.binding.productDiscountTextView.setVisibility(View.GONE);
}
}
else{
holder.binding.productDiscountTextView.setVisibility(View.GONE);
}
if(data.getDiscount()==0){
holder.binding.rowServiceHorizontalActualPriceTextView.setVisibility(View.GONE);
}
}
@Override
public int getItemCount() {
return serviceList.size();
}
public class VerticalServiceHolder extends RecyclerView.ViewHolder {
RowVerticalServiceListSearchBinding binding;
public VerticalServiceHolder(@NonNull @NotNull RowVerticalServiceListSearchBinding binding) {
super(binding.getRoot());
this.binding = binding;
binding.getRoot().setOnClickListener(v->{
Intent intent = new Intent(context, ServiceDetailsActivity.class);
intent.putExtra("name",String.valueOf(serviceList.get(getAdapterPosition()).getName()));
intent.putExtra("id",String.valueOf(serviceList.get(getAdapterPosition()).getId()));
context.startActivity(intent);
});
}
}
}
extends RecyclerView.Adapter<VerticalServiceAdapter.VerticalServiceHolder> {
private Context context;
private List<ProductServiceSearchResponse.Result.DataItem> serviceList;
DecimalFormat formatter ;
public VerticalServiceAdapter(Context context, List<ProductServiceSearchResponse.Result.DataItem> serviceList) {
this.context = context;
this.serviceList = serviceList;
formatter = new DecimalFormat("##,##,##,##,##,##,##0");
}
@NonNull
@NotNull
@Override
public VerticalServiceHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
RowVerticalServiceListSearchBinding binding = RowVerticalServiceListSearchBinding.inflate(LayoutInflater.from(context),parent,false);
return new VerticalServiceHolder(binding);
}
@Override
public void onBindViewHolder(@NonNull @NotNull VerticalServiceHolder holder, int position) {
ProductServiceSearchResponse.Result.DataItem data = serviceList.get(position);
Glide.with(context).load(data.getThumbnailImg()).into(holder.binding.rowServiceVerticalImage);
holder.binding.rowServiceVerticalNameTextView.setText(data.getName());
holder.binding.rowServiceVerticalCategoryTextView.setText(data.getCategoryName());
holder.binding.rowServiceVerticalBusinessNameTextView.setText(data.getBusinessName());
holder.binding.rowServiceHorizontalDiscountedPriceTextView.setText("₹"+formatter.format(data.getDiscountedPrice()));
holder.binding.rowServiceHorizontalActualPriceTextView.setText(String.valueOf(data.getUnitPrice()));
holder.binding.rowServiceHorizontalActualPriceTextView.setPaintFlags(holder.binding.rowServiceHorizontalActualPriceTextView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
if(data.getDiscountType()!=null) {
if (data.getDiscountType().equalsIgnoreCase("percent") && data.getDiscount() != 0) {
CustomMethods.setTextData(holder.binding.productDiscountTextView, data.getDiscount() + "% OFF");
} else if (data.getDiscountType().equalsIgnoreCase("amount") && data.getDiscount() != 0) {
CustomMethods.setTextData(holder.binding.productDiscountTextView, "₹" + formatter.format(data.getDiscount()) + " OFF");
} else {
holder.binding.productDiscountTextView.setVisibility(View.GONE);
}
}
else{
holder.binding.productDiscountTextView.setVisibility(View.GONE);
}
if(data.getDiscount()==0){
holder.binding.rowServiceHorizontalActualPriceTextView.setVisibility(View.GONE);
}
}
@Override
public int getItemCount() {
return serviceList.size();
}
public class VerticalServiceHolder extends RecyclerView.ViewHolder {
RowVerticalServiceListSearchBinding binding;
public VerticalServiceHolder(@NonNull @NotNull RowVerticalServiceListSearchBinding binding) {
super(binding.getRoot());
this.binding = binding;
binding.getRoot().setOnClickListener(v->{
Intent intent = new Intent(context, ServiceDetailsActivity.class);
intent.putExtra("name",String.valueOf(serviceList.get(getAdapterPosition()).getName()));
intent.putExtra("id",String.valueOf(serviceList.get(getAdapterPosition()).getId()));
context.startActivity(intent);
});
}
}
}
Thread Reply