android:自定义ExpandableListView

时间:2013-01-22 12:49:00

标签: android android-listview expandablelistview baseadapter

我正在尝试通过调整我找到的一些代码来自定义expandableListView。这将调用自定义适配器,并为列表视图扩展所需的行。

我的问题:我需要将这个东西与其他布局组件一起使用,所以为了我需要调用setContentView,然后在我需要它的地方扩展listView。我的所有尝试都以应用程序崩溃告终,有人可以告诉我如何调整以下代码以在布局中工作吗?

MainActivity:

 public class MainActivity extends ExpandableListActivity implements
    OnChildClickListener {
private static final String[] Groups = { "G1", "G2",
        "G3", "G4" };
private static final String[] Services = { "Service1",
        "Service2", "Service3", "Service4", "Service5",
        "Service6" };
private static final String[] Test = { "Test1", "Test2",
    "Test3", "Test4" };
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    ExpandableListView expandbleLis = getExpandableListView();
    expandbleLis.setDividerHeight(2);
    expandbleLis.setGroupIndicator(null);
    expandbleLis.setClickable(true);

    setGroupData();
    setChildGroupData();

    NewAdapter mNewAdapter = new NewAdapter(groupItem, childItem);
    mNewAdapter
            .setInflater(
                    (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE),
                    this);
    expandbleLis.setAdapter(mNewAdapter);
    expandbleLis.setOnChildClickListener(this);
}

public void setGroupData() {
    for (int i = 0; i < 4; i++) {
        groupItem.add(Groups[i]);
    }
}

ArrayList<String> groupItem = new ArrayList<String>();
ArrayList<Object> childItem = new ArrayList<Object>();

public void setChildGroupData() {
    /**
     * Add Data For Services
     */
    ArrayList<String> child = new ArrayList<String>();
    for (int i = 0; i < 6; i++) {
        child.add(Services[i]);
    }
    childItem.add(child);

    /**
     * Add Data For Test
     */
    child = new ArrayList<String>();
    for (int i = 0; i < 4; i++) {
        child.add(Test[i]);
    }
    childItem.add(child);
    /**
     * 
     */
    child = new ArrayList<String>();
    child.add("Dummy Text");
    child.add("Dummy Text");
    child.add("Dummy Text");
    child.add("Dummy Text");
    childItem.add(child);
    /**
     * 
     */
    child = new ArrayList<String>();
    child.add("Dummy Text");
    child.add("Dummy Text");
    child.add("Dummy Text");
    child.add("Dummy Text");
    childItem.add(child);
}

@Override
public boolean onChildClick(ExpandableListView parent, View v,
        int groupPosition, int childPosition, long id) {
    Toast.makeText(MainActivity.this, "Clicked On Child",
            Toast.LENGTH_SHORT).show();
    return true;
}
     }

Adapter类:

public class NewAdapter extends BaseExpandableListAdapter {

public ArrayList<String> groupItem, tempChild;
public ArrayList<Object> Childtem = new ArrayList<Object>();
public LayoutInflater minflater;
public Activity activity;

public NewAdapter(ArrayList<String> grList, ArrayList<Object> childItem) {
    groupItem = grList;
    this.Childtem = childItem;
}

public void setInflater(LayoutInflater mInflater, Activity act) {
    this.minflater = mInflater;
    activity = act;
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    return null;
}

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

@SuppressWarnings("unchecked")
@Override
public View getChildView(int groupPosition, final int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {
    tempChild = (ArrayList<String>) Childtem.get(groupPosition);
    TextView text = null;
    if (convertView == null) {
        convertView = minflater.inflate(R.layout.childrow, null);
    }
    text = (TextView) convertView.findViewById(R.id.textView1);
    text.setText(tempChild.get(childPosition));
    convertView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(activity, tempChild.get(childPosition),
                    Toast.LENGTH_SHORT).show();
        }
    });
    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    return ((ArrayList<String>) Childtem.get(groupPosition)).size();
}

@Override
public Object getGroup(int groupPosition) {
    return null;
}

@Override
public int getGroupCount() {
    return groupItem.size();
}

@Override
public void onGroupCollapsed(int groupPosition) {
    super.onGroupCollapsed(groupPosition);
}

@Override
public void onGroupExpanded(int groupPosition) {
    super.onGroupExpanded(groupPosition);
}

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = minflater.inflate(R.layout.grouprow, null);
    }
    ((CheckedTextView) convertView).setText(groupItem.get(groupPosition));
    ((CheckedTextView) convertView).setChecked(isExpanded);
    return convertView;
}

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

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

1 个答案:

答案 0 :(得分:1)

您必须致电setContentView,传递包含身份为ExpandableListView的{​​{1}}的布局。
然后,您可以@android:id/list拨打ExpandableListView
顺便说一句,因为您可以按XML自定义getExpandableListView(),所以您不必调用此方法,只会使用ExpandableListView
您的活动无需实施setListAdapter()并致电OnChildClickListener,您只需覆盖setOnChildClickListener的方法onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id)
请参阅此link以获得布局示例 并且在接下来的几次中,不要忘记发布您获得的异常日志。