使用两个不同的布局中ExpandableListView子项 [英] Using two different layouts for child items in ExpandableListView
问题描述
这是我在计算器的第一个问题。
我试图做 ExpandableListView
来请根据项目类型两个不同的布局。这时我犯了基本的功能,除了一件事:
在列表视图
选择了一组后,显示子项的文字错误(不同项目的文本是混合)
我的适配器
源$ C $ c为以下内容:
SimpleExpandableListAdapter适配器=新SimpleExpandableListAdapter(
本,
groupData,
com.oleg.mart.foreign.R.layout.group_row,
groupFrom,
新的INT [] {} com.oleg.mart.foreign.R.id.Group,
childData,
R.layout.simple_list_item_1,
childFrom,
childTo)
{
@覆盖
公众诠释getChildTypeCount(){
返回2;
}
/ * @覆盖
公众诠释getChildrenCount(INT groupPosition)
{
返回childData.get(groupPosition).size();
} * /
@覆盖
公众诠释getChildType(INT groupPosition,INT childPosition)
{
INT结果为0;
如果(childPosition == getChildrenCount(groupPosition) - 1)
结果= 1;
返回结果;
}
@覆盖
公共查看getChildView(
INT groupPosition,INT childPosition,
布尔isLastChild,
查看convertView,父母的ViewGroup)
{
如果(convertView == NULL){
TextView中的TextView = NULL;
LayoutInflater充气=(LayoutInflater)getSystemService(getBaseContext()LAYOUT_INFLATER_SERVICE);
INT ITEMTYPE = getChildType(groupPosition,childPosition);
会将myText的String = childData.get(groupPosition)获得(childPosition)获得(章);
开关(ITEMTYPE){
情况下0:
convertView = inflater.inflate(com.oleg.mart.foreign.R.layout.child_row_notlast,NULL);
TextView的=(的TextView)convertView.findViewById(com.oleg.mart.foreign.R.id.NChild);
textView.setPadding(30,20,30,20);
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,15);
textView.setText(Html.fromHtml(会将myText));
打破;
情况1:
convertView = inflater.inflate(com.oleg.mart.foreign.R.layout.child_row,NULL);
TextView的=(的TextView)convertView.findViewById(com.oleg.mart.foreign.R.id.Child);
textView.setPadding(30,20,30,20);
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,15);
textView.setText(Html.fromHtml(会将myText));
打破;
}
}
返回convertView;
}
};
正如你可以看到我试图设置不同的风格上的最后一个子项。可能是什么问题? 非常感谢你!
右 getChildView
方法:
的TextView TextView的= NULL;
会将myText的String = NULL;
=会将myText childData.get(groupPosition)获得(childPosition)获得(章);
如果(convertView == NULL){
LayoutInflater充气=(LayoutInflater)getSystemService(getBaseContext()LAYOUT_INFLATER_SERVICE);
INT ITEMTYPE = getChildType(groupPosition,childPosition);
开关(ITEMTYPE){
情况下0:
convertView = inflater.inflate(com.oleg.mart.foreign.R.layout.child_row_notlast,NULL);
打破;
情况1:
convertView = inflater.inflate(com.oleg.mart.foreign.R.layout.child_row,NULL);
打破;
}
}
TextView的=(的TextView)convertView.findViewById(com.oleg.mart.foreign.R.id.NChild);
textView.setPadding(30,20,30,20);
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,15);
textView.setText(Html.fromHtml(会将myText));
返回convertView;
我是pretty的确保您不返回好孩子值时convertView!=你的getChildView方法无效。 试试你的条件后,设置你的价值观,这样的:
的TextView TextView的= NULL;
如果(convertView == NULL){
LayoutInflater充气=(LayoutInflater)getSystemService(getBaseContext()LAYOUT_INFLATER_SERVICE);
INT ITEMTYPE = getChildType(groupPosition,childPosition);
会将myText的String = childData.get(groupPosition)获得(childPosition)获得(章);
开关(ITEMTYPE){
情况下0:
convertView = inflater.inflate(com.oleg.mart.foreign.R.layout.child_row_notlast,NULL);
打破;
情况1:
convertView = inflater.inflate(com.oleg.mart.foreign.R.layout.child_row,NULL);
打破;
}
}
TextView的=(的TextView)convertView.findViewById(com.oleg.mart.foreign.R.id.NChild);
textView.setPadding(30,20,30,20);
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,15);
textView.setText(Html.fromHtml(会将myText));
如果你仍然有一个问题,请把我的回答评论,告诉我,我会看看我能做些什么。
This is my first question on stackoverflow.
I'm trying to do the ExpandableListView
to use two different layouts depending on item type. At this time I made basic functionality except one thing:
after selecting a group in the listview
, the child item's text is displayed wrong (text of different items is mixed)
My adapter
source code is following:
SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
this,
groupData,
com.oleg.mart.foreign.R.layout.group_row,
groupFrom,
new int[] { com.oleg.mart.foreign.R.id.Group },
childData,
R.layout.simple_list_item_1,
childFrom,
childTo)
{
@Override
public int getChildTypeCount() {
return 2;
}
/*@Override
public int getChildrenCount(int groupPosition)
{
return childData.get(groupPosition).size();
} */
@Override
public int getChildType(int groupPosition, int childPosition)
{
int result = 0;
if (childPosition == getChildrenCount(groupPosition) - 1)
result = 1;
return result;
}
@Override
public View getChildView (
int groupPosition, int childPosition,
boolean isLastChild,
View convertView, ViewGroup parent)
{
if (convertView == null) {
TextView textView = null;
LayoutInflater inflater = (LayoutInflater)getSystemService(getBaseContext().LAYOUT_INFLATER_SERVICE);
int itemType = getChildType(groupPosition,childPosition);
String myText = childData.get(groupPosition).get(childPosition).get("chapter");
switch (itemType) {
case 0:
convertView = inflater.inflate(com.oleg.mart.foreign.R.layout.child_row_notlast, null);
textView = (TextView)convertView.findViewById(com.oleg.mart.foreign.R.id.NChild);
textView.setPadding(30,20,30,20);
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
textView.setText(Html.fromHtml(myText));
break;
case 1:
convertView = inflater.inflate(com.oleg.mart.foreign.R.layout.child_row, null);
textView = (TextView)convertView.findViewById(com.oleg.mart.foreign.R.id.Child);
textView.setPadding(30,20,30,20);
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
textView.setText(Html.fromHtml(myText));
break;
}
}
return convertView;
}
};
As you can see I'm trying to set a different style on the last child item. What could be wrong? Thank you very much!
Right getChildView
method:
TextView textView = null;
String myText = null;
myText = childData.get(groupPosition).get(childPosition).get("chapter");
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater)getSystemService(getBaseContext().LAYOUT_INFLATER_SERVICE);
int itemType = getChildType(groupPosition,childPosition);
switch (itemType) {
case 0:
convertView = inflater.inflate(com.oleg.mart.foreign.R.layout.child_row_notlast, null);
break;
case 1:
convertView = inflater.inflate(com.oleg.mart.foreign.R.layout.child_row, null);
break;
}
}
textView = (TextView)convertView.findViewById(com.oleg.mart.foreign.R.id.NChild);
textView.setPadding(30,20,30,20);
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
textView.setText(Html.fromHtml(myText));
return convertView;
I'm pretty sure that you are not returning the good child values when convertView != null on your getChildView method. Try to set your values after your condition, like that :
TextView textView = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater)getSystemService(getBaseContext().LAYOUT_INFLATER_SERVICE);
int itemType = getChildType(groupPosition,childPosition);
String myText = childData.get(groupPosition).get(childPosition).get("chapter");
switch (itemType) {
case 0:
convertView = inflater.inflate(com.oleg.mart.foreign.R.layout.child_row_notlast, null);
break;
case 1:
convertView = inflater.inflate(com.oleg.mart.foreign.R.layout.child_row, null);
break;
}
}
textView = (TextView)convertView.findViewById(com.oleg.mart.foreign.R.id.NChild);
textView.setPadding(30,20,30,20);
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
textView.setText(Html.fromHtml(myText));
If you still have a problem with that, please put a comment on my answer to tell me and I will see what I can do.
这篇关于使用两个不同的布局中ExpandableListView子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!