Horizo​​ntalListView不起作用

时间:2013-02-16 19:28:29

标签: android android-adapterview

我的应用中需要HorizontalListView,因为Android SDK中没有这样的小部件,我使用this one而不是自己构建一个。但问题是列表项没有保持其大小并占据屏幕的整个宽度。

这是列表项的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="106dp"
    android:layout_height="140dp"
    android:background="@drawable/list_bck" >

    <CheckBox
        android:id="@+id/mail_marker"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="4dp"
        android:layout_marginRight="5dp" />

    <View
        android:id="@+id/h_rule1"
        android:layout_width="100dp"
        android:layout_height="1dp"
        android:layout_above="@+id/mail_marker"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="2dp"
        android:background="#CDC1C5" />

    <TextView
        android:id="@+id/date_txt"
        android:layout_width="40dp"
        android:layout_height="15dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp"
        android:text="FEB 4"
        android:textSize="11dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/time_txt"
        android:layout_width="50dp"
        android:layout_height="12dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/date_txt"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="2dp"
        android:text="12:34 PM"
        android:textSize="9dp" />

    <ImageButton
        android:id="@+id/flag_mail"
        android:layout_width="22dp"
        android:layout_height="22dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="5dp"
        android:layout_marginTop="4dp"
        android:background="@android:color/transparent"
        android:scaleType="fitXY"
        android:src="@drawable/star" />

    <ImageView
        android:id="@+id/read_unread"
        android:layout_width="22dp"
        android:layout_height="22dp"
        android:layout_alignParentTop="true"
        android:layout_marginRight="2dp"
        android:layout_marginTop="4dp"
        android:layout_toLeftOf="@+id/flag_mail"
        android:background="@android:color/transparent"
        android:scaleType="fitXY"
        android:src="@drawable/mail_unread" />

    <TextView
        android:id="@+id/count_txt"
        android:layout_width="12dp"
        android:layout_height="12dp"
        android:layout_alignParentTop="true"
        android:layout_marginRight="1dp"
        android:layout_marginTop="10dp"
        android:layout_toLeftOf="@+id/read_unread"
        android:text="3"
        android:textSize="8dp"
        android:textStyle="bold" />

    <View
        android:id="@+id/h_rule"
        android:layout_width="95dp"
        android:layout_height="1dp"
        android:layout_below="@+id/time_txt"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="2dp"
        android:background="#CDC1C5" />

    <TextView
        android:id="@+id/sub_txt"
        android:layout_width="95dp"
        android:layout_height="16dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/h_rule"
        android:layout_marginLeft="6dp"
        android:layout_marginTop="2dp"
        android:ellipsize="middle"
        android:singleLine="true"
        android:text="Subject Line goes here"
        android:textSize="13dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/sender_txt"
        android:layout_width="92dp"
        android:layout_height="15dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/sub_txt"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="4dp"
        android:layout_marginTop="2dp"
        android:ellipsize="middle"
        android:singleLine="true"
        android:text="Name of sender"
        android:textSize="11dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/mail_txt"
        android:layout_width="92dp"
        android:layout_height="24dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/sender_txt"
        android:layout_marginLeft="6dp"
        android:layout_marginRight="4dp"
        android:layout_marginTop="2dp"
        android:ellipsize="middle"
        android:maxLines="2"
        android:text="This is the body of the mail."
        android:textSize="9dp" />

</RelativeLayout>

我看到fill_parentwrap_content没有任何效果但没有运气后仍使用绝对尺码,但物品仍然伸展到屏幕的宽度。

以下是我正在使用的适配器的代码:

private class EmailAdapter extends ArrayAdapter<EmailItem> {

    ArrayList<EmailItem> items;

    public EmailAdapter(Context context, int textViewResourceId, ArrayList<EmailItem> Items) {
        super(context, textViewResourceId, Items);
        this.items = Items;
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.list_item, null);
        }
        final EmailItem o = items.get(position);
        if (o != null) {
            TextView date = (TextView) v.findViewById(R.id.date_txt);
            TextView time = (TextView) v.findViewById(R.id.time_txt);
            TextView count = (TextView) v.findViewById(R.id.count_txt);
            TextView sub = (TextView) v.findViewById(R.id.sub_txt);
            TextView sender = (TextView) v.findViewById(R.id.sender_txt);
            TextView body = (TextView) v.findViewById(R.id.mail_txt);

            ImageView read = (ImageView) v.findViewById(R.id.read_unread);
            ImageButton flag = (ImageButton) v.findViewById(R.id.flag_mail);

            date.setText(o.date);
            time.setText(o.time);
            count.setText(String.valueOf(o.count));
            sub.setText(o.sub);
            sender.setText(o.sender);
            body.setText(o.body);
            if (o.read) {
                read.setImageResource(R.drawable.mail_read);
                View vrule = (View) v.findViewById(R.id.h_rule);
                vrule.setVisibility(View.INVISIBLE);
            } else {
                read.setImageResource(R.drawable.mail_unread);
            }

            if (o.flag) {
                flag.setImageResource(R.drawable.star_full);
            } else {
                flag.setImageResource(R.drawable.star);
            }
        }
        return v;
    }
}

请帮我解决这个问题。

我需要添加选择反馈(点亮或选择发光)并在结尾处停止(滚动列表后将完全显示项目的部分项目可见性。)

任何有关如何实现这些目标的见解都会非常明显。

2 个答案:

答案 0 :(得分:0)

水平ListView这样的东西不存在,并且自定义适配器,如果在实际的ListView上设置它将无法获得所需的结果。

为什么不调查Gallery班级? 它更接近正在寻找的东西,您可以更改适配器的getView以根据需要呈现视图。

答案 1 :(得分:0)

查看addAndMeasureChild()中的HorizontalListView功能。您可以在测量

时指定最大宽度/高度
child.measure(
    MeasureSpec.makeMeasureSpec(yourWidth, MeasureSpec.AT_MOST),
    MeasureSpec.makeMeasureSpec(yourHeight, MeasureSpec.AT_MOST));

然而,这种解决方法仅适用于,如果您的项目总是具有相同(最大)宽度/高度,但对我来说它确实有效。

至于选择反馈,只需向OnItemClickListener注册HorizontalListView,然后在OnItemClick方法中指定所需的行为。