4

Would it be possible to create a ListView where each sub-item is a ListView on it's own?

For example: Let's say that the ListView with ID "list" contains items of type "sublist", where the "sublist" contains "sublist_item" views?

In list.xml:

<ListView 
    android:id="@+id/list"
    ...
    tools:listitem="sublist"
/>

In sublist.xml:

<ListView 
    android:id="@+id/sublist"
    ...
    tools:listitem="sublist_item"
/>

In sublist_item.xml:

<LinearLayout 
    android:id="@+id/sublist_item"
    ...     >

     <TextView
         android:text="I'm a sublist-item"
         ...
     </TextView>
</LinearLayout>

I don't think this approach would be a good one even if it would work - so how would you go about solving the problem of having an unknown number of ListViews within a Fragment? Sort of like this:

<ScrollView 
    android:id="@+id/listview_container"
    ...   >

     <ListView
        android:id="@+id/list1"
        ...
     </ListView>

     <ListView
        android:id="@+id/list2"
        ...
     </ListView>

     ...


</ScrollView>

But the problem is that I can't know the number of ListViews that is needed when creating the XML layout.

aberg
  • 269
  • 3
  • 12
  • 3
    Does ExpandableListView meet your needs ? – Madhur Ahuja Jul 28 '14 at 09:59
  • Theoretical, it's possible but I think an expandableListView would be better. – Gödel77 Jul 28 '14 at 10:01
  • It is possible in many of my apps i have been using horizontal listview inside listview and set its adapter in getview of listview. if you need example of code let me know – Umer Kiani Jul 28 '14 at 10:03
  • @UmerKiani I would love to see some examples if you could provide me with a link! Thanks – aberg Jul 28 '14 at 10:23
  • I dont have a link. I implemented it my self i had horizontal listview inside a listview the above example would be same i am currently online from cell will post soltuion in a while – Umer Kiani Jul 28 '14 at 10:24
  • http://stackoverflow.com/questions/3135112/android-nested-listview possible duplicate – Brandon Ling Jul 28 '14 at 10:31

1 Answers1

2

I would an expandable listview using something similar to the code below:

main class (in fragment)

public class LineupFragment extends Fragment {

    View rootView;
    ExpandableListView lv;
    private String[] groups;
    private String[][] children;


    public LineupFragment() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        groups = new String[] { "Test Header One", "Test Header Two", "Test Header Three", "Test Header Four" };

        children = new String [][] {
                { "s simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." },
                { "Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of comes from a line in section 1.10.32." },
                { "It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like)." },
                { "There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc." }
        };
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_lineup, container, false);  

    return rootView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        lv = (ExpandableListView) view.findViewById(R.id.expListView);
        lv.setAdapter(new ExpandableListAdapter(groups, children));
        lv.setGroupIndicator(null);
    }

    public class ExpandableListAdapter extends BaseExpandableListAdapter {

        private final LayoutInflater inf;
        private String[] groups;
        private String[][] children;

        public ExpandableListAdapter(String[] groups, String[][] children) {
            this.groups = groups;
            this.children = children;

            inf = LayoutInflater.from(getActivity());
        }

        @Override
        public int getGroupCount() {
            return groups.length;
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return children[groupPosition].length;
        }

        @Override
        public Object getGroup(int groupPosition) {
            return groups[groupPosition];
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return children[groupPosition][childPosition];
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

        @Override
        public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
             ViewHolder holder;
                if (convertView == null) {
                    convertView = inf.inflate(R.layout.list_item, parent, false);
                    holder = new ViewHolder();

                    holder.text = (TextView) convertView.findViewById(R.id.lblListItem);
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();
                }


                holder.text.setText(getChild(groupPosition, childPosition).toString());

                return convertView;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            ViewHolder holder;

            if (convertView == null) {
                convertView = inf.inflate(R.layout.list_group, parent, false);

                holder = new ViewHolder();
                holder.text = (TextView) convertView.findViewById(R.id.lblListHeader);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.text.setText(getGroup(groupPosition).toString());

            ImageView Photo = (ImageView) convertView.findViewById(R.id.photo);


            //Images for all artists need to be hardcoded below

            if (groupPosition == 0) {
                Photo.setBackgroundResource(R.drawable.ivan_grech);
            }
            else if (groupPosition == 1) {
                Photo.setBackgroundResource(R.drawable.ira_losco);
            }

            return convertView;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }

        private class ViewHolder {
            TextView text;
        }
    }
}

Adapter class:

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Activity _context;
    private List<String> _listDataHeader; // header titles
    private HashMap<String, List<String>> _listDataChild;// child data in format of header title, child title

    public ExpandableListAdapter(Activity context, List<String> listDataHeader,
            HashMap<String, List<String>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {

        final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item, null);
        }

        TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);

        txtListChild.setText(childText);
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);
        }

        TextView lblListHeader = (TextView) convertView.findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

Hope this helps :)

Michele La Ferla
  • 6,775
  • 11
  • 53
  • 79