在代码中设置 ListView 行高 [英] Set ListView row height in code
问题描述
我有一个 Activity
包含一个基于 SimpleCursorAdapter
的 ListView
和一个按钮.当我单击按钮时,我希望行高变大.我决定在按钮 OnClick
上使用 TextView
高度:
I have an Activity
containing a ListView
based on SimpleCursorAdapter
and a button.
When I click the button, I want the row height to get bigger.
I decided to use TextView
height for this on button OnClick
:
TextView tvRow = (TextView) findViewById(R.id.tvRow);
tvRow.setHeight(20);
但它只更改列表的第一行.
but it changes only the first row of the list.
我可以在Adapter
的getView
方法中改变高度:
I can change the height in the Adapter
's getView
method:
TextView tvRow = (TextView) view.findViewById(R.id.tvRow);
tvRow.setHeight(20);
但这不是我需要的功能;我需要这个按钮 OnClick
.
but that's not the functionality I need; I need this on button OnClick
.
如何通过点击按钮更改 ListView
行高?甚至可能是一个技巧:)感谢您抽出宝贵时间.
How can I change ListView
Row height by tapping a button?
Maybe even a trick :)
Thank you for your time.
更新:
当我将此代码放入 SimpleCursorAdapter
时,一切正常,但在 BaseAdapter
的方法 getView
中:
When I put this code in SimpleCursorAdapter
everything works fine, but in method getView
in BaseAdapter
:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.itemF, parent, false);
}
...
final LayoutParams params = view.getLayoutParams();
if (params == null) {
view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, mRowHeight));
}else {
params.height = mRowHeight; }
return view;
显示的列表只是忽略了这一点,它的行宽保持不变.怎么了?
the list at display just ignores this and its row width stays the same. What is wrong?
更新:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/llRow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/cbBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/llRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="5dp" >
<TextView
android:id="@+id/tvName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Наименование фирмы"
android:textAllCaps="true"
android:textSize="14dp" />
<TextView
android:id="@+id/tvAddr"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Адрес фирмы"
android:textSize="10dp" />
</LinearLayout>
推荐答案
我认为你必须改变 getView() 你的 SimpleCursorAdapter
方法,像这样:
I think you must chage getView() method of your SimpleCursorAdapter
, like this:
final SimpleCursorAdapter adapter = new SimpleCursorAdapter (context, cursor) {
@Override
public View getView (int position, View convertView, ViewGroup parent) {
final View view = super.getView(position, convertView, parent);
final TextView text = (TextView) view.findViewById(R.id.tvRow);
final LayoutParams params = text.getLayoutParams();
if (params != null) {
params.height = mRowHeight;
}
return view;
}
}
当您单击按钮时,您必须更改 mRowHeight
并通知 ListView
有关诸如 adapter.notifyDataSetChanged()
之类的更改.对于 mRowHeight
的第一个值,您可以设置:
When you click on your button, you must change mRowHeight
and notify ListView
about changes like adapter.notifyDataSetChanged()
. For the first value of mRowHeight
you can set this:
mRowHeight = LayoutParams.WRAP_CONTENT
UPD:
如果方法 hasStableIds()你的 BaseAdapter
返回 false
(它返回 false
作为默认值)你必须在你的 getView()
(您必须手动将 LayoutParams
设置为您的 View
):
If method hasStableIds() of your BaseAdapter
return false
(it return false
as default) you must apply little changes in your getView()
(you must set LayoutParams
to your View
manually):
LayoutParams params = view.getLayoutParams();
if (params == null) {
params = new LayoutParams(LayoutParams.MATCH_PARENT, mRowHeight);
} else {
params.height = mRowHeight;
}
view.setLayoutParams(params);
这篇关于在代码中设置 ListView 行高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!