2013-04-26 18 views
0

我有一個ListView有一個自定義適配器,以提供定製的View。每個列表項「改變」它所在屏幕的一側(就好像你將它翻轉過一個垂直軸)。下面是我在談論的一個例子:如何從屏幕右側開始將`ListView`項目轉換爲wrap_content?

enter image description here

的暗照片中的灰色框覆蓋了ProfilePictureView和包含用戶的姓名和照片TextView。如果用戶未登錄,則無名稱和照片(如照片中的第二個列表項所示)。正如您從照片中看到的那樣,背景圖像環繞視圖中第一個和第三個項目的內容(從左側開始到右側結束)。 問題是:面向另一種方式的項目忽略了我的「wrap_content」調用,並且與父視圖「匹配」(如您在項目二中所見)。我相信它正確地包裝了內容,但有什麼方法可以從屏幕右側開始並將內容包裝到左側?

下面是該項目XML代碼視圖:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:custom="http://schemas.android.com/apk/res-auto" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="right|center_vertical"> 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:layout_alignParentRight="true" 
    android:gravity="right" > 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <com.namespace.RobotoTextView 
      android:id="@+id/list_item_user_name" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="24sp" 
      android:textStyle="bold" 
      android:textColor="@color/white" 
      custom:typeface="roboto_light"/> 

     <com.namespace.RobotoTextView 
      android:id="@+id/list_item_user_score" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="36sp" 
      android:textStyle="bold" 
      android:textColor="@color/waf_warm_yellow_orange" 
      custom:typeface="roboto_bold"/> 

    </LinearLayout> 

    <com.facebook.widget.ProfilePictureView 
      android:id="@+id/list_item_user_image" 
      android:layout_width="69dp" 
      android:layout_height="69dp" 
      android:layout_margin="10dp" /> 

</LinearLayout> 

所以,我怎麼能正確對齊的觀點的權利和有背景圖像圍繞其內容的正確包裝?

回答

1

爲左側和右側創建單獨的列表項目佈局。在您的適配器...

@Override 
public int getViewTypeCount() { 
    return 2; 
} 

@Override 
public int getItemViewType(int position) { 
    return position % 2; 
} 

public View getView(int position, View convertView, ViewGroup parent) { 
    int viewType = getItemViewType(position); 
    int layoutRes = viewType == 0 ? R.layout.list_item_left : R.layout.list_item_right; 

    View row = inflater.inflate(layoutRes); 
    /* ... */ 
} 

基本上,你的適配器報告兩種不同的視圖類型和它們之間的交替getView。 (如果還有其他邏輯來確定左側和右側,則在getItemViewType()中執行它。)

0

我的問題的答案非常簡單,在我的自定義適配器內我設置背景圖像資源取決於它是否是偶數或奇怪的listview項目。而不是這樣做,我只需要製作一個imageview並將該圖像資源設置爲背景圖像,並將其設置爲android:layout_alignParentRight="true和`android:layout_alignParentLeft =「true」屬性在屏幕的正確一側。

相關問題