Reputation: 1044
i've been working on a simple UI which displays a few photos and some other info! I have a simple layout for displaying each photo in a box and I wanted to add this layout dinamicaly multiple times (the number of photos I have).
This is my info_wrapper.xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/info_wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:paddingLeft="1dp"
android:paddingRight="1dp"
>
<TextView
android:id="@+id/usernamePosting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"/>
<ImageView
android:below="@+id/usernamePosting"
android:id="@+id/photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"/>
</RelativeLayout>
And my main_screen.xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<RelativeLayout
android:id="@+id/menuBar"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:background="@color/green">
<ImageView
android:id="@+id/locationIcon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="@drawable/location_place"
></ImageView>
<TextView
android:id="@+id/userLocationTxtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_toRightOf="@+id/locationIcon"
android:text="@string/app_name"
android:textColor="@color/white"
android:textSize="15dp" />
</RelativeLayout>
<RelativeLayout
android:layout_below="@id/menuBar"
android:id="@+id/bodyView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/light_gray">
</RelativeLayout>
</RelativeLayout>
Right now when I try to add multiple times the info_wrapper view - all the view ar on top of each other - but I want to be one below other
Upvotes: 1
Views: 907
Reputation: 1933
Just use a ListView
with your own implementation of e.g. BaseAdapter
or ArrayAdapter
.
Have a look at getItemViewType and getViewTypeCount.
If you have two different types of views, have getViewTypeCount
return 2
and getItemViewType(position)
either 0
or 1
depending on which element is at position
.
You could for example have a custom Adapter implementation which inflates/reuses different views for different types of objects:
class ExampleAdapter extends BaseAdapter
{
[...]
View getView(int position, View convertView, ViewGroup parent)
{
Object o = getItemFromSomeDataSource(position);
if(o instanceof Type1)
{
if(convertView == null)
{
convertView = inflater.inflate(item_layout_1);
[...]
}
else
{
[...] // reuse existing View
}
}
else if(o instanceof Type2)
{
[...]
}
[...]
}
int getViewTypeCount(){ return n; } // where n = no. of different types to display
int getItemViewType (int position){ [...] } // mapping of item-position and item-type
}
Upvotes: 2