单击android中可扩展列表视图中的父列表项时加载子项

时间:2014-08-29 09:55:05

标签: java android expandablelistview

我正在开发一个包含一个可扩展列表视图的Android应用程序,用于从Web服务加载数据,但我不想在启动时加载整个数据,但是首先加载父列表然后在展开列表视图的特定列表项时加载特定项目的子项目首先是否可能?

我的示例代码:

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

}

其他的问题是我想在布局中自定义更改像扩展按钮那样在图像中有点像+和 -

enter image description here

1 个答案:

答案 0 :(得分:1)

是的,可以通过ExpandableListView OnGroupClickListener来完成。首先将适配器中的children数组设为public并默认初始化:

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

下一步覆盖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;
    }
});

注意:

  • 为避免加载过多,您可以覆盖适配器中的onGroupCollapsed,以便卸载未使用的子项。
  • 我提供的代码可能不正确,因为我不知道您的数据结构。你必须适应。