2012-04-05 98 views
0

預先感謝您的時間。 我正在使用RelativeLayout來顯示我的列表項目。我有圖像按鈕左對齊與圖像右邊的父母和一堆TextViews。 只有文本視圖永遠是可見的ordersummaryrow.itemtitleordersummaryrow.itemprice。 根據CustomAdapter中的條件設置其餘文本視圖的可見性。 例如,如果提供了特殊請求,那麼它應該顯示在底部,如果提供了item選項,那麼在請求之上,如果提供了額外的item,那麼在item選項的頂部,最後是main item。 另外,如果其他文本視圖都不可見,如主題here所示,我還希望主要項目標題集中對齊。 但這不是我用這種行佈局得到的。如果提供特殊請求,那就是我所看到的唯一的textview。它甚至沒有顯示主項目標題。 我相信這是可行的,我必須錯過一些東西。 感謝您的幫助。RelativeLayout的屬性alignWithParentIfMissing沒有給出預期的結果

ordersummaryrow.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/listitem_background" 
    > 
<LinearLayout 
    android:id="@+id/ordersummaryrow.buttons" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:layout_marginRight="5dp"> 
<ImageView 
    android:id="@+id/ordersummaryrow.delete" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/orderdelete" 
    android:layout_weight="1" /> 
<ImageView 
    android:id="@+id/ordersummaryrow.update" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/orderedit" 
    android:layout_weight="1" /> 
</LinearLayout> 
<TextView 
    android:id="@+id/ordersummaryrow.splrequest" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textColor="@color/custom_black_color" 
    android:layout_toRightOf="@id/ordersummaryrow.buttons" 
    android:layout_alignParentBottom="true" /> 
<TextView 
    android:id="@+id/ordersummaryrow.itemoption" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textColor="@color/custom_black_color" 
    android:text="Item Option" 
    android:layout_toRightOf="@id/ordersummaryrow.buttons" 
    android:layout_above="@id/ordersummaryrow.splrequest" 
    android:layout_alignWithParentIfMissing="true" 
    /> 
<TextView 
    android:id="@+id/ordersummaryrow.extraitem" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textColor="@color/custom_black_color" 
    android:text="Extra Item Title" 
    android:layout_toRightOf="@id/ordersummaryrow.buttons" 
    android:layout_above="@id/ordersummaryrow.itemoption" 
    android:layout_alignWithParentIfMissing="true" 
    /> 
<TextView 
    android:id="@+id/ordersummaryrow.extraitemprice" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textColor="@color/custom_black_color" 
    android:text="$" 
    android:layout_above="@id/ordersummaryrow.itemoption" 
    android:layout_alignParentRight="true" 
    android:layout_alignWithParentIfMissing="true" 
    /> 

<TextView 
    android:id="@+id/ordersummaryrow.itemtitle" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textColor="@color/custom_black_color" 
    android:layout_above="@id/ordersummaryrow.extraitem" 
    android:layout_toRightOf="@id/ordersummaryrow.buttons" 
    android:layout_alignWithParentIfMissing="true" 
    android:gravity="center_vertical" 
    android:text="Item Title" /> 

<TextView 
    android:id="@+id/ordersummaryrow.itemprice" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textColor="@color/custom_black_color" 
    android:text="$$" 
    android:layout_alignTop="@id/ordersummaryrow.itemtitle" 
    android:layout_alignParentRight="true" 
    /> 
</RelativeLayout> 

這裏是GetView()的自定義適配器

@Override 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 
     View row = convertView; 
     if(row == null) 
     { 
      LayoutInflater vi = (LayoutInflater)m_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      try{ 
       row = vi.inflate(R.layout.ordersummaryrow, null); 
      } 
      catch(Exception e) 
      { 
       Log.i("SUBMENU", e.getMessage()); 
      } 
     } 
     OrderSummaryRowViewHolder holder = (OrderSummaryRowViewHolder)row.getTag(); 
     if(holder == null) 
     { 
      holder = new OrderSummaryRowViewHolder(row); 
      row.setTag(holder); 
     } 
     OrderedItem orderItem = m_orderedItem.get(position); 
     if (orderItem != null) 
     { 
      if(m_updateListener != null) 
       holder.m_updateBtn.setOnClickListener(m_updateListener); 
      else 
       Log.i("OrderSummaryRowAdapter", "Update Listener is not registered"); 

      if(m_deleteListener != null) 
       holder.m_deleteBtn.setOnClickListener(m_deleteListener); 
      else 
       Log.i("OrderSummaryRowAdapter", "Delete Listener is not registered"); 

      holder.m_titleTxt.setText(orderItem.get_orderItem().getName()); 
      holder.m_priceTxt.setText("$" + (String.valueOf(orderItem.get_orderItem().getPrice()))); 
      if(orderItem.get_extraItems().size() == 0) //No extra items added so hide corresponding text box 
      { 
       holder.m_extraItemTxt.setVisibility(View.GONE); 
       holder.m_extraItemPriceTxt.setVisibility(View.GONE); 
      } 
      else 
      { 
       StringBuilder sbItemName = new StringBuilder(); 
       StringBuilder sbItemPrice = new StringBuilder(); 
       for (SubMenu extraItem : orderItem.get_extraItems()) 
       { 
        sbItemName.append(extraItem.getName()); 
        sbItemName.append("\n"); 
        sbItemPrice.append("$" + (String.valueOf(extraItem.getPrice()))); 
        sbItemPrice.append("\n"); 
       } 
       holder.m_extraItemTxt.setText(sbItemName); 
       holder.m_extraItemPriceTxt.setText(sbItemPrice); 
      } 

      if(orderItem.get_options().size() == 0) 
       holder.m_optionTxt.setVisibility(View.GONE); 
      else 
      { 
       StringBuilder sbOption = new StringBuilder(); 
       for (ItemOption option : orderItem.get_options()) 
       { 
        sbOption.append(option.getName()); 
        sbOption.append("\n"); 
       } 
       holder.m_optionTxt.setText(sbOption); 
      } 

      if(orderItem.getspecialRequest() != null && 
        orderItem.getspecialRequest().length() > 0) 
       holder.m_splRequestTxt.setText(orderItem.getspecialRequest()); 
      else 
       holder.m_splRequestTxt.setVisibility(View.GONE); 

     } 
     return row; 
    } 
+0

是關於用戶界面?如果是的話,誰在乎getView;)?它令人困惑......整篇文章有點難以置信,如果你描述你要完成的是什麼樣的佈局,我想你會得到一個快速的答案,例如用油漆畫一個骯髒的位圖 – 2012-04-05 22:46:43

回答

0

在你所有的,如果()..其他()語句,添加setVisibility(View.VISIBLE)聲明。對於例如,

if(orderItem.getspecialRequest() != null && 
       orderItem.getspecialRequest().length() > 0){ 
      holder.m_splRequestTxt.setVisibility(View.VISIBLE); 
      holder.m_splRequestTxt.setText(orderItem.getspecialRequest()); 
     }else{ 
      holder.m_splRequestTxt.setVisibility(View.GONE); 
     } 

同樣,對於我們的情況下,所有的休息。

更新

請試試這個代碼,並檢查是否正常工作

@Override 
public View getView(int position, View convertView, ViewGroup parent) 
{ 
    View row = convertView; 
    if(row == null) 
    { 
     LayoutInflater vi = (LayoutInflater)m_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     try{ 
      row = vi.inflate(R.layout.ordersummaryrow, null); 
     } 
     catch(Exception e) 
     { 
      Log.i("SUBMENU", e.getMessage()); 
     } 
    } 
    OrderSummaryRowViewHolder holder = (OrderSummaryRowViewHolder)row.getTag(); 
    if(holder == null) 
    { 
     holder = new OrderSummaryRowViewHolder(row); 
     row.setTag(holder); 
    } 
    OrderedItem orderItem = m_orderedItem.get(position); 
    if (orderItem != null) 
    { 
     if(m_updateListener != null) 
      holder.m_updateBtn.setOnClickListener(m_updateListener); 
     else 
      Log.i("OrderSummaryRowAdapter", "Update Listener is not registered"); 

     if(m_deleteListener != null) 
      holder.m_deleteBtn.setOnClickListener(m_deleteListener); 
     else 
      Log.i("OrderSummaryRowAdapter", "Delete Listener is not registered"); 

     if(orderItem.getspecialRequest() != null && 
       orderItem.getspecialRequest().length() > 0) 
      holder.m_splRequestTxt.setText(orderItem.getspecialRequest()); 
     else 
      holder.m_splRequestTxt.setVisibility(View.GONE); 

     if(orderItem.get_options().size() == 0) 
      holder.m_optionTxt.setVisibility(View.GONE); 
     else 
     { 
      StringBuilder sbOption = new StringBuilder(); 
      for (ItemOption option : orderItem.get_options()) 
      { 
       sbOption.append(option.getName()); 
       sbOption.append("\n"); 
      } 
      holder.m_optionTxt.setText(sbOption); 
     } 

     if(orderItem.get_extraItems().size() == 0) //No extra items added so hide corresponding text box 
     { 
      holder.m_extraItemTxt.setVisibility(View.GONE); 
      holder.m_extraItemPriceTxt.setVisibility(View.GONE); 
     } 
     else 
     { 
      StringBuilder sbItemName = new StringBuilder(); 
      StringBuilder sbItemPrice = new StringBuilder(); 
      for (SubMenu extraItem : orderItem.get_extraItems()) 
      { 
       sbItemName.append(extraItem.getName()); 
       sbItemName.append("\n"); 
       sbItemPrice.append("$" + (String.valueOf(extraItem.getPrice()))); 
       sbItemPrice.append("\n"); 
      } 
      holder.m_extraItemTxt.setText(sbItemName); 
      holder.m_extraItemPriceTxt.setText(sbItemPrice); 
     } 

     holder.m_titleTxt.setText(orderItem.get_orderItem().getName()); 
     holder.m_priceTxt.setText("$" + (String.valueOf(orderItem.get_orderItem().getPrice()))); 

    } 
    return row; 
} 
+0

在佈局中默認是可見的。在GetView中,我只是根據條件隱藏它。儘管如此,我還是通過在代碼中將可見性設置爲可見。它沒有工作。 – 2012-04-06 07:35:49

+0

Plz檢查代碼,看看它是否工作 – Shubhayu 2012-04-06 08:05:06

+0

我沒有看到任何邏輯原因翻轉代碼(即首先初始化spl請求和標題最後)。雖然出於好奇,我嘗試了,仍然沒有解決問題。 – 2012-04-09 05:47:08

相關問題