Change the divider height of listview dynamically?

2019-02-13 23:18发布

This question has been asked here a link

Also I want to clarify the question I have 10 List Items in a Listview I want to have the deviderheight of each List Items differently like for first Item it should be setDividerheight(2) for 2nd setDividerheight(4) like this..

I have made a custom Adapeter in which I am setting my Layout Like

public View getView(int position, View convertView, ViewGroup parent) {
    View 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);
        }
    }
}

but this seems not working properly.

Any idea on above this as how to set the divider height of Listview dynamically

Regards, Laxmikant

2条回答
2楼-- · 2019-02-13 23:36

A slight tweak to above got it working for me (no setHeight() method in View?), thanks:

Holder.View = (View)convertView.findViewById(R.id.view);
if(position == 0){
  (Holder.View).getLayoutParams().height = *your desired height e.g. 20*;
}
查看更多
祖国的老花朵
3楼-- · 2019-02-13 23:42
//set Divider as you like   

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

//then set the height dynamically

listView.setDividerHeight(1);

in your Activity which has the ListView. Not the Adapter class.

If you what exactly what you wrote in the question. Do this:

let each listView Item layout contain a TextView and a View(divider after each item), then depending on the position parameter you get in the getView() method change the Height of the View.

ListView Item layout:

<?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>

now in the adapter class your ViewHolder contains the TextView and also the View.

so,

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

and so on.

查看更多
登录 后发表回答