2012-04-05 83 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

相关问题