2012-09-20 63 views
0

我需要爲每個組使用Android ExpandableListView和自定義佈局(組有一個孩子)。爲了做到這一點,我創建了一個擴展BaseExpandableListAdapter的類。在getChildView方法上,我創建了每個孩子的視圖。有用。當我點擊一個組時,它的孩子會有真正的佈局。但是,在調試過程中,我發現它多次觸發getchildview方法,所以顯示子進程太慢。因爲每個孩子都有很多操作(數據庫操作,動態子佈局...)。Android BaseExpandableListAdapter多次觸發GetChildView方法

有一個小項目,類似於我的項目(邏輯和代碼結構相同,只有佈局代碼和創建每個子視圖的代碼是不同的)。

public class MainActivity extends Activity { 

private ArrayList<String> groups; 
private ArrayList<ArrayList<ArrayList<String>>> childs; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_main); 

    ExpandableListView l = (ExpandableListView)findViewById(R.id.expandableListView1); 

    loadData(); 


    final myExpandableAdapter adapter = new myExpandableAdapter(this, groups, childs); 
    l.setAdapter(adapter); 


} 

public class myExpandableAdapter extends BaseExpandableListAdapter { 

    private ArrayList<String> groups; 

    private ArrayList<ArrayList<ArrayList<String>>> children; 

    private Context context; 

    public myExpandableAdapter(Context context, ArrayList<String> groups, ArrayList<ArrayList<ArrayList<String>>> children) { 
     this.context = context; 
     this.groups = groups; 
     this.children = childs; 
    } 


    @Override 
    public boolean areAllItemsEnabled() 
    { 
     return true; 
    } 


    public ArrayList<String> getChild(int groupPosition, int childPosition) { 
     return children.get(groupPosition).get(childPosition); 
    } 

    public long getChildId(int groupPosition, int childPosition) { 
     return childPosition; 
    } 


    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,View convertView, ViewGroup parent) { 

     String child=(String)getChild(groupPosition, childPosition).get(0); 
     switch (groupPosition){ 

     case 0: 
      LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.child_row, null); 
      TextView tvPlayerName = (TextView) convertView.findViewById(R.id.tvPlayerName); 
      tvPlayerName.setText(child); 

      break; 

     case 1: 
      LayoutInflater inflater1 = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater1.inflate(R.layout.child_row1, null); 
      TextView tvPlayerName1 = (TextView) convertView.findViewById(R.id.tvPlayerName); 
      tvPlayerName1.setText(child); 
      Button btn=(Button)convertView.findViewById(R.id.button1); 
      btn.setOnClickListener(new OnClickListener() { 

       public void onClick(View v) { 
        TextView tv=(TextView)findViewById(R.id.tv); 
        tv.setText("Clicked"); 
       } 
      }); 

      break; 
     case 2: 
      LayoutInflater inflater2 = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater2.inflate(R.layout.child_row1, null); 
      TextView tvPlayerName12 = (TextView) convertView.findViewById(R.id.tvPlayerName); 
      tvPlayerName12.setText(child); 
      Button btn2=(Button)convertView.findViewById(R.id.button1); 
      btn2.setOnClickListener(new OnClickListener() { 

       public void onClick(View v) { 
        TextView tv=(TextView)findViewById(R.id.tv); 
        tv.setText("Clicked"); 
       } 
      }); 

      break; 
    } 
     return convertView; 

    } 

    public int getChildrenCount(int groupPosition) { 
     return children.get(groupPosition).size(); 
    } 

    public String getGroup(int groupPosition) { 
     return groups.get(groupPosition); 
    } 

    public int getGroupCount() { 
     return groups.size(); 
    } 

    public long getGroupId(int groupPosition) { 
     return groupPosition; 
    } 

    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 

     String group = (String) getGroup(groupPosition); 

     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.expandablelistview_group, null); 
     } 

     TextView grouptxt = (TextView) convertView.findViewById(R.id.TextViewGroup); 

     grouptxt.setText(group); 

     return convertView; 
    } 

    public boolean hasStableIds() { 
     return true; 
    } 

    public boolean isChildSelectable(int arg0, int arg1) { 
     return true; 
    } 

} 

private void loadData(){ 
    groups= new ArrayList<String>(); 
    childs= new ArrayList<ArrayList<ArrayList<String>>>(); 

    groups.add("Group 1"); 
    groups.add("Group 2"); 
    groups.add("Group 3"); 

    childs.add(new ArrayList<ArrayList<String>>()); 
    childs.get(0).add(new ArrayList<String>()); 
    childs.get(0).get(0).add("Child 1 group 1"); 


    childs.add(new ArrayList<ArrayList<String>>()); 
    childs.get(1).add(new ArrayList<String>()); 
    childs.get(1).get(0).add("Child 1 group 2"); 


    childs.add(new ArrayList<ArrayList<String>>()); 
    childs.get(2).add(new ArrayList<String>()); 
    childs.get(2).get(0).add("Child 1 group 3"); 

}} 

在調試過程中,如果我點擊任何組,它會來到String child=(String)getChild(groupPosition, childPosition).get(0);行。在break; }return convertView;之後,它會一次又一次地出現,然後顯示佈局。

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<ExpandableListView 
    android:id="@+id/expandableListView1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_centerVertical="true" 
    android:layout_marginLeft="34dp" > 
</ExpandableListView> 

expandablelistview_group.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<TextView 
    android:id="@+id/TextViewGroup" 
    android:layout_width="wrap_content" 
    android:layout_height="50px" 
    android:layout_marginLeft="50px" 
    android:gravity="center_vertical" 
> 
</TextView> 

child_row.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/tvPlayerName" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="TextView" /> 

<CheckBox 
    android:id="@+id/checkBox1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="CheckBox" /> 

childrow1.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="horizontal" > 

    <TextView 
    android:id="@+id/tv" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Row1" /> 

<TextView 
    android:id="@+id/tvPlayerName" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="TextView" /> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Button" /> 

是否有任何建議,以解決這個問題。

感謝

回答

2

,而不是膨脹從getChildView使用持有人的觀點read here

1

試着改變你的ExpandableListView xml和,而不是機器人:layout_height = 「WRAP_CONTENT」,把機器人:layout_height =」 FILL_PARENT

相關問題