我使用Expandable Listview显示两组内容,列表总是像图片A 那样展开。 当我在我的设备上运行它时,我发现了一个问题。 当我跑步时,它只显示列表的一半,如图片B ,所以我向下滚动以查看列表的其余部分,但我得到的是第二组中的一些孩子来自第一组,如图片C 。
我检查了我的代码,我认为没有错误或问题。 这是我的代码
这个活动
public class MainActivity extends ActionBarActivity {
ExpandableListView lv;
ParentListAdapter listAdapter;
List<HeaderObject> listDataHeader;
List<OpiniObject> opini;
List<OpiniObject> related;
HashMap<HeaderObject, List<OpiniObject>> listDataChild;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
opini = new ArrayList<OpiniObject>();
related = new ArrayList<OpiniObject>();
lv = (ExpandableListView) findViewById(R.id.expandLV);
prepareListData();
}
private void prepareListData() {
listDataHeader = new ArrayList<HeaderObject>();
listDataChild = new HashMap<HeaderObject, List<OpiniObject>>();
HeaderObject itemData1 = new HeaderObject();
itemData1.setOwner("Your Name");
itemData1.setQuestion("Whats up");
listDataHeader.add(itemData1);
HeaderObject itemData2 = new HeaderObject();
itemData2.setTitle("Related");
listDataHeader.add(itemData2);
opini.add(new OpiniObject("name 1","Lorem \n ipsum \n hahaha \n test \n data"));
opini.add(new OpiniObject("name 2","content 2 \n jus \n test \n content"));
opini.add(new OpiniObject("name 3","content 3 \n bla \n bla \n bla"));
related.add(new OpiniObject("text 1"));
related.add(new OpiniObject("text 2"));
related.add(new OpiniObject("text 3"));
related.add(new OpiniObject("text 4"));
related.add(new OpiniObject("text 5"));
related.add(new OpiniObject("text 6"));
related.add(new OpiniObject("text 7"));
related.add(new OpiniObject("text 8"));
listDataChild.put(listDataHeader.get(0), opini);
listDataChild.put(listDataHeader.get(1), related);
listAdapter = new ParentListAdapter(this, listDataHeader, listDataChild);
lv.setAdapter(listAdapter);
}
}
这是我的适配器
public class ParentListAdapter extends BaseExpandableListAdapter{
private Context context;
private List<HeaderObject> listDataHeader;
private HashMap<HeaderObject, List<OpiniObject>> listDataChild;
public ParentListAdapter(Context context,
List<HeaderObject> listDataHeader,
HashMap<HeaderObject, List<OpiniObject>> listDataChild) {
super();
this.context = context;
this.listDataHeader = listDataHeader;
this.listDataChild = listDataChild;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return this.listDataChild.get(this.listDataHeader.get(groupPosition)).get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.listview2, null);
}
LinearLayout container = (LinearLayout) convertView.findViewById(R.id.containerOpini);
TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);
TextView txtUser = (TextView) convertView.findViewById(R.id.lblUserOpini);
TextView txtOpini = (TextView) convertView.findViewById(R.id.lblIsiOpini);
OpiniObject opini = (OpiniObject) getChild(groupPosition, childPosition);
if(groupPosition==0){
container.setVisibility(View.VISIBLE);
txtListChild.setVisibility(View.GONE);
txtUser.setText(opini.getName());
txtOpini.setText(opini.getContent());
}else{
txtListChild.setText(opini.getText());
txtListChild.setTypeface(null, Typeface.BOLD);
}
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return this.listDataChild.get(this.listDataHeader.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return this.listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return this.listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.listview1, null);
}
ExpandableListView eLV = (ExpandableListView) parent;
eLV.expandGroup(groupPosition);
LinearLayout container = (LinearLayout) convertView.findViewById(R.id.PertanyaanHeader);
TextView lblListHeader = (TextView) convertView.findViewById(R.id.lblListHeader);
TextView lblOwner = (TextView) convertView.findViewById(R.id.lblUser);
TextView lblQuestion = (TextView) convertView.findViewById(R.id.lblTanya);
HeaderObject item = (HeaderObject) getGroup(groupPosition);
if(groupPosition==0){
container.setVisibility(View.VISIBLE);
lblListHeader.setVisibility(View.GONE);
lblOwner.setText(item.getOwner());
lblQuestion.setText(item.getQuestion());
}else{
lblListHeader.setText(item.getTitle());
lblListHeader.setTypeface(null, Typeface.BOLD);
}
return convertView;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
}
任何人都可以帮助我吗?也许我错过了一些非常重要的东西?
谢谢。
答案 0 :(得分:1)
由于android recycling mechanism,您有此结果。
要解决此问题,只需更改此代码:
if(groupPosition==0){
container.setVisibility(View.VISIBLE);
txtListChild.setVisibility(View.GONE);
txtUser.setText(opini.getName());
txtOpini.setText(opini.getContent());
}else{
txtListChild.setText(opini.getText());
txtListChild.setTypeface(null, Typeface.BOLD);
}
到此:
if(groupPosition==0){
container.setVisibility(View.VISIBLE);
txtListChild.setVisibility(View.GONE);
txtUser.setText(opini.getName());
txtOpini.setText(opini.getContent());
}else{
container.setVisibility(View.GONE); // changes here
txtListChild.setVisibility(View.VISIBLE);// changes here
txtListChild.setText(opini.getText());
txtListChild.setTypeface(null, Typeface.BOLD);
}