我正在尝试做一个可扩展的列表但是孩子必须只有一行中有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个元素:
List<String> mychildlist;
mychildlist.add(date);
mychildlist.add(name);
mychildlist.add(song);
mychildlist.add(duration);
mychildlist.add(artist);
我喜欢这样的列表:
+rock
--------------
George Michael
song´s title
5 min
lp
--------------
U2
song´s title
4 min
cd
--------------
非常感谢
答案 0 :(得分:0)
尝试使用此功能,但我认为您的元素将具有可滚动的视图:
Is possibe displays child views in horizontally of expandable ListView?
答案 1 :(得分:0)
比方说,你有一些作为孩子的物品,并且每个物品里面都有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;
}
答案 2 :(得分:0)
创建两个自定义对象:
class Group {
String groupname;
ArrayList<Child> childsarray;
}
class Child{
String item1;
String item2;
String item3;
String item4;
String item5;
}
将groups数组传递给适配器。