Change the divider height of listview dynamically?

让人想犯罪 __ 提交于 2019-11-30 20:55:52
//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.

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*;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!