动态改变列表视图的分隔高度? [英] change the divider height of listview dynamically?
问题描述
这个问题已经在这里<一问href="http://stackoverflow.com/questions/2749551/how-to-change-the-divider-height-of-listview-dynamically">a链接
此外,我想澄清的问题 我有一个列表视图10列表项 我想有每个列表项的deviderheight不同像第一个项目应该setDividerheight(2)第二setDividerheight(4)这样的..
我做了一个自定义的Adapeter中,我设置我的布局如同
公开查看getView(INT位置,查看convertView,ViewGroup中父){ 视图V = super.getView(位置,convertView,父母);
如果(位置== 2)
{
如果(V = convertView和放大器;!&安培;!V = NULL){
ViewHolder持有人=新ViewHolder();
// TextView的电视=(TextView中)v.findViewById(R.id.artist_albums_textview);
// holder.albumsView =电视;
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(保持器);
}
}
其他
{
如果(V = convertView和放大器;!&安培;!V = NULL){
ViewHolder持有人=新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(保持器);
}
}
但这似乎不能正常工作。
以上任何想法如何设置列表视图的分隔高度动态
问候, Laxmikant
//设置分频器,只要你喜欢
listView.setDivider(。(可绘制)getResources()getDrawable(R.drawable.orange));
//然后动态设置高度
listView.setDividerHeight(1);
在你的活动有ListView控件。没有适配器类。
如果你什么,你的问题写什么。做到这一点:
让每一个列表视图项布局包含一个TextView和一个视图(分隔每个项目后),则取决于你在getView(获得位置参数)方法来改变观看的高度。
ListView项的布局:
&LT; XML版本=1.0编码=UTF-8&GT?;
&LT; RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:填充=5DP&GT;
&LT;的TextView
机器人:ID =@ + /标记
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_toRightOf =@ ID /标志
机器人:填充=5DP
机器人:TEXTSIZE =14dp&GT;
&LT; / TextView的&GT;
&LT;查看
机器人:ID =@ + ID /视图
机器人:layout_width =FILL_PARENT
机器人:layout_height =1DP
机器人:layout_below =@ /标记
机器人:背景=@可绘制/橙色/&GT;
&LT; / RelativeLayout的&GT;
现在的适配器类的ViewHolder包含了TextView的,也是观。
所以,
Holder.View =(查看)convertView.findViewById(R.id.view);
如果(位置== 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.
这篇关于动态改变列表视图的分隔高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!