简体   繁体   中英

Load Child Item when click on Parent List Item in Expandable List view in android

I am developing an Android Application that Contain one Expandable List view for Load data from web service, but I do not want to load whole data on start up but first load parent list then when expand particular list item of list view load that particular item's child item so first is it possible or not ?

My Sample Code :

package adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.rk.socialsync.R;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import utils.Global;
import utils.OnlineProperty;

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context _context;
    private List<Date> _listDataHeader;
    private HashMap<Date, List<OnlineProperty>> _listDataChild;

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

    @Override
    public OnlineProperty 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) {

        OnlineProperty onlineProperty = getChild(groupPosition, childPosition);

        String child1Text = onlineProperty.getOnlineTime();
        String child2Text = onlineProperty.getOfflineTime();

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

        TextView textOnline = (TextView) convertView.findViewById(R.id.textTimeOnline);
        TextView textOffline = (TextView) convertView.findViewById(R.id.textTimeOffline);
        TextView textView = (TextView) convertView.findViewById(R.id.textTo);

        textView.setTypeface(Global.AppsFont);
        textOnline.setText(child1Text);
        textOffline.setText(child2Text);

        textOffline.setTypeface(Global.AppsFont);
        textOnline.setTypeface(Global.AppsFont);
        return convertView;
    }

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

    @Override
    public String getGroup(int groupPosition) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        return dateFormat.format(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.listelementexpandable, null);
        }

        TextView textHeader = (TextView) convertView.findViewById(R.id.textExpandable);
        textHeader.setTypeface(Global.AppsFont);
        textHeader.setText(headerTitle);

        return convertView;
    }

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

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

}

Other Question that is i want custom change in layout like expand button that i have point in image Like + and -

在此处输入图片说明

Yes it's possible and should be done through your ExpandableListView 's OnGroupClickListener . First make the children array in your adapter public and initialized by default:

public HashMap<Date, List<OnlineProperty>> _listDataChild = 
            new HashMap<Date, List<OnlineProperty>>();

Next Override the groupClickListener:

mExpList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
    @Override
    public boolean onGroupClick(ExpandableListView p, View v, int grpPos, long id) {
        // Check if children not yet fetched
        if (mExpAdapter._listDataChild.get(date) == null) {
            // Fetch list of children for group "grpPos"

            // Add the children to your adapters HashMap
            mExpAdapter._listDataChild.put(date, children);
        }                       
        return false;
    }
});

Notes:

  • To avoid loading too much, you can Override onGroupCollapsed in your adapter to make it un-load the children that aren't being used.
  • The code I supplied might be incorrect as I don't know your data structure. You'll have to adapt.

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