2012-08-08 97 views
4

這個問題已經被問這裏a link動態改變listview的分隔高度?

此外,我想澄清的問題 我有一個列表視圖 10個列表項目我想有各列表項的deviderheight不同像第一個項目應該setDividerheight( 2)第二setDividerheight(4)這樣的..

我做了一個自定義的Adapeter在我設置我的佈局就像

公共查看getView(INT位置,查看convertView,ViewGroup以及母公司){ 查看v = super.getView(position,convertView,parent);

if(position ==2) 
    { 
     if (v != convertView && v != null) { 
      ViewHolder holder = new ViewHolder(); 

      // TextView tv = (TextView) v.findViewById(R.id.artist_albums_textview); 
      // holder.albumsView = tv; 

      convertView = mInflater.inflate(R.layout.jazz_artist_list_item, null); 
      holder.albumsView = (TextView)convertView.findViewById(R.id.artist_albums_textview); 

      // lv.setDividerHeight(8); 
      v.setTag(holder); 
      } 
    } 
    else 
    { 
     if (v != convertView && v != null) { 
      ViewHolder holder = new ViewHolder(); 

      convertView = mInflater.inflate(R.layout.jazz_artist_list_item, null); 
      holder.albumsView = (TextView)convertView.findViewById(R.id.artist_albums_textview); 

      // lv.setDividerHeight(2); 
      v.setTag(holder); 
      } 
    } 

但這似乎不能正常工作。

上面這個任何想法如何設置列表視圖的分隔器高度動態

問候, Laxmikant

回答

5
//set Divider as you like 

listView.setDivider((Drawable) getResources().getDrawable(R.drawable.orange)); 

//then set the height dynamically 

listView.setDividerHeight(1); 
在你的活動具有ListView控件

。不是適配器類。

如果你在問題中寫了什麼。這樣做:

讓每個listView項目佈局包含一個TextView和一個視圖(每個項目之後的分隔符),然後根據您在getView()方法中獲得的位置參數更改視圖的高度。

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:padding="5dp" > 
    <TextView 
     android:id="@+id/label" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/logo" 
     android:padding="5dp" 
     android:textSize="14dp" > 
    </TextView> 
    <View 
     android:id="@+id/view" 
     android:layout_width="fill_parent" 
     android:layout_height="1dp" 
     android:layout_below="@id/label" 
     android:background="@drawable/orange" /> 
</RelativeLayout> 

現在在適配器類的ViewHolder包含TextView的,也是觀。

所以,

Holder.View = (View)convertView.findViewById(R.id.view); 
if(position == 0){ 
    (Holder.View).setHeight(2); 
} 

等。

+0

嗯,我的工作是基於拖放一個ListView,所以在我的情況下,我不能添加一個分配器是ListItem的一部分,我想要一個分離器更像是一個Listviews分配器;-) – kendrelaxman 2012-08-08 06:33:14

+0

我已經嘗試過這種解決方案,它給了我幾乎所需的一些調整,但有一個當我滾動高度的問題正在改變的意見。我猜這是在getview中的convertview bcoz是否有任何解決方案來處理? – kendrelaxman 2012-08-14 09:01:25

+0

我不認爲@kendrelaxman在問題中提出的是什麼。他想爲ListView中的行的不同視圖設置不同的dividerHeight。 – JaydeepW 2014-06-12 07:25:02

0

點小文章上面得到它的工作對我來說(?在查看沒有自動調用setHeight()方法),謝謝:

Holder.View = (View)convertView.findViewById(R.id.view); 
if(position == 0){ 
    (Holder.View).getLayoutParams().height = *your desired height e.g. 20*; 
}