开发者

android nested listview

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, a开发者_StackOverflow社区nd then in each row, you'd have another list view for the comments for each post (that would be collapsible)


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.


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


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜