Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
289 views
in Technique[技术] by (71.8m points)

android - Set ListView row height in code

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.

I can change the height in the Adapter's getView method:

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

but that's not the functionality I need; I need this on button OnClick.

How can I change ListView Row height by tapping a button? Maybe even a trick :) Thank you for your time.

UPDATE:

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?

UPDATE:

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...