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
1.2k views
in Technique[技术] by (71.8m points)

android - how can i automatically size listview so it doesn't scroll

currently i have the following layout

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 

    android:layout_marginTop="9px" 
    android:layout_below="@+id/desc" 
    android:id="@+id/ll_item"  
    android:orientation="vertical" 
    android:paddingRight="3px" 
    android:paddingLeft="3px" 
    android:paddingBottom="5px" 
    android:paddingTop="5px" 
    android:background="@drawable/rounded_corner_lists" >
<!--
        <ListView android:drawSelectorOnTop="false" android:id="@+id/lv"    android:layout_height="fill_parent" android:layout_width="wrap_content" android:divider="#ddd" android:dividerHeight="1px" android:background="@drawable/white"    />
    -->
    </LinearLayout>

the listview that i have commented out, i have tried to make this in the xml, with the height set to wrap_content, fill_parent, currently i am doing this programatically with the following code

LinearLayout ll_item = (LinearLayout) this.findViewById(R.id.ll_item);
        if(list.length() > 0)
        {
            ll_item.setVisibility(View.VISIBLE);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,calcListHeight(list));

            listview = new ListView(this);
            listview.setBackgroundResource(R.drawable.white);
            listview.setDivider( new ColorDrawable(this.getResources().getColor(R.drawable.dividercolor)) );
            listview.setDividerHeight(1);
            listview.setCacheColorHint(0);

            mAdapter  = new JSONAdapter( list, this );
            listview.setAdapter(mAdapter);
            mAdapter.notifyDataSetChanged();
            ll_item.addView(listview, lp);
        }

this is the result

alt text

alt text

so you can see in this image, that since i'm containing the listview in a linearlayout to get the rounded corner look, it doesn't just automatically stretch to contain the entire listview, is there any way to have the two elements just wrap the content vertically so there is no scrolling without me programatically setting the height ? ? ?

i guess one other thing i should mention is that i have all this layout in a scrollview, because i want this listview to be a tiny subsection of the entire layout, so it would be something like

-scrollview
-textview
-textview

-linearlayout
-listview

- button

here is a simpler layout of what i have

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical" android:layout_height="fill_parent" android:layout_width="fill_parent"
              android:background="@drawable/bg" xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/titlebar">

    <ScrollView android:id="@+id/sv" android:layout_width="fill_parent" android:background="@drawable/bg"
                android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">

        <RelativeLayout android:id="@+id/widget28"
                        android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="4dip"
                >


            <LinearLayout android:orientation="vertical" style="@style/rounded_corner_full_width_button"
                          android:id="@+id/editfields">

                <ListView android:drawSelectorOnTop="false" android:id="@+id/lv" android:layout_height="fill_parent"
                          android:layout_width="wrap_content" android:divider="#ddd" android:dividerHeight="1px"
                          android:background="@drawable/white"/>

            </LinearLayout>
        </RelativeLayout>
    </ScrollView>
</LinearLayout>
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

ListViews do not go in ScrollViews.

ListView is for displaying a limited window into unbounded content efficiently. If you were to "disable scrolling" on a ListView to put it within a ScrollView you lose all practical reason for using a ListView in the first place.

If you want to use a ListView to show lots of content or unbounded content but also have content above and below that scrolls with it, add header or footer views to the ListView using addHeaderView or addFooterView. If the list content is going to be a small portion of your overall layout as you describe, this probably isn't the best approach for you.

If you have a small, bounded set of content to present, go ahead and use a ScrollView and programmatically generate child views for your "list items" where appropriate.

A common pattern used in the framework to mix inflated XML content with programmatically generated content is to add a placeholder view in the layout XML, usually a LinearLayout or FrameLayout. Use findViewById to locate it at runtime and add generated child views to it.

You could even still use a ListAdapter with this approach if you have one written already, just call content.addView(adapter.getView(position, null, content)) in a loop for all adapter positions (where content is the placeholder view you located with findViewById). Note that this is only practical if you know that you have a small number of list items in the adapter!


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

...