动态更改listview的分频高度? [英] change the divider height of listview dynamically?

查看:242
本文介绍了动态更改listview的分频高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题已在此被问到链接



此外,我想澄清问题
我在Listview中有10个列表项
我想让每个列表项的deviderheight有所不同喜欢第一个项目应该是setDividerheight(2)for 2nd setDividerheight(4)like this ..



我已经制作了一个自定义的Adapeter,其中我设置了我的布局



public View getView(int position,View convertView,ViewGroup parent){
查看v = super.getView(position,convertView,parent); {
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(持有人);
}
}
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(持有人);
}
}

但这似乎无法正常工作。



以上任何想法,如何动态设置Listview的分隔线高度

Regards,
Laxmikant

解决方案

  //设置分隔符,你喜欢

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

//然后动态设置高度

listView.setDividerHeight(1);

在您的Activity中有ListView。不是适配器类。



如果你在问题中写的是什么。这样做:



让每个listView项目布局包含一个TextView和一个View(每个项目之后的分隔符),然后根据getView()方法中得到的position参数更改视图的高度。



ListView项目布局:

  <?xml version =1.0encoding =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和View。



所以

  Holder.View =(查看)convertView.findViewById(R.id。视图); 
if(position == 0){
(Holder.View).setHeight(2);
}

等等。


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

解决方案

//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.

这篇关于动态更改listview的分频高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆