How to use RecyclerView

/** Add the dependency into the gradle file **/
implementation 'com.android.support:recyclerview-v7:27.1.1'

/** Add this view into xml file **/
 <android.support.v7.widget.RecyclerView       
        android:id="@+id/item_recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="4dp"/>


/** Added this into onCreate activity **/
ArrayList<HashMap<String, String>> Item_List = new ArrayList<>();
PaymentAdapter checkoutAdapter = new PaymentAdapter(this, Item_List);

RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
item_recyclerView.setLayoutManager(mLayoutManager);
item_recyclerView.setItemAnimator(new DefaultItemAnimator());

//this line is optional, it's draw the line between two items
item_recyclerView.addItemDecoration(new MyDividerItemDecoration(this, LinearLayoutManager.VERTICAL, 16));

item_recyclerView.setAdapter(checkoutAdapter);

//call notifyDataSetChanged after add the item in Item_List
checkoutAdapter.notifyDataSetChanged();


/** RecyclerView Adapter **/
public class PaymentAdapter extends RecyclerView.Adapter<PaymentAdapter.MyViewHolder> {
    Activity activity;
    private ArrayList<HashMap<String,String>> data;
    DatabaseHelper my_Database;

    public PaymentAdapter(Activity activity, ArrayList<HashMap<String, String>> cart_item_list) {
        this.activity = activity;
        this.data = cart_item_list;
        my_Database = new DatabaseHelper(activity);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.ordered_item_design, parent, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        HashMap<String, String> hashMap = data.get(position);
        String name = hashMap.get("name");
        String price = hashMap.get("price");
        final int[] quantity = {Integer.parseInt(hashMap.get("quantity"))};
        holder.prodct_list.setText(name);
        holder.list_quant.setText(quantity[0]+"");
        holder.list_price.setText("$ "+Double.parseDouble(price)*quantity[0]);
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView prodct_list, list_quant, list_price;
        MyViewHolder(View root) {
            super(root);
            list_quant = root.findViewById(R.id.list_quant);
            prodct_list = root.findViewById(R.id.prodct_list);
            list_price = root.findViewById(R.id.list_price);
        }
    }


}


/** LayoutInflater item list design **/
 <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="42dp"
        android:orientation="horizontal"
        android:weightSum="4">
        <TextView
            android:id="@+id/txt_date"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Date"
            android:textColor="@color/colorPrimaryDark"
            android:gravity="center"
            android:textSize="14sp"/>
        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@color/placeholder_bg"/>
        <TextView
            android:id="@+id/txt_time"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Time"
            android:textColor="@color/colorPrimaryDark"
            android:gravity="center"
            android:textSize="14sp"/>
        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@color/placeholder_bg"/>
        <TextView
            android:id="@+id/txt_guests"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Guests"
            android:textColor="@color/colorPrimaryDark"
            android:gravity="center"
            android:textSize="14sp"/>
        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@color/placeholder_bg"/>
        <TextView
            android:id="@+id/txt_status"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Status"
            android:textColor="@color/colorPrimaryDark"
            android:gravity="center"
            android:textSize="14sp"/>
    </LinearLayout>

Comments