2013-03-04 34 views
1

我一直在研究一個簡單的UI,其中顯示一些照片和其他信息! 我有一個簡單的佈局來顯示每個照片在一個盒子裏,我想多次添加這個佈局dinamicaly(我有照片的數量)。使用LayoutInflater將相同視圖多次添加到彼此的下方

這是我info_wrapper.xml文件

<?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> 

而且我main_screen.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> 

現在,當我嘗試添加多次info_wrapper視圖 - 對所有視圖AR彼此頂部 - 但我想成爲一個低於其他

+0

什麼是它應該是什麼樣子的?考慮使用'LinearLayout'(或'ListView'和'ListAdapter') – Tedil 2013-03-04 20:19:14

+0

它看起來像一個listview - 但在列表視圖中的一些行之間,我有時必須添加其他信息...所以我不認爲我可以使用!像這樣的東西 - http://stackoverflow.com/questions/13955122/android-foursquare-display – Alin 2013-03-04 20:26:09

+0

以及你*可以*使用一個自定義適配器的列表視圖,膨脹不同類型的數據項不同的佈局 – Tedil 2013-03-04 20:37:01

回答

2

只需使用ListView與您自己的實施例如BaseAdapterArrayAdapter

看看getItemViewTypegetViewTypeCount。 如果您有兩種不同類型的視圖,請getViewTypeCount返回2getItemViewType(position)或者01,具體取決於哪個元素位於position

例如,你可以有一個自定義的適配器實現其膨脹/重複使用於不同類型的對象的不同意見:

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 
} 
+0

謝謝 - 這似乎是最好的方法! – Alin 2013-03-09 12:02:26

相關問題