首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >可扩展列表

可扩展列表
EN

Stack Overflow用户
提问于 2014-11-29 20:07:43
回答 3查看 121关注 0票数 0

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

代码语言:javascript
复制
 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个元素:

代码语言:javascript
复制
 List<String> mychildlist;

 mychildlist.add(date);

                            mychildlist.add(name);
                            mychildlist.add(song);
                            mychildlist.add(duration);
                            mychildlist.add(artist);

我喜欢这样的单子:

代码语言:javascript
复制
+rock
 --------------
 George Michael
 song´s title
 5 min
 lp
 --------------
 U2
 song´s title
 4 min
 cd
 --------------

非常感谢

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-11-29 20:21:49

假设,您有一些项目作为子,其中每一个有5个字符串在里面,你想要显示他们。首先,创建您的子数据模型:

代码语言:javascript
复制
  public class ChildModel {
     String date;
     String name;
     String song;
     String duration;
     String artist;
     // constructor and getter setters 
  }

现在,您必须实现适配器:

代码语言:javascript
复制
 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;
}
票数 0
EN

Stack Overflow用户

发布于 2014-11-29 20:16:37

尝试使用此方法,但我认为您的元素将具有一个可滚动的视图:

Is possibe displays child views in horizontally of expandable ListView?

票数 0
EN

Stack Overflow用户

发布于 2014-11-29 20:30:23

创建两个自定义对象:

代码语言:javascript
复制
class Group {
 String groupname;
 ArrayList<Child> childsarray;
 }

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

将组数组传递给适配器。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27206565

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档