12

我有一个Activity包含一个ListView基于SimpleCursorAdapter和一个按钮。当我单击按钮时,我希望行高变大。我决定TextView在按钮上使用高度OnClick

TextView tvRow = (TextView) findViewById(R.id.tvRow);
tvRow.setHeight(20);

但它只更改列表的第一行。

Adapter我可以在'sgetView方法中更改高度:

TextView tvRow = (TextView) view.findViewById(R.id.tvRow);
tvRow.setHeight(20);

但这不是我需要的功能;我需要这个按钮OnClick

如何ListView通过点击按钮更改行高?甚至可能是一个技巧 :) 谢谢你的时间。

更新:

当我将此代码放入SimpleCursorAdapter一切正常时,但在方法getViewBaseAdapter

@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;

显示的列表忽略了这一点,它的行宽保持不变。怎么了?

更新:

<?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>

4

2 回答 2

18

我认为您必须更改您的getView()方法SimpleCursorAdapter,如下所示:

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您可以设置:

mRowHeight = LayoutParams.WRAP_CONTENT

升级版:

如果您返回的方法hasStableIds()(默认返回),您必须在您的(您必须手动设置)中应用少量更改:BaseAdapterfalsefalsegetView()LayoutParamsView

LayoutParams params = view.getLayoutParams();
if (params == null) { 
    params = new LayoutParams(LayoutParams.MATCH_PARENT, mRowHeight); 
} else {
    params.height = mRowHeight;
}

view.setLayoutParams(params);
于 2012-12-04T20:21:34.343 回答
5

我做了类似的事情:

@Override
public View getView(int position, View convertView,ViewGroup parent) {
        View view = super.getView(position, convertView, parent);

        TextView textView=(TextView) view.findViewById(android.R.id.text1);
        textView.setHeight(30);
        textView.setMinimumHeight(30);



        / * Couleur de votre choix * / 
        textView . SetTextColor ( Couleur . BLACK );



        retourner voir ; 
    }

您必须将两个字段都放入 textView.setHeight (30); textView.setMinimumHeight (30); 否则它将无法正常工作。对我来说它有效,我遇到了同样的问题。

于 2013-04-15T09:06:43.117 回答