2

I'm currently writting an android application and I'd like it to be well written/designed.

Right now I have a set of multiples views (2 images views, 3 textviews) in a relative layout that I use pretty often. Is there any way to create a custom view that regroup them all?

I've took this screenshot to explain what I've done right now:

enter image description here

As you can see, right now I've just copied/pasted my framelayout which contain all my views... Is this the proper way to do it or there's a cleaner solution?

Thanks guys

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
Shinnyx
  • 2,144
  • 2
  • 14
  • 21

2 Answers2

2

No a better way would be to put all the content you putting in you layouts in a ArrayList of objects. and then create an ArrayAdapter to populate a ListView from this objects the way you want.

The advantage of this technic is that you gain a Views recycle mechanism that will recycle the Views inside you ListView in order to spend less memory.

In Short you would have to:

1. Create an object that represents your data for a single row.

2. Create an ArrayList of those objects.

3. Create a layout that contains a ListView or add a ListView to you main layout using code.

4. Create a layout of a single row (you already have it).

5. Create a ViewHolder that will represent the visual aspect of you data row from the stand point of Views.

6. Create a custom ArrayAdapter that will populate the rows according to you needs.

7. Finally assign this ArrayAdapter to your ListView in onCreate.

You can get an Idea of how to implement this by reading this blog post I wrote:

Create a Custom ArrayAdapter

Emil Adz
  • 40,709
  • 36
  • 140
  • 187
  • Thanks for your anwser Emil! It exactly represent what I wanna do. One question thought, I get the general idea of your code. But in the first step you suggest me to create an object (RowData I suppose) which contain my "data" for my row. Exactly, in my situation... What is this data? My text strings & my image path source? If I get it right, I should create a constructor that put the correct strings/imagepath for each object (which represent a different row?) – Shinnyx Apr 10 '13 at 16:51
  • Your RowData object is exactly what you think it is. all the data related to the content you want to display in one row. on most cases it will be Strings,ints and FilePath or Url's for Images, but it can be even a Color object if you want to specify color for each row and use it dynamically in the ArrayAdapter, it can even be an ArrayList of other items that will be presented in another list when you click on the row. – Emil Adz Apr 10 '13 at 16:56
  • I see, thanks! Last question, as your last sentence opened my eye on another problem... When I hit the row, it pushes a new xml layout via fragmentManager (that xml layout always has the same form with 6 textviews) ... I guess I did the same mistake twice (I copied/pasted my xml layout with theses 6 textviews and changed their contain directly in the XML using strings reference)... the layout contain isn't programmatically coded here! I guess I'll have to create another arraylist of a new object which will hold my contain of theses 6 textviews, right? Thanks for your time, really appreciated! – Shinnyx Apr 10 '13 at 17:12
  • I can't get a grip of the layout you describe, do you mind to show a screen shot? – Emil Adz Apr 10 '13 at 17:21
  • http://img42.imageshack.us/img42/5625/stackk.png, There! Made my best to make it clear! EDIT: I'm pretty sure I can do only ONE layout and assign different content by code! Like you told me to do in my listview! – Shinnyx Apr 10 '13 at 17:41
  • in this case if the number of TextView is only 6 (I see 8 in the pic) and it's not going to grow, then there is no need to bother and create an adapter. adapters are designed to show a large amount of view and recycle them. you can leave this layout as it is. – Emil Adz Apr 10 '13 at 17:44
  • I see, well I'm gonna keep it that way then! I thought I could get rid of the extra XML files (since they all have the same view structure) and keep only one "master" XML which I could use to dynamically change the content of it depending on which row was clicked... But well, if it's ok to keep all theses files I won't go further, then! :) Thanks Emil! – Shinnyx Apr 10 '13 at 17:56
0

If you do it in code instead of the interface builder you can use LayoutInflator to create a view from your xml each time, and then add those views as subviews to your LinearLayout. This would also allow you to vary the number without having to copy paste each new view.

LayoutInflater layoutInflator = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout ll = (LinearLayout) findViewById(R.id.yourLayout);
for(int i=0; i<numOfSubViews; i++){
    View view = layoutInflator.inflate(R.layout.yourFrameLayout, null);
    ll.add(view);
}
zimmryan
  • 1,099
  • 10
  • 19