2

Я разрабатываю приложение для Android, поэтому я пытаюсь использовать ExpandableListView для использования двух разных макетов для каждого ребенка. Но у меня проблема, и я не могу ее исправить. Я думал, что проблема в том, что я не получил правильного ребенка. Оба ребенка одновременно отображают два макета.Как разместить различные макеты для дочерних элементов в 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; 
} 

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> 

expandablelist_child_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="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> 

Ниже вы можете увидеть запуск первого и второго ребенка.

+0

Аналогичная проблема, с которой я столкнулся ... удачи здесь? – CoDe

ответ

0
int result = 0; 
if (childPosition == getChildrenCount(groupPosition)-1)//logical fail ? 
result = 1; 

пытается вернуть "child_pos"

getChildrenCount даст у everey раза, то же самое значение. u есть 2 ребенка -1 = 1

кстати. проверить эту инициализацию

ArrayList<ExpandListChild> chList = groups.get(groupPosition).getItems(); 
-1

Вы выполняете следующий фрагмент кода только в случае, если вид является нулевым, что неправильно и должен делать, что независимо от того, является ли нуль/не нулевой вид.

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"); 
} 
+0

Затем вы раздуваете каждый раз, и ваши результаты будут очень плохими –