2013-02-16 66 views
0

我需要在我的應用中使用HorizontalListView,因爲在Android SDK中沒有這種存在的小部件,我使用的是this one而不是自己創建的。但問題在於列表項不能保持其大小並佔據屏幕的整個寬度。Horizo​​ntalListView不能正常工作

這裏的列表項的佈局:

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

請幫我解決這個問題。

我需要添加選擇反饋(點擊或選擇發光)並在最後停止(在滾動列表後將顯示項目完全沒有部分項目的可見性)。

任何有關如何實現這些目標的見解都將非常可觀。

回答

0

查看HorizontalListView中的addAndMeasureChild()函數。您可以測量像時指定的最大寬度/高度

child.measure(
    MeasureSpec.makeMeasureSpec(yourWidth, MeasureSpec.AT_MOST), 
    MeasureSpec.makeMeasureSpec(yourHeight, MeasureSpec.AT_MOST)); 

然而,這種解決方法只適用,如果你的項目將始終具有相同的(最大)寬度/高度,但對我來說它的伎倆。

至於選擇反饋,只需將OnItemClickListener註冊到您的HorizontalListView並在OnItemClick方法中指定所需的行爲。

0

像水平ListView這樣的事情不存在,並且定製適配器,如果您將它設置在實際的ListView上將無法獲得所需的結果。

爲什麼你不看Gallery類? 它與要查找的內容非常接近,並且可以根據需要更改適配器的getView以呈現視圖。

+0

我不明白爲什麼不創建這樣的視圖。不知道如何製作一個,但他可能也想檢查viewPager類。 – 2013-02-16 19:41:12

+0

我使用這個庫的Horizo​​ntalListView https://github.com/dinocore1/DevsmartLib-Android。 我已經看過畫廊類,其棄用和其中心鎖定。 我也在這個應用程序中使用過ViewPager,但是我想要一個具有ListView相同功能的水平列表視圖。 – Mehedi 2013-02-16 19:49:50