-1

I am creating dynamic views and adding it to the parent layout in a loop. So, when the data is much bigger, then it hangs the android system while setting the view.

How to stop this ? Or any better way to populate large dynamic data which does not hangs up the device.

Basic operation :

  1. I get a list of data with some format information

  2. Run a loop and create views dynamically based on the switch case for different type of data (text, image, link, etc.) and add that view to the parent layout.

lajeet
  • 437
  • 2
  • 4
  • 22

1 Answers1

0

Please do not use to dynamic adding a lot of UI elements to screen by hand without using RecyclerView. There are two ways how to propagate your data to screen with RecyclerView.

  1. For each data type create UI component with header like class CurrentItem(itemView: View) : RecyclerView.ViewHolder(itemView), DataTypeItem. DataTypeItem is interface for each data type. Than create RecyclerView and to in the adapter RecyclerView.Adapter<DataTypeItem>. Now your adapter is able to show all your data types with interface DataTypeItem. In adapter override method onBindViewHolder(holder: DataTypeItem, position: Int) and re-type holder to CurrentItem. On the end propagate your incoming data to UI component.
  2. Create layouts for your data only without UI components. Here is a nice example, how to do it.
  • On use recyclerView to populate the dynamic views, onScroll of the recyclerView, the data item order changes. How to prevent that from happening ? @dzejkob23 – lajeet Sep 18 '19 at 08:56
  • If your incoming data order has changed then you can create a map with indexes. These indexes are items positions on the screen in adapter. If you change all items call `notifyDataSetChanged`, only items range `notifyItemRangeChanged`, only one item `notifyItemChanged` or `notifyItemMoved`. By these methods you are able to propagate changes the screen. – dzejkob23 Sep 20 '19 at 08:07