Android 使用LayoutInflater将同一视图添加到彼此下方多次
我一直在开发一个简单的UI,它可以显示一些照片和其他信息! 我有一个简单的布局,可以在一个框中显示每张照片,我想将这个布局添加多次(我拥有的照片数量) 这是我的info_wrapper.xml文件Android 使用LayoutInflater将同一视图添加到彼此下方多次,android,layout,user-interface,layout-inflater,Android,Layout,User Interface,Layout Inflater,我一直在开发一个简单的UI,它可以显示一些照片和其他信息! 我有一个简单的布局,可以在一个框中显示每张照片,我想将这个布局添加多次(我拥有的照片数量) 这是我的info_wrapper.xml文件 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/info_w
<?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>
和我的主屏幕.xml文件:
<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>
现在,当我尝试多次添加信息包装视图时—所有视图都在彼此之上—但我希望是一个低于另一个的视图只需使用
列表视图
,并使用自己的实现,例如BaseAdapter
或ArrayAdapter
看一看,看一看。
如果您有两种不同类型的视图,请使用getViewTypeCount
return2
和getItemViewType(position)
或者0
或者1
,具体取决于哪个元素位于位置
例如,您可以有一个自定义适配器实现,它为不同类型的对象扩展/重用不同的视图:
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
}
它应该是什么样子的?考虑使用<代码>线性布局< /代码>(或<代码> ListVIEW < /代码>使用<代码> ListAdvult<代码>),它看起来像一个ListVIEW——但是在ListVIEW中的一些行之间,有时必须添加其他信息…所以我不认为我能用!类似这样的东西-你可以使用一个带有自定义适配器的listview,为不同类型的数据项扩展不同的布局。你能给我一个例子吗-一个链接或者我可以仔细看看的东西?谢谢谢谢-这似乎是最好的方法!