1

Suppose I have a CardView which contains 2 TextView, and I need to display 10 CardView with same layout as 10 times one below the other

These 10 will always be fixed, it will never be more or less than 10 in the future.

What would be a better approach among the 2 given below (in terms of performance)?

1st Approach :

Type the code for all the 10 CardView in xml 10 times and set their individual TextView's value in Java Code

2nd approach :

Type the code for only one CardView and use it in a "ListView" or "RecyclerView" to display it 10 times in Java code

Or can you suggest any other approach?

Pang
  • 9,564
  • 146
  • 81
  • 122
Avtar Singh
  • 261
  • 8
  • 14

2 Answers2

1

For me the second approach is better.

You just need to use View holder to optimize user experience.

The ViewHolder pattern is a pattern that a developer can use to increase the speed at which their ListView renders data. The reason for this improvement is that the number of times which the findViewById method is invoked is drastically reduced, existing views do not have to be garbage collected and new views do not have to be inflated.

Check these links to understand more about view holder and its usefulness

Android Developer Documentation -Making ListView Scrolling Smooth

Optimizing Your Listview

Android ViewHolder Pattern

Android RecyclerView

Cristiana Chavez
  • 11,349
  • 5
  • 55
  • 54
  • @Christiana214 Thank You for giving explanation and sharing the links. That was really helpful in increasing the performance – Avtar Singh Jul 24 '15 at 10:44
  • Your Welcome Avtar, oh I also read in the comment below that you're having problem in displaying different number of textview in each listItem. – Cristiana Chavez Jul 24 '15 at 10:59
  • 1
    You can achieved it dynamically by creating a gridview inside the listview, while the gridview will inflate the textview , you will also use view holder in this. I created an illustration http://imgur.com/kHQp4Uf . – Cristiana Chavez Jul 24 '15 at 11:10
  • This is what I have to do, http://imgur.com/AT89C7q , the outer black is "recyclerView",...the red one is "cardView",..the Blue one is another "cardView" (containing 2 textviews always, it will always remain 2 texitviews),...now the outer "Black-recyclerView" may contain near about 15 or 20 "Red-cardView"......But the main problem I am having is the following one...the "Red-cardView" can sometime contain "1 blue-cardView" or "2 blue-cardView" or "3 blue-cardView"...Now tell me how to do this...Please tell in detail explanation as I am not used to RecyclerView and its Adapter – Avtar Singh Jul 24 '15 at 11:27
  • as what I understand cardview is like a layout container that you can put views like textview. so in order to achieved what you want .the number of textviews in a cardview is fixed 2 , but number of cardview is nth. you just need listview -that inflates Gridview, Gridview will inflate Cardview inside it is 2 textview. Baseadapter inside baseadapter. – Cristiana Chavez Jul 24 '15 at 12:17
  • I will send you sample code next monday. Same as my image illustion but the textview in there will be cardview. – Cristiana Chavez Jul 24 '15 at 12:17
  • Yes almost same as your image, I tried but was not able to have that nested thing, try it, If you get anything working then tell me, Anyways, Thank You for all – Avtar Singh Jul 24 '15 at 12:23
  • I made it worked before, gonna show the code next time. as of now I gotta go . – Cristiana Chavez Jul 24 '15 at 12:25
  • 1
    this is the sample check it out https://github.com/cristiana214/NestedAdapterView – Cristiana Chavez Jul 27 '15 at 10:53
  • @AvtarSingh what happen next? – Cristiana Chavez Jul 29 '15 at 07:36
  • I was Off for 2 days, Now Testing your Sample Code, Just give me some time – Avtar Singh Jul 29 '15 at 08:14
  • Hey @Cristiana214 This is Exactly what I was trying to do, Perfect, Thank You Very Very Much (Y) – Avtar Singh Jul 29 '15 at 08:28
  • Great! :-D i'm glad it helps. – Cristiana Chavez Jul 29 '15 at 08:40
  • Hey @Cristiana214 Can you help me in creating the layout similar to this Video, the coordinator layout and then the transparent toolbar on collapsing it...its just not happening...I don't know exactly how to do it...please help...https://dl.pushbulletusercontent.com/rezXzeTxwRdGxvi9fLJUOw4VKccxjNue/2015_09_14_23_37_58.mp4 – Avtar Singh Sep 14 '15 at 18:29
0

Second approach more flexible. In terms performance and especially in terms of code management in future.

Gunaseelan
  • 14,415
  • 11
  • 80
  • 128
  • Yeah I too thought that 2nd Approach will be more Flexible in terms of code management. But I think that it would be slightly slow. May be, I am not sure. Just a bit Confused. What you Say ? – Avtar Singh Jul 24 '15 at 04:29
  • If it was 10 rows only means it will not affect that much in performance. And also you have two `textview`s only per rows. So don't worry about performance here. – Gunaseelan Jul 24 '15 at 04:35
  • 1
    Yes.. 2nd approach is optimal in terms of `layout rendering` and `memory`.. It is also open for expansion if in case requirement changes in future.. – Kushal Jul 24 '15 at 04:39
  • @Kushal Yeah you are right that "It is also open for expansion if in case requirement changes in future" , But, why are you saying that it is optimal in terms of "layout rendering" and "memory" , I was thinking that there will be alight delay on loading that activity because "ListView" will be creating 10 layouts. Can you clarify my doubt in detail why it would be more optimal to use 2nd Approach PLEASE – Avtar Singh Jul 24 '15 at 04:49
  • 2
    `RecyclerView` and `ListView` re-uses views when user scrolls across.. Further, `RecyclerView` is optimal `ListView` provided by Android.. so it is optimal in terms of layout rendering because of re-use using `getView()` callback.. even more optimal if we use `ViewHolder` inside `getView()`.. – Kushal Jul 24 '15 at 05:02
  • @Kushal Thank You. Just one more thing. I got another problem just now. Suppose a CardView will contain 2 textviews, and 2nd cardview may contain 1 textview or more and next cardview may contain 4 textviews, so how to tackle that, but notice that number of total cardview will always be 10. Please help in how to tackle this thing.....should I manually write all cardview with different layout in xml or you have some other trick, but performance should not be degraded – Avtar Singh Jul 24 '15 at 05:16
  • Have a look at [here](http://stackoverflow.com/questions/4394293/create-a-new-textview-programmatically-then-display-it-below-another-textview) and [here](https://www.google.co.in/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=create%20textview%20programmatically%20android) @AvtarSingh – Gunaseelan Jul 24 '15 at 05:22
  • @Gunaseelan I did that, and it is working fine, but i dont wanna use this approach as I think that Creating textviews at runtime is not really preferred. It will be slow , I think – Avtar Singh Jul 24 '15 at 05:25
  • @AvtarSingh How did you say it will be slow... If you add it by xml work we have to inflate xml, then have find our textview as integer, then have to convert it as textview, then only we can set text or do operations on that textview. – Gunaseelan Jul 24 '15 at 05:34
  • If you only see 2 or 3 CardViews on screen at a time, RecyclerView will be faster to render. – BladeCoder Jul 24 '15 at 10:32