我有与ListView的基础上SimpleCursorAdapter和一个按钮的活动。 我想我按一下按钮,行高变大。 我决定使用TextView的高度这一点。 在按钮的OnClick:
I have got an activity with ListView based on SimpleCursorAdapter and a button. I want I click the button and row height gets bigger. I decided to use TextView height for this. in button OnClick:
TextView tvRow = (TextView) findViewById(R.id.tvRow);
tvRow.setHeight(20);
但它改变了一个列表,只有第一行。
but it changes only first row of a list.
我可以在适配器改变高度,在getView方法
I can change height in Adapter, in getView method
TextView tvRow = (TextView) view.findViewById(R.id.tvRow);
tvRow.setHeight(20);
但它不是正确的我需要什么,我需要这样的按钮的OnClick。
but it is not correctly what I need, I need this in button OnClick.
如何通过按钮来更改的ListView行高度? 可能连一招:) 感谢您的时间。
How can I change ListView Row height by button? May be even a trick :) Thank you for your time.
UPD
当我不喜欢这样的code SimpleCursorAdapter everithing工作正常,但在BaseAdapter方法getView:
When I do like this in code SimpleCursorAdapter everithing 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 ignore this and its row width stay original. What is wrong?..
UPD:
<?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
返回假
(它返回假
默认)必须适用于变化不大的 getView()
(你必须设置的LayoutParams
您查看
手动):
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);
相关推荐
最新文章