Android 如何在ExpandableListView中为子项放置不同的布局?
我开发了一个Android应用程序,所以我尝试使用Android 如何在ExpandableListView中为子项放置不同的布局?,android,android-layout,expandablelistview,expandablelistadapter,get-childitem,Android,Android Layout,Expandablelistview,Expandablelistadapter,Get Childitem,我开发了一个Android应用程序,所以我尝试使用ExpandableListView为每个孩子使用两种不同的布局。但我有个问题,似乎无法解决。我想问题是我没有找到合适的孩子。两个子项同时显示两个布局 这是我的类适配器 public class ExpandListAdapter extends BaseExpandableListAdapter { private Context context; private ArrayList<ExpandListGroup> groups
ExpandableListView
为每个孩子使用两种不同的布局。但我有个问题,似乎无法解决。我想问题是我没有找到合适的孩子。两个子项同时显示两个布局
这是我的类适配器
public class ExpandListAdapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<ExpandListGroup> groups;
public ExpandListAdapter(Context context, ArrayList<ExpandListGroup> groups) {
this.context = context;
this.groups = groups;
}
public void addItem(ExpandListChild item, ExpandListGroup group) {
if (!groups.contains(group)) {
groups.add(group);
}
int index = groups.indexOf(group);
ArrayList<ExpandListChild> ch = groups.get(index).getItems();
ch.add(item);
groups.get(index).setItems(ch);
}
public Object getChild(int groupPosition, int childPosition) {
//TODO Auto-generated method stub
ArrayList<ExpandListChild> chList = groups.get(groupPosition).getItems();
return chList.get(childPosition);
}
@Override
public int getChildTypeCount() {
return 2;
}
public long getChildId(int groupPosition, int childPosition) {
//TODO Auto-generated method stub
//return childPosition;
return (long)( groupPosition*50+childPosition );
}
@Override
public int getChildType(int groupPosition, int childPosition)
{
int result = 0;
if (childPosition == getChildrenCount(groupPosition)-1)
result = 1;
return result;
}
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view,
ViewGroup parent) {
TextView textView = null;
ExpandListChild child = (ExpandListChild) getChild(groupPosition, childPosition);
/* if (view == null) {
if(childPosition == 0){
LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
view = infalInflater.inflate(R.layout.expandlist_child_item, null);}
else if (childPosition == 1){
LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
view = infalInflater.inflate(R.layout.child2, null);
}
}*/
if (view == null) {
int itemType = getChildType(groupPosition,childPosition);
LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
//view = infalInflater.inflate(R.layout.expandlist_child_item, null);
if(itemType == 0)
{
view = infalInflater.inflate(R.layout.expandlist_child_item, null);
textView = (TextView) view.findViewById(R.id.tvChild);
textView.setText("0");
//textView.setTag(child.getTag());
}
else
{ view = infalInflater.inflate(R.layout.child2, null);
textView = (TextView) view.findViewById(R.id.tvChild1);
textView.setText("1");
textView.setTag(child.getTag());}
/*
switch (itemType) {
case 0:
{ view = infalInflater.inflate(R.layout.expandlist_child_item, null);
textView = (TextView) view.findViewById(R.id.tvChild);
textView.setText("0");
//textView.setTag(child.getTag());
break;}
case 1:
{ view = infalInflater.inflate(R.layout.child2, null);
textView = (TextView) view.findViewById(R.id.tvChild1);
textView.setText("1");
textView.setTag(child.getTag());
break;}
}
*/
//textView.setPadding(30,20,30,20);
}
//TODO Auto-generated method stub
return view;
}
public int getChildrenCount(int groupPosition) {
//TODO Auto-generated method stub
ArrayList<ExpandListChild> chList = groups.get(groupPosition).getItems();
return chList.size();
}
public Object getGroup(int groupPosition) {
//TODO Auto-generated method stub
return groups.get(groupPosition);
}
public int getGroupCount() {
//TODO Auto-generated method stub
return groups.size();
}
public long getGroupId(int groupPosition) {
//TODO Auto-generated method stub
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isLastChild, View view,
ViewGroup parent) {
ExpandListGroup group = (ExpandListGroup) getGroup(groupPosition);
if (view == null) {
LayoutInflater inf = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
view = inf.inflate(R.layout.expandlist_group_item, null);
}
TextView tv = (TextView) view.findViewById(R.id.tvGroup);
tv.setText(group.getName());
//TODO Auto-generated method stub
return view;
}
public boolean hasStableIds() {
//TODO Auto-generated method stub
return true;
}
public boolean isChildSelectable(int arg0, int arg1) {
//TODO Auto-generated method stub
return true;
}
公共类ExpandListAdapter扩展了BaseExpandableListAdapter{
私人语境;
私有ArrayList组;
公共ExpandListAdapter(上下文、ArrayList组){
this.context=上下文;
这个组=组;
}
公共无效附加项(ExpandListChild项,ExpandListGroup组){
如果(!groups.contains(组)){
组。添加(组);
}
int index=组。indexOf(组);
ArrayList ch=groups.get(index.getItems();
总附录(项目);
groups.get(index).setItems(ch);
}
公共对象getChild(int groupPosition,int childPosition){
//TODO自动生成的方法存根
ArrayList chList=groups.get(groupPosition.getItems();
返回chList.get(childPosition);
}
@凌驾
public int getChildTypeCount(){
返回2;
}
公共长getChildId(int-groupPosition,int-childPosition){
//TODO自动生成的方法存根
//返回子位置;
返回(长)(groupPosition*50+childPosition);
}
@凌驾
public int getChildType(int groupPosition,int childPosition)
{
int结果=0;
if(childPosition==getChildrenCount(groupPosition)-1)
结果=1;
返回结果;
}
公共视图getChildView(int groupPosition、int childPosition、boolean isLastChild、View视图、,
视图组(父级){
TextView TextView=null;
ExpandListChild=(ExpandListChild)getChild(groupPosition,childPosition);
/*如果(视图==null){
if(childPosition==0){
LayoutInflater infalInflater=(LayoutInflater)context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE);
视图=infalInflater.flate(R.layout.expandlist_child_item,null);}
else if(childPosition==1){
LayoutInflater infalInflater=(LayoutInflater)context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE);
视图=充气器充气(R.layout.child2,空);
}
}*/
如果(视图==null){
int itemType=getChildType(groupPosition,childPosition);
LayoutInflater infalInflater=(LayoutInflater)context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE);
//视图=infalInflater.充气(R.layout.expandlist\u child\u项,空);
如果(itemType==0)
{
视图=infalInflater.充气(R.layout.expandlist\u child\u项,空);
textView=(textView)view.findViewById(R.id.tvChild);
textView.setText(“0”);
//setTag(child.getTag());
}
其他的
{view=infalInflater.flate(R.layout.child2,null);
textView=(textView)view.findViewById(R.id.tvChild1);
textView.setText(“1”);
textView.setTag(child.getTag());}
/*
开关(项目类型){
案例0:
{view=infalInflater.flate(R.layout.expandlist\u child\u item,null);
textView=(textView)view.findViewById(R.id.tvChild);
textView.setText(“0”);
//setTag(child.getTag());
中断;}
案例1:
{view=infalInflater.flate(R.layout.child2,null);
textView=(textView)view.findViewById(R.id.tvChild1);
textView.setText(“1”);
setTag(child.getTag());
中断;}
}
*/
//设置填充(30,20,30,20);
}
//TODO自动生成的方法存根
返回视图;
}
公共整数getChildrenCount(整数组位置){
//TODO自动生成的方法存根
ArrayList chList=groups.get(groupPosition.getItems();
返回chList.size();
}
公共对象getGroup(int-groupPosition){
//TODO自动生成的方法存根
返回groups.get(groupPosition);
}
public int getGroupCount(){
//TODO自动生成的方法存根
返回组。size();
}
公共长getGroupId(int-groupPosition){
//TODO自动生成的方法存根
返回组位置;
}
public View getGroupView(int-groupPosition、boolean-isLastChild、View-View、,
视图组(父级){
ExpandListGroup=(ExpandListGroup)getGroup(groupPosition);
如果(视图==null){
LayoutInflater inf=(LayoutInflater)context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE);
view=inf.inflate(R.layout.expandlist\u group\u项,空);
}
TextView tv=(TextView)view.findviewbyd(R.id.tvGroup);
tv.setText(group.getName());
//TODO自动生成的方法存根
返回视图;
}
公共布尔表ID(){
//TODO自动生成的方法存根
返回true;
}
公共布尔值isChildSelectable(int arg0,int arg1){
//TODO自动生成的方法存根
返回true;
}
child2.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tvChild1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@color/Black"
android:textSize="17sp" >
</TextView>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/b1" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/checkboxh" />
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="55dip"
android:background="@color/ExpandChildBackground"
android:orientation="vertical" >
<TextView
android:id="@+id/tvChild"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@color/Black"
android:textSize="17sp" >
</TextView>
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/b1" />
</LinearLayout>
expandablelist\u child\u item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tvChild1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@color/Black"
android:textSize="17sp" >
</TextView>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/b1" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/checkboxh" />
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="55dip"
android:background="@color/ExpandChildBackground"
android:orientation="vertical" >
<TextView
android:id="@+id/tvChild"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@color/Black"
android:textSize="17sp" >
</TextView>
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/b1" />
</LinearLayout>
下面可以看到第一个和第二个孩子的跑步
int result = 0;
if (childPosition == getChildrenCount(groupPosition)-1)//logical fail ?
result = 1;
尝试返回“child_pos”
getChildrenCount每次都会给你相同的值。你有2个孩子-1=1
顺便说一句,检查这个初始化
ArrayList<ExpandListChild> chList = groups.get(groupPosition).getItems();
ArrayList chList=groups.get(groupPosition.getItems();
尝试返回“child_pos”
getChildrenCount每次都会给你相同的值。你有2个孩子-1=1
顺便说一句,检查这个初始化
ArrayList<ExpandListChild> chList = groups.get(groupPosition).getItems();
ArrayList chList=groups.get(groupPosition.getItems();
只有在视图为null的情况下才执行以下代码段,这是错误的,无论视图是否为null,都应执行该操作
if(itemType == 0)
{
view = infalInflater.inflate(R.layout.expandlist_child_item, null);
textView = (TextView) view.findViewById(R.id.tvChild);
textView.setText("0");
//textView.setTag(child.getTag());
}
else
{ view = infalInflater.inflate(R.layout.child2, null);
textView = (TextView) view.findViewById(R.id.tvChild1);
textView.setText("1");
}
只有在视图为null的情况下才执行以下代码段,这是错误的,无论视图是否为null,都应该执行该操作
if(itemType == 0)
{
view = infalInflater.inflate(R.layout.expandlist_child_item, null);
textView = (TextView) view.findViewById(R.id.tvChild);
textView.setText("0");
//textView.setTag(child.getTag());
}
else
{ view = infalInflater.inflate(R.layout.child2, null);
textView = (TextView) view.findViewById(R.id.tvChild1);
textView.setText("1");
}
然后你每次都在膨胀,你的表现会很差。然后你每次都在膨胀,你的表现会很差。我面临着类似的问题。这里运气好吗?我面临着类似的问题。这里运气好吗?