0

在此先感谢您的时间。我正在使用 RelativeLayout 来显示我的列表项。我有图像按钮左对齐父和图像右侧的一堆 TextViews。只有始终可见的文本视图是ordersummaryrow.itemtitleordersummaryrow.itemprice。我正在根据 CustomAdapter 中的条件设置其余文本视图的可见性。例如,如果提供了特殊请求,那么它应该显示在底部,如果提供了项目选项,那么在请求的顶部,如果提供了额外的项目,那么它应该显示在项目选项的顶部,最后是主项目。另外,如果没有其他文本视图可见,我希望 Main Item Title 居中对齐,如此处所述. 但这不是我通过这种行布局得到的。如果提供了特殊要求,那么这就是我看到的唯一文本视图。它甚至没有显示主要项目标题。我确信它是可行的,我一定错过了一些东西。感谢你的帮助。

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;
    }
4

1 回答 1

0

在所有 if()..else() 语句中,添加 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;
}
于 2012-04-06T02:00:15.077 回答