1

Expandable ListView を使用している 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 を取得できます。しかし、各 child の下に別のビューを追加したいと思います。

4

2 に答える 2

-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 に答える