1
\$\begingroup\$

So I've been working on a RecyclerView recently that's in a popup, and it takes a few seconds to popup with around 100 items. I want to make this faster, so any improvements on my code is appreciated :)

layout/activity_supervisor_order_popup.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".layoutfiles.SupervisorView.OrderPopup">

    <LinearLayout android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:tools="http://schemas.android.com/tools"
                  android:orientation="vertical">
        <TextView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:id="@+id/materialsTitle"
                android:textSize="70dp"
                android:gravity="center"
                android:layout_weight="0.1"
        />

        <!--<include layout="@layout/card_supervisor_order_item"-->
                 <!--android:layout_below="@+id/materialsTitle"-->
                 <!--android:layout_width="match_parent"-->
                 <!--android:layout_height="0dp"-->
                 <!--android:layout_weight="0.1"-->
                 <!--android:id="@+id/firstRow"/>-->

        <androidx.recyclerview.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                tools:layout_editor_absoluteY="8dp"
                tools:layout_editor_absoluteX="8dp"
                android:id="@+id/popupRecycler"
                android:layout_weight="0.7"
        />

        <LinearLayout android:layout_width="match_parent"
                      android:layout_height="0dp"
                      android:orientation="horizontal"
                      android:layout_weight="0.1"
        >

            <Button android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/orderSelectedButton"
                    android:text="Order Selected"
                    android:layout_weight="1"
            />

            <Button android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/orderAllButton"
                    android:text="Order All"
                    android:layout_weight="1"

            />

            <Button android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/cancelButton"
                    android:text="Cancel"
            android:layout_weight="1"

            />

        </LinearLayout>


    </LinearLayout>


</androidx.coordinatorlayout.widget.CoordinatorLayout>

card_supervisor_order_item.xml

<androidx.cardview.widget.CardView 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="wrap_content"
                                   android:id="@+id/workOrderCardView"
                                   android:layout_margin="5dp"
                                   app:cardBackgroundColor="#FCFCFC"
                                   app:cardCornerRadius="12dp"
                                   app:cardElevation="3dp"
                                   app:contentPadding="4dp"
                                   android:gravity="center"
                                   tools:context=".layoutfiles.SupervisorView.OrderPopup"

>
    <TableLayout android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:gravity="center"
    >
        <TableRow
        >

            <ImageButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/addRemove"
                    android:layout_gravity="center"
                    android:gravity="center"
                    android:src="@drawable/item_order_unchecked"
            />
            <TextView
                    android:background="@drawable/cellshape"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:textColor="@color/cardview_dark_background"
                    android:textSize="30sp"
                    android:id="@+id/materialName"
                    android:gravity="center"
                    android:layout_weight="1"/>
            <TextView
                    android:background="@drawable/cellshape"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:id="@+id/materialQty"
                    android:textColor="@color/cardview_dark_background"
                    android:gravity="center"
                    android:textSize="30sp"
                    android:layout_weight="1"/>
        </TableRow>
    </TableLayout>


</androidx.cardview.widget.CardView>

OrderPopup.kt

package com.rkalim.digifloor.layoutfiles.SupervisorView

import android.app.Activity
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.widget.*
import androidx.core.app.NotificationCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.rkalim.digifloor.R
import com.rkalim.digifloor.models.RequiredItem
import kotlinx.android.synthetic.main.recycler_supervisor_order.*

class OrderPopup : Activity() {
    lateinit var adapter: RequiredItemAdapter
    lateinit var titleFromIntent: String
    var dataListSend: MutableList<RequiredItem> = ArrayList()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        this.requestWindowFeature(Window.FEATURE_NO_TITLE)

        var extras = intent.extras

        if (extras != null) {
            titleFromIntent = extras.getString("title")
            Log.d("PutExtra", titleFromIntent)
        }

        setContentView(R.layout.activity_supervisor_order_popup)
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        var sendAllButton = findViewById<Button>(R.id.orderAllButton)
        var sendSelectedButton = findViewById<Button>(R.id.orderSelectedButton)
        var cancel = findViewById<Button>(R.id.cancelButton)

        sendAllButton.setOnClickListener(View.OnClickListener { sendAll() })
        sendSelectedButton.setOnClickListener(View.OnClickListener { sendSelected() })
        cancel.setOnClickListener(View.OnClickListener { cancel() })

        popupRecycler.layoutManager = LinearLayoutManager(this)
        addData()
        adapter = RequiredItemAdapter(this, dataListSend)
        popupRecycler.adapter = adapter
    }

    fun sendAll() {
        send(ArrayList(dataListSend))
        this.finish()
    }

    fun sendSelected() {
        var selectedItems = adapter.selected
        send(selectedItems)
        this.finish()
    }

    fun cancel() {
        this.finish()
    }

    fun send(list: ArrayList<RequiredItem>) {
        if (list.size == 0) {
            Toast.makeText(this@OrderPopup, "Please select at least one item.", Toast.LENGTH_LONG).show()
            return
        }

        var notifContent = StringBuilder()
        notifContent.append(titleFromIntent + " order received from CNC")
        var i = 1

        notifContent.append("\n")
        for (item in list) {
            notifContent.append(i.toString() + ") " + item.qty + " quantity of " + item.item)
            notifContent.append("\n")
            i++
        }
        var notifTitle = "New Order"

        var builder = NotificationCompat.Builder(this, NotificationChannel.DEFAULT_CHANNEL_ID)
        builder.setContentTitle(notifTitle)
        builder.setContentText("Order for " + dataListSend.size.toString() + titleFromIntent)
        builder.setSmallIcon(R.drawable.ic_shopping_cart_black_24dp)
        builder.setStyle(NotificationCompat.BigTextStyle().bigText(notifContent))
        var mNotifManager = (this.getSystemService(Context.NOTIFICATION_SERVICE))
        if (mNotifManager is NotificationManager) {
            mNotifManager.notify(999, builder.build())
        }
    }

    fun addData() {
        //This is actually replaced with an API Call to get the data. However, this isn't the performance issue!
        var items = arrayListOf<String>("Paint A","Paint B","Paint C", "Tape", "Socks", "Pants")
        for (i in 1..100) {
            var item = RequiredItem(items.get((0..5).shuffled().first()), (1..10000).shuffled().first().toString())
            dataListSend.add(item)
        }
    }

    class RequiredItemAdapter(internal var context: Context, internal var itemList: List<RequiredItem>) :
        RecyclerView.Adapter<RequiredItemAdapter.CustomViewHolder>() {
        private var imgList: List<RequiredItem> = itemList;
        //        var selected: MutableList<RequiredItem> = ArrayList()
        var selected = ArrayList<RequiredItem>()

        override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
            holder.materialName.setText(imgList.get(position).item)
            holder.materialQty.setText(imgList.get(position).qty)
            var clicked = false

            holder.toggle.setOnClickListener {
                if (clicked) {
                    selected.remove(imgList.get(position))
                    clicked = false
                    holder.toggle.setImageResource(R.drawable.item_order_unchecked)
                    Log.d("LIST", selected.toString())

                } else {
                    selected.add(imgList.get(position))
                    clicked = true
                    holder.toggle.setImageResource(R.drawable.order_item_checked)
                    Log.d("LIST", selected.toString())
                }
            }
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
            val layoutInflater = LayoutInflater.from(context)
            val cellForRow = layoutInflater.inflate(R.layout.card_supervisor_order_item, parent, false)
            return CustomViewHolder(
                cellForRow
            )
        }

        override fun getItemCount(): Int {
            return imgList.size
        }

        class CustomViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            var materialName: TextView
            var materialQty: TextView
            var toggle: ImageView

            init {
                materialName = itemView.findViewById(R.id.materialName)
                materialQty = itemView.findViewById(R.id.materialQty)
                toggle = itemView.findViewById(R.id.addRemove)
            }
        }
    }
}

```
\$\endgroup\$

1 Answer 1

-1
\$\begingroup\$

First sight of layout/activity_supervisor_order_popup.xml: no need having the first LinearLayout at least.

\$\endgroup\$
2
  • 3
    \$\begingroup\$ Welcome to code review! Can you explain what that element isn’t needed? \$\endgroup\$ Jul 19, 2019 at 6:21
  • \$\begingroup\$ The first LinearLayout wraps textView (with id materialsTitle) and the recyclerView. It can be done by using constraintLayout relative positioning \$\endgroup\$
    – Josephine
    Jul 24, 2019 at 5:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.