android nested listview

后端 未结 5 2041
别跟我提以往
别跟我提以往 2020-11-27 05:15

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 lis

相关标签:
5条回答
  • 2020-11-27 05:48

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

    0 讨论(0)
  • 2020-11-27 05:59

    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.

    0 讨论(0)
  • 2020-11-27 06:02

    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 讨论(0)
  • 2020-11-27 06:05

    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.

    0 讨论(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 讨论(0)
提交回复
热议问题