我需要写一个可变高度的 ListView 。我也需要它用于显示目的。列号是不变的。我还需要一些不同颜色的细胞背景。那么,如何实现呢?我应该使用 listview 方式还是在 scrollview 上做一些自定义绘图? 一些 fragment 会帮助我。
最佳答案
inside your listview
...
<ListView
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollingCache="true"
android:divider="#000000"
android:dividerHeight="1px"
android:background="#FFFFFF"
android:cacheColorHint="#FFFFFF" android:fastScrollEnabled="true"
/>
...
you will inflate diferent types of layouts, customizing sizes and colours. supossing we have two types, rowa.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50px"
android:layout_gravity="center_horizontal"
android:background="#FF0000">
<TextView
android:id="@+id/row_section"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/imagearticle"
android:layout_toLeftOf="@id/Lrightaccessory"
android:capitalize="characters"
android:textStyle="bold"
android:ellipsize="marquee"
android:textColor="@color/colorSecciones"
android:focusable="false"
/>
</LinearLayout >
and rowb.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="20px"
android:background="#00FF00">
<TextView
android:id="@+id/row_section"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/imagearticle"
android:layout_toLeftOf="@id/Lrightaccessory"
android:capitalize="characters"
android:textStyle="bold"
android:ellipsize="marquee"
android:textColor="@color/colorSecciones"
android:focusable="false"
/>
</LinearLayout >
在你的 getview() 函数中你决定 哪个位置会使您的不同布局膨胀。
public View getView(int position, View convertView, ViewGroup parent) {
View result=convertView;
if (result == null) {
if (position == 0) { //Inflating row type A
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
result = vi.inflate(R.layout.rowa, null);
views.set(position, result);
}
else if (position == 1) { //Inflating row type B
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
result = vi.inflate(R.layout.rowb, null);
}
}
return result;
}
关于java - 可变高度 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3336147/