简体   繁体   中英

Android: how to create this kind of dynamic layout?

My aim is to display people/addresses in a list - but not just a simple list, rather a list of boxes that contain several information, in my case a number, the address and a type:

在此处输入图片说明

The data structure for this are POJOs:

  • MyPojo.java (Class)
  • + getNumer()
  • + getType()
  • + getName()
  • + getStreet()
  • + getCity()

Of course, there are more than 4 entries which means that there should be a scrollbar. My first idea: a Scrollview that contains a Linear Layout . But I'm not sure about the content, means the boxes you can see above.

  1. How can I define such boxes that represent the list items? As you can see above, a box consists of three parts that are divided from each other (Number, Type, Address).
  2. Of course, it must be possible to add and remove boxes (means: entries) dynamically at runtime to the Linear Layout.

Thank you

My first idea: a Scrollview that contains a Linear Layout.

Are you familiar with a listView ? This already exists to do exactly that. I'm sure you can find dozens of tutorials on how to use them by searching.

  • Create a ListActivity class
  • Create a separate layout file for your item

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" > <LinearLayout android:orientation="vertical" > <TextView /> <TextView /> </LinearLayout> </TextView> </LinearLayout>

  • Then Create an inner class in ListActivity which extends from ArrayAdapter . In the getView() method, use LayoutInflater to inflate yourItemLayout and update the TextView based on the position values.
  • From onCreate in the main class, call setListAdapter(new InnerClass(this,R.layout.yourItemLayout,list)

You can refer customArrayAdapter to know how to custom ArrayAdapter works.

Done and dusted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM