28

is it possible/advisable to have a nested listview?

i.e. a listView that's contained within a row of another listview?

an example would be where my main list is displaying blog posts, and then in each row, you'd have another list view for the comments for each post (that would be collapsible)

Ben
  • 16,124
  • 22
  • 77
  • 122

5 Answers5

34

I had the same problem today, so this is what I did to solve it:

I have a ListView, with a CustomAdapter, and on the getView of the customAdapter, I have something like this:

LinearLayout list = (LinearLayout) myView.findViewById(R.id.list_musics);
list.removeAllViews();

for (Music music : albums.get(position).musics) {
    View line = li.inflate(R.layout.inside_row, null);

    /* nested list's stuff */

    list.addView(line);
}

So, resuming, It's not possible to nest to ListViews, but you can create a list inside a row using LinearLayout and populating it with code.

Paulo Cesar
  • 2,250
  • 1
  • 25
  • 35
  • I want to implement list Within the List but i am not Able to implement .is there any Alternative for Solving this issue?except Expandable ListView – seon Jun 05 '17 at 06:27
13

Is what you're looking for the ExpandableListView? Of course, that's limited to only two levels of listings (but that sounds like it would work for your needs).

kiswa
  • 14,737
  • 1
  • 21
  • 29
  • well, the thing is, that i dont want the list to be exapandible... i just want to show some nested data... i.e. the original blog post, then the 2/3 most recent comments, and perhaps followed by a 'show all comments' button... – Ben Jun 28 '10 at 22:42
  • 1
    That sounds like a different layout to me. I'd see that as a container of some kind (LinearLayout, RelativeLayout, whatever) with a TextView for the blog post, and a ListView for the comments. Just load the first few comments into the list view, and implement a click listener for the last list item which loads the remaining comments. My $.02 anyway. – kiswa Jun 28 '10 at 23:19
  • well yeah except that there will be multiple blog posts & multiple comments under each blog post. so i tried a test, by having my row view layout contain a list view, i then used a simple array list adapter w/ some hard coded dummy strings just to see if i could get it to render. it rendered, the inner list view appeared to have scrollbars within it, but that list was not scrollable. it kinda looked like a hacked up iframe, except it only showed the first row, no matter what i did. not even height=fill_parent would work. my next approach will be to try a table view nested in the list. – Ben Jun 29 '10 at 01:01
6

This sound like what you're looking for? If you're not, or if this doesn't work, I would suggest having two list views: one of, say, blog posts, and the second of comments, and an action on a blog post item takes you to the second view, populated with the relevant comments.

Kris
  • 566
  • 3
  • 12
  • unfortunately, we want to display the comments inline... so opening them in another activity wont work. – Ben Jun 28 '10 at 18:44
0

you can do it like this :

inside the parent listview row xml layout add the following table layout

   <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/table_show"
        android:background="#beb4b4">
    </TableLayout>

then you have to make a layout for the child list with name reply_row.xml

<?xml version="1.0" encoding="utf-8"?>
<TableRow android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="3dp"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tv_reply_row"
        android:textColor="#000"/>
</TableRow>

in your parent listview adapter getview method add the following code :

TableLayout replyContainer = (TableLayout) 
// vi is your parent listview inflated view
vi.findViewById(R.id.table_show); 
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);


       //child listview contents list
        String [] replys = {"a","b","c","d"};

        for (int i=0;i<replys.length;i++)
        {
        final View comments = inflater.inflate(R.layout.reply_row, null);
        TextView reply_row = (TextView) comments.findViewById(R.id.tv_reply_row) ;
        reply_row.setText(replys[i]);

//for changing your tablelayout parameters 
        TableLayout.LayoutParams tableRowParams=new TableLayout.LayoutParams
                      (TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);

        int leftMargin=3;
        int topMargin=2;
        int rightMargin=3;
        int bottomMargin=2;
        tableRowParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
        comments.setLayoutParams(tableRowParams);
            TableRow tr = (TableRow) comments;
            replyContainer.addView(tr);
          }
0

You'd better use one ListView, not nested. Nesting ListView is an inefficient way. Your ListView may not scroll smoothly and take up more memory.

You could organize your data structure to show nested data in one ListView. Or you can use this project PreOrderTreeAdapter. It is convenient to show nested data in ListView or RecyclerView. It can be used to make ListView or RecyclerView collapsible, just change the way you provide your data than notify the adapter.