简体   繁体   中英

How to override the child listview of a ExpandableListView

I would like the children of ExpandableListView to be shown in a scrollview. If there are more than 10 comments on a group item, i would like to be able to scroll through the children without scrolling the main screen.

I have tried googling this but all I found are standard tutorials on expandablelists. I did read that ExpandableListView uses a ListView for its childelements so I assume that I should be able to override the layout of the list but i dont quite know where to start.

Could you guys point me in the right direction?

For this, you would need a couple of classes, so I'll cut to it and just give it to you.

import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class ItemsExpandableListAdapter extends BaseExpandableListAdapter {

 private Activity context;
    private Map<String, List<String>> ItemsCategoryCollections;
    private List<String> ItemsCategories;

    public ItemsExpandableListAdapter(Activity context, List<String> Categories,
            Map<String, List<String>> CategoryCollections) {
        this.context = context;
        this.ItemsCategoryCollections = CategoryCollections;
        this.ItemsCategories = Categories;
    }

    public Object getChild(int groupPosition, int childPosition) {
        return ItemsCategoryCollections.get(ItemsCategories.get(groupPosition)).get(childPosition);
    }

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

    public View getChildView(final int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        final String Itemscategory = (String) getChild(groupPosition, childPosition);
        LayoutInflater inflater = context.getLayoutInflater();

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.items_child_item, parent, false);
        }


        return convertView;
    }

    public int getChildrenCount(int groupPosition) {
        return ItemsCategoryCollections.get(ItemsCategories.get(groupPosition)).size();
    }

    public Object getGroup(int groupPosition) {
        return ItemsCategories.get(groupPosition);
    }

    public int getGroupCount() {
        return ItemsCategories.size();
    }

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

    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        String ItemsName = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.items_group_item, parent, false);
        }

        return convertView;
    }

    public boolean hasStableIds() {
        return true;
    }

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

In the getChildView Method above, you can change the view in the layout to a Listview, then you can implement what type of Listview you want using a quick google search on what you desire (I don't know what you need, specify and I'll edit to suit your need).

In my ItemsActivity, I'll do this like so (I'm using a Fragment based system not an Activity type class, so put in the right OnCreate Method)

ExpandableListView expListView;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    expListView = (ExpandableListView) getActivity().findViewById(R.id.exLstItems);
    final ExpandableListAdapter expListAdapter = new ItemsExpandableListAdapter(getActivity(), groupItemList, ItemCollection);
    expListView.setAdapter(expListAdapter);

    expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
            if (groupPosition == 0){
                if (childPosition == 0){
                }
            }
         }
    });
}

For the items_child_item.xml, a simple implementation of your Listview should do the trick.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<ListView
    android:id="@+id/lstItemsChild"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:gravity="center|left"/>

</RelativeLayout>

Incase you are feeling lazy/lucky, here is the items.xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp" >

<ExpandableListView
    android:id="@+id/exLstItems"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:childDivider="@color/red"
    android:divider="@color/green"
    android:dividerHeight="2dp"
    android:fastScrollEnabled="true"
    android:groupIndicator="@null"
    android:overScrollMode="always"
    android:scrollbars="none"
    android:smoothScrollbar="true" >
</ExpandableListView>

</RelativeLayout>

I'm assuming you know how to add items to an Expandable view, so I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM