1

我正在开发一个使用可扩展列表视图的 Android 应用程序。

活动 A 有以下片段,

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.Toast;

public class A extends Activity {

    ExpandableListAdapter listAdapter;
    ExpandableListView expListView;
    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.bussinessproductmanagement);
        getWindow().setWindowAnimations(0);
        // get the listview
        expListView = (ExpandableListView) findViewById(R.id.list);
        expListView.setGroupIndicator(null);
        expListView.setSelector(R.drawable.clientprojecthover);
        // preparing list data
        prepareListData();

        listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

        // setting list adapter
        expListView.setAdapter(listAdapter);

        // Listview Group click listener
        expListView.setOnGroupClickListener(new OnGroupClickListener() {

            @Override
            public boolean onGroupClick(ExpandableListView   parent, View v,
                    int groupPosition, long id) { 

                return false;
            }
        });

        // Listview Group expanded listener
        expListView.setOnGroupExpandListener(new OnGroupExpandListener() {

            @Override
            public void onGroupExpand(int groupPosition) {

                int len = listAdapter.getGroupCount();
                for (int i = 0; i < len; i++) {
                    if (i != groupPosition) {
                        expListView.collapseGroup(i);
                    }
                }
            }
        });

        // Listview Group collasped listener
        expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {

            @Override
            public void onGroupCollapse(int groupPosition) {
                Toast.makeText(getApplicationContext(),
                        listDataHeader.get(groupPosition) + " Collapsed",
                        Toast.LENGTH_SHORT).show();
            }
        });

        // Listview on child click listener
        expListView.setOnChildClickListener(new OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {
                // TODO Auto-generated method stub
                Toast.makeText(
                        getApplicationContext(),
                        listDataHeader.get(groupPosition)
                                + " : "
                                + listDataChild.get(
                                        listDataHeader.get(groupPosition)).get(
                                        childPosition), Toast.LENGTH_SHORT)
                        .show();

                return false;
            }
        });
    }

    /*
     * Preparing the list data
     */

    private void prepareListData() {
        listDataHeader = new ArrayList<String>();
    listDataChild = new HashMap<String, List<String>>();
    ArrayList<List<String>> childObjects=new ArrayList<List<String>>();

        for(int i=1;i<11;i++)
        {
        listDataHeader.add("Client "+ i);
        List<String> childLists = new ArrayList<String>();

        for(int childCount=1;childCount<11;childCount++)
        {
            childLists.add("Project "+childCount);
        }

        childObjects.add(childLists);

        }

        for(int j=0;j<listDataHeader.size();j++)
        {
            listDataChild.put(listDataHeader.get(j), childObjects.get(j));
        }
    }
    }

Expandable List Adapter 类有以下片段,

import java.util.HashMap;
import java.util.List;
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.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ExpandableListAdapter extends BaseExpandableListAdapter {

private Context _context;
private List<String> _listDataHeader;
private HashMap<String, List<String>> _listDataChild;

public ExpandableListAdapter(Context 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);
    ImageView imgView=(ImageView)convertView.findViewById(R.id.indicator);

    lblListHeader.setTypeface(null, Typeface.BOLD);
    lblListHeader.setText(headerTitle);

    if(isExpanded)
    {
        imgView.setImageResource(R.drawable.clientbuttonarrowt);
    }
    else
    {
        imgView.setImageResource(R.drawable.arrowrght);
    }

    return convertView;
}

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

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

在这里,我可以获得带有行及其子项的 Expandable ListView。但我想在每个孩子下添加另一个视图。

4

2 回答 2

0

在可展开视图中添加其他视图作为页脚视图,如下所示

ExpandableListView list = getListView();
View headerView = View.inflate(this, R.layout.header, null);
list.addHeaderView(headerView);
View footerView = View.inflate(this, R.layout.footer, null);
list.addFooterView(footerView);
于 2013-09-16T04:54:19.600 回答
-1

这是我为解决问题所做的

public class MyExpandableAdapter extends BaseExpandableListAdapter {
Context context;
List<ParentObject> parentObjects;

public MyExpandableAdapter(Context context, List<ParentObject> parentObjects)
{
    this.context = context;
    this.parentObjects = parentObjects;
}
@Override
public int getGroupCount() {
    return parentObjects.size();
}

 //Add 2 to childcount. The first row and the last row are used as header and footer to childview
@Override
public int getChildrenCount(int i) {
    return parentObjects.get(i).childObjects.size() +2;
}

@Override
public ParentObject getGroup(int i) {
    return parentObjects.get(i);
}

@Override
public ChildObject getChild(int i, int i2) {
    return parentObjects.get(i).childObjects.get(i2);
}

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

@Override
public long getChildId(int i, int i2) {
    return 0;
}

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

@Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup)
{
    ParentObject currentParent = parentObjects.get(i);
    if(view ==null)
    {
        LayoutInflater inflater =(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.parent_row,null);
    }
    TextView txtMother = (TextView)view.findViewById(R.id.txtMother);
    TextView txtFather = (TextView)view.findViewById(R.id.txtFather);
    txtMother.setText(currentParent.mother);
    txtFather.setText(currentParent.father);
    return view;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean b, View view, ViewGroup viewGroup) {
    ParentObject currentParent = getGroup(groupPosition);
    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    //the first row is used as header
    if(childPosition ==0)
    {
        view = inflater.inflate(R.layout.child_header, null);
        TextView txtHeader = (TextView)view.findViewById(R.id.txtHeader);
        txtHeader.setText(currentParent.textToHeader);
    }

    //Here is the ListView of the ChildView
    if(childPosition>0 && childPosition<getChildrenCount(groupPosition)-1)
    {
        ChildObject currentChild = getChild(groupPosition,childPosition-1);
        view = inflater.inflate(R.layout.child_row,null);
        TextView txtChildName = (TextView)view.findViewById(R.id.txtChildName);
        TextView txtChildAge = (TextView)view.findViewById(R.id.txtChildAge);
        txtChildName.setText(currentChild.childName);
        txtChildAge.setText("Age: " + String.valueOf(currentChild.age));
    }
    //the last row is used as footer
    if(childPosition == getChildrenCount(groupPosition)-1)
    {
        view = inflater.inflate(R.layout.child_footer,null);
        TextView txtFooter = (TextView)view.findViewById(R.id.txtFooter);
        txtFooter.setText(currentParent.textToFooter);
    }
    return view;
}

@Override
public boolean isChildSelectable(int i, int i2) {
    return false;
}

}

您可以在这里查看教程:http ://robusttechhouse.com/how-to-add-header-footer-to-expandablelistview-childview/ 希望对您有所帮助!

于 2015-06-07T03:22:03.490 回答