android: listview in listview

前端 未结 5 1322
栀梦
栀梦 2020-11-28 06:31

i\'m trying to place a listview inside a listviewitem. the inner listview should not be scrollable but take all size it needs to display all it\'s rows. is there a better wa

5条回答
  •  眼角桃花
    2020-11-28 07:25

    From the Android documentation - Listview: ListView is a view group that displays a list of scrollable items

    You do not really want to scroll that inner list view, you want to scroll the outer listview. However I asume that the inner listview may vary on the amount of elements it contains.

    Instead of the inner list view you could use a

    • linear layout, see this tutorial or look at Adding content to a linear layout dynamically?
    • table layout

    For the linear layout (some sample code):

    // access your linear layout
    LinearLayout layout = (LinearLayout)findViewById(R.id.layout);
    // load the xml structure of your row
    View child = getLayoutInflater().inflate(R.layout.row);
    // now fill the row as you would do with listview
    //e.g. (TextView) child.findViewById(...
    ...
    // and than add it
    layout.addView(child);
    

    You should save the linear layout in a view holder (see View Holder pattern). I think the removeAllViews() is only necessary when the current row has lesser inner rows than the reused one, so I would also save the number of rows in the view holder.

    If the maximum number of inner rows is not to high you could also think about caching them in the view holder to avoid the inflate and findByViewId (lets say in an ArrayList).

提交回复
热议问题