Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android: listview in listview

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 way to to this? table, grid, ...? the problem i'm facing right now is that the inner listview doesn't take the space it needs, so it's cut at about the end of the first listitem. if i try to scroll, just the outer listview is scrolling which is exactly what i want.

how_it_should_look_like thanks, my final solution is

LinearLayout layout = (LinearLayout) row.findViewById(R.id.LLBroadcasts); layout.removeAllViews();  for (Item b : bs.getItems()) {    View child = _inflater.inflate(R.layout.item_row, null);    TextView tvTitle = (TextView) child.findViewById(R.id.TVItemTitle);   tvTitle.setText(b.getTitle());    TextView tvDesc = (TextView) child.findViewById(R.id.TVItemDescription);   tvDesc.setText(b.getDescription());   layout.addView(child); } 
like image 445
kopi_b Avatar asked Jul 25 '12 15:07

kopi_b


People also ask

Is ListView deprecated Android?

Conclusion. While the ListView is still a very capable view, for new projects, I'll strongly advise you use RecyclerView, and consider the ListView as deprecated. I can't think of any situation where the ListView is better than the RecyclerView, even if you implement your ListView with the ViewHolder pattern.

What is an ArrayAdapter in Android?

android.widget.ArrayAdapter<T> You can use this adapter to provide views for an AdapterView , Returns a view for each object in a collection of data objects you provide, and can be used with list-based user interface widgets such as ListView or Spinner .

How do I use ArrayAdapter?

Go to app > res > layout > right-click > New > Layout Resource File and create a new layout file and name this file as item_view. xml and make the root element as a LinearLayout. This will contain a TextView that is used to display the array objects as output.

Which is better ListView or RecyclerView?

Simple answer: You should use RecyclerView in a situation where you want to show a lot of items, and the number of them is dynamic. ListView should only be used when the number of items is always the same and is limited to the screen size.


2 Answers

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

like image 182
ChrLipp Avatar answered Sep 28 '22 03:09

ChrLipp


I have the same problem in my App but I needed to use a ListView cause it was a shared item and I didn't want to replicate equal components. So.. I just fixed the size of inner ListView programatically to show all rows and.. voila! Problem solved:

ViewGroup.LayoutParams layoutParams = innerListView.getLayoutParams(); layoutParams.height = (int) context.getResources().getDimension(R.dimen.rowheight) * innerListView.getCount(); innerListView.setLayoutParams(layoutParams); CustomAdapter adapter = new CustomAdapter(context, blabla..); innerListView.setAdapter(adapter);  rowListView.invalidate(); 
like image 38
Ian Holing Avatar answered Sep 28 '22 04:09

Ian Holing