设置的ListView行高度code [英] Set ListView row height in code

查看:184
本文介绍了设置的ListView行高度code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有与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>

推荐答案

我想你一定恰克<一href="http://developer.android.com/reference/android/widget/CursorAdapter.html#getView%28int,%20android.view.View,%20android.view.ViewGroup%29">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:

如果方法<一href="http://developer.android.com/reference/android/widget/BaseAdapter.html#hasStableIds%28%29">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);

这篇关于设置的ListView行高度code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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