动态获取(设置)listView行高

时间:2012-11-26 15:06:30

标签: android listview android-arrayadapter row-height

我有listView,其中每一行看起来都是这样的:

<?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:orientation="horizontal"
    android:padding="5dip" 
    android:background="#FFFFFF">
    <LinearLayout
        android:id="@+id/thumbnail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="5dip"
        android:padding="3dip" >

        <TextView
            android:id="@+id/altOrderId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ID"
            android:gravity="center|center"
            android:textColor="#040404"
            android:textSize="15dp"
            android:textStyle="bold"
            android:typeface="sans" />
    </LinearLayout>

    <TextView
        android:id="@+id/altOrderTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="1dip"
        android:textStyle="bold"
        android:layout_toRightOf="@+id/thumbnail"
        android:text="Just gona stand there and ..."
        android:textColor="#000000"
        android:textSize="14dp" />

    <TextView
        android:id="@+id/altOrderStatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/altOrderTitle"
        android:layout_alignParentBottom="true"
        android:layout_marginRight="5dip"
        android:layout_toRightOf="@+id/thumbnail"
        android:textColor="#838B8B"
        android:textSize="14dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/altOrderPrice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/thumbnail"
        android:text="Rihanna Love the way lie"
        android:textColor="#458B74"
        android:textSize="15dp"
        android:textStyle="bold"
        android:typeface="sans" />

</RelativeLayout>

我的适配器类是:

public class OrderAdapter extends ArrayAdapter<Order>{


    Context context; 
    int layoutResourceId;    
    public static int rowHeight = 0;
    List<Order> orders = null;
    private Activity activity;
    public OrderAdapter(Context context,  int layoutResourceId,List<Order> orders) {
        super(context, layoutResourceId, orders);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.orders = orders;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        OrderHolder holder = null;

        if(row == null)
        {


            LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.dash_alt_item, parent, false);
            row.setBackgroundColor(Color.WHITE);
            //row.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,30));
            holder = new OrderHolder();
            holder.orderId = (TextView)row.findViewById(R.id.altOrderId);
            holder.orderTitle = (TextView)row.findViewById(R.id.altOrderTitle);
            holder.orderStatus = (TextView)row.findViewById(R.id.altOrderStatus);
            holder.orderPrice = (TextView)row.findViewById(R.id.altOrderPrice);

            row.setTag(holder);
        }
        else
        {
            holder = (OrderHolder)row.getTag();
        }

        Order order = orders.get(position);
        holder.orderId.setText(Integer.toString(order.getOrderid()));
        holder.orderTitle.setText(order.getTitle());
        holder.orderStatus.setText(order.getProcess_status().getProccessStatusTitle());
        holder.orderPrice.setText(Float.toString(order.getPrice()));

        return row;
    }

    static class OrderHolder
    {
        TextView orderId;
        TextView orderTitle;
        TextView orderStatus;
        TextView orderPrice;
    }
}

我需要检测(或设置)每个listview行的高度,并将相应的项目数量显示到设备屏幕高度。我该如何实施呢?

1 个答案:

答案 0 :(得分:0)

使用gridview而不是listview,并设置此android:numColumns="auto_fit" >

<GridView
        android:id="@+id/gridView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:numColumns="auto_fit" >
    </GridView>

获取屏幕高度

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
相关问题