Android 可扩展列表

Android 可扩展列表,android,android-listview,expandablelistview,expandablelistadapter,Android,Android Listview,Expandablelistview,Expandablelistadapter,我正在尝试做一个可扩展列表,但子元素必须在一行中只有5个元素,我正在尝试做适配器,但我不知道如何做到这一点。这是我的适配器代码: public class ExpandableListAdapter extends BaseExpandableListAdapter { private Context _context; private List<String> _listDataHeader; // header titles // child data

我正在尝试做一个可扩展列表,但子元素必须在一行中只有5个元素,我正在尝试做适配器,但我不知道如何做到这一点。这是我的适配器代码:

 public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    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 ChildModel childText = (ChildModel) 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);
        TextView txtListChilddos = (TextView) convertView
                .findViewById(R.id.textView1);
        TextView txtListChildtres = (TextView) convertView
                .findViewById(R.id.textView2);
        TextView txtListChildcuatro = (TextView) convertView
                .findViewById(R.id.textView3);
        TextView txtListChildcinco = (TextView) convertView
                .findViewById(R.id.textView4);


        txtListChild.setText(childText.getDate());
        txtListChilddos.setText(childText.getSong());
        txtListChildtres.setText(childText.getDuration());
        txtListChildcuatro.setText(childText.getArtist());


        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);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        return convertView;
    }

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

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

}

非常感谢您

尝试一下,但我认为您的元素将具有可滚动的视图:


比如说,您有一些项目作为子项,其中每个项目都有5个字符串,您希望显示它们。首先,创建您的子数据模型:

  public class ChildModel {
     String date;
     String name;
     String song;
     String duration;
     String artist;
     // constructor and getter setters 
  }
现在,您必须实现您的适配器:

 public View getChildView(int groupPosition, final int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {

    final ChildModel child = (ChildModel) getChild(groupPosition, childPosition);

    // init yout layout
    ... 

    TextView txtListChildDate = (TextView) convertView
            .findViewById(R.id.item_date_text_view);
    TextView txtListChildName = (TextView) convertView
            .findViewById(R.id.item_name_text_view);
    TextView txtListChildSong = (TextView) convertView
            .findViewById(R.id.item_song_text_view);
    TextView txtListChildDuration = (TextView) convertView
            .findViewById(R.id.item_duration_text_view);
    TextView txtListChildArtist = (TextView) convertView
            .findViewById(R.id.item_artist_text_view);


    txtListChildDate.setText(child.getDate());
    txtListChildName.setText(child.getName();
    txtListChildSong.setText(child.getSong());
    txtListChildDuration.setText(child.getDuration());
    txtListChildArtist.setText(child.getArtist());

    return convertView;
}

创建两个自定义对象:

class Group {
 String groupname;
 ArrayList<Child> childsarray;
 }

class Child{
 String item1;
 String item2;
 String item3;
 String item4;
 String item5;
}

将组数组传递给适配器。

如何创建新对象?这样地?michild=新儿童模型;michild.setdate;michild.setNamename;薛松松;michild.setDurationduration;米奇尔德·塞塔提斯特;对您还可以向构造函数中添加参数,而不是调用setter方法;,你能帮我吗?更改私有HashMap\u listDataChild;到私有HashMap _listDataChild;
 public View getChildView(int groupPosition, final int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {

    final ChildModel child = (ChildModel) getChild(groupPosition, childPosition);

    // init yout layout
    ... 

    TextView txtListChildDate = (TextView) convertView
            .findViewById(R.id.item_date_text_view);
    TextView txtListChildName = (TextView) convertView
            .findViewById(R.id.item_name_text_view);
    TextView txtListChildSong = (TextView) convertView
            .findViewById(R.id.item_song_text_view);
    TextView txtListChildDuration = (TextView) convertView
            .findViewById(R.id.item_duration_text_view);
    TextView txtListChildArtist = (TextView) convertView
            .findViewById(R.id.item_artist_text_view);


    txtListChildDate.setText(child.getDate());
    txtListChildName.setText(child.getName();
    txtListChildSong.setText(child.getSong());
    txtListChildDuration.setText(child.getDuration());
    txtListChildArtist.setText(child.getArtist());

    return convertView;
}
class Group {
 String groupname;
 ArrayList<Child> childsarray;
 }

class Child{
 String item1;
 String item2;
 String item3;
 String item4;
 String item5;
}