3

我試圖以個性化的ExpandableLisTviewExpandableList查看不擴大

創建

  • Iterator.Xml:隨着ExpandableList查看
  • group.xml:一個相對佈局與簡單的TextView和一個Button
  • Child.xml:只有一個TextView的相對佈局

類:

package Android.MyExapandableListView; 

import android.app.Activity; 
import android.app.ExpandableListActivity; 
import android.os.Bundle; 

import android.view.ContextMenu; 
import android.view.LayoutInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.ContextMenu.ContextMenuInfo; 
import android.widget.BaseExpandableListAdapter; 
import android.widget.ExpandableListAdapter; 
import android.widget.ExpandableListView; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.widget.ExpandableListView.ExpandableListContextMenuInfo; 

public class main extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.iterator); 
    ExpandableListView lv = 
     (ExpandableListView) this.findViewById(R.id.ExpandableListView); 
    MyExpandableListAdapter questions = new MyExpandableListAdapter(); 
    lv.setAdapter(questions); 

    } 

    public class MyExpandableListAdapter extends BaseExpandableListAdapter { 
    private LayoutInflater inflater = null; 
    private String[] groups = { "People Names", "Dog Names", "Cat Names", 
     "Fish Names" }; 
    private String[][] children = { { "Arnold", "Barry", "Chuck", "David" }, 
     { "Ace", "Bandit", "Cha-Cha", "Deuce" }, { "Fluffy", "Snuggles" }, 
     { "Goldy", "Bubbles" } }; 

    public MyExpandableListAdapter(){ 
     this.inflater = main.this.getLayoutInflater(); 
    } 

    public Object getChild(int groupPosition, int childPosition) { 
     return children[groupPosition][childPosition]; 
    } 

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

    public int getChildrenCount(int groupPosition) { 
     return children[groupPosition].length; 
    } 

    public Object getGroup(int groupPosition) { 
     return groups[groupPosition]; 
    } 

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

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

    public View getChildView(int groupPosition, int childPosition, 
     boolean isLastChild, View convertView, ViewGroup parent) { 
     convertView = inflater.inflate(R.layout.child, null); 
     TextView question = (TextView) convertView.findViewById(R.id.nomChild); 
     question.setText(getChild(groupPosition, childPosition).toString()); 
     return convertView; 
    } 

    public View getGroupView(int groupPosition, boolean isExpanded, 
     View convertView, ViewGroup parent) { 
     // TextView textView = getGenericView(); 
     // textView.setText(getGroup(groupPosition).toString()); 
     convertView = inflater.inflate(R.layout.group, null); 
     TextView question = (TextView) convertView.findViewById(R.id.nomGroup); 
     question.setText(getGroup(groupPosition).toString()); 
     return convertView; 
    } 

    public boolean isChildSelectable(int groupPosition, int childPosition) { 
     return true; 
    } 

    public boolean hasStableIds() { 
     return true; 
    } 
    } 
} 

問題是我可以正確地獲取項目,但是當我嘗試擴展組時,它無法正常工作。

有什麼想法?


編輯:意見

Iterator.XML

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<ExpandableListView 
android:id="@+id/ExpandableListView" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_gravity="center_vertical" 
> 
    </ExpandableListView> 

</LinearLayout> 

Group.XML

<?xml version="1.0" encoding="utf-8"?> 
<AbsoluteLayout 
android:id="@+id/widget0" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
xmlns:android="http://schemas.android.com/apk/res/android" 
> 
<RelativeLayout 
android:id="@+id/widget29" 
android:layout_width="305px" 
android:layout_height="41px" 
android:layout_x="8px" 
android:layout_y="29px"> 
<TextView 
android:id="@+id/nomGroup" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="TextView" 
android:layout_centerVertical="true" 
android:layout_centerHorizontal="true" 
> 
</TextView> 
<Button 
android:id="@+id/widget31" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="Button" 
android:layout_alignParentTop="true" 
android:layout_alignParentRight="true" 
> 
</Button> 
</RelativeLayout> 
</AbsoluteLayout> 

Child.XML

<?xml version="1.0" encoding="utf-8"?> 
<AbsoluteLayout 
android:id="@+id/widget0" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
xmlns:android="http://schemas.android.com/apk/res/android" 
> 
<RelativeLayout 
android:id="@+id/nomChild" 
android:layout_width="305px" 
android:layout_height="41px" 
android:layout_x="8px" 
android:layout_y="29px" 
> 
<TextView 
android:id="@+id/widget30" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="TextView" 
android:layout_centerVertical="true" 
android:layout_centerHorizontal="true" 
> 
</TextView> 
</RelativeLayout> 
</AbsoluteLayout> 
+1

您是否找到了解決方案?我有完全相同的問題。 – dineth 2011-09-12 01:01:28

回答

1

我們無法訪問您的視圖,因此我將您的代碼更改爲使用硬編碼的TextView,並且對我來說工作正常。看看,也許你可以發現我們看不到的錯誤。

public class Main extends Activity { 

    class MyExpandableListAdapter extends BaseExpandableListAdapter { 

     private String[]  groups = { "People Names", "Dog Names", "Cat Names", "Fish Names" }; 
     private String[][] children = { { "Arnold", "Barry", "Chuck", "David" }, 
             { "Ace", "Bandit", "Cha-Cha", "Deuce" }, 
             { "Fluffy", "Snuggles" }, { "Goldy", "Bubbles" } }; 

     public Object getChild(int groupPosition, int childPosition) { 
      return children[groupPosition][childPosition]; 
     } 

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

     public int getChildrenCount(int groupPosition) { 
      return children[groupPosition].length; 
     } 

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

      TextView question = new TextView(Main.this); 
      question.setText(getChild(groupPosition, childPosition).toString()); 
      return question; 
     } 

     public Object getGroup(int groupPosition) { 
      return groups[groupPosition]; 
     } 

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

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

     public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 
      TextView question = new TextView(Main.this); 
      question.setText(getGroup(groupPosition).toString()); 
      return question; 
     } 

     public boolean hasStableIds() { 
      return true; 
     } 

     public boolean isChildSelectable(int groupPosition, int childPosition) { 
      return true; 
     } 
    } 

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

     ExpandableListView lv = new ExpandableListView(this); 
     MyExpandableListAdapter questions = new MyExpandableListAdapter(); 
     lv.setAdapter(questions); 

     LinearLayout ll = new LinearLayout(this); 
     setContentView(ll); 
     ll.addView(lv); 

    } 

} 
+0

它也應該工作,但我試圖用XML文件:( – moktar 2011-04-02 19:57:02

1

您是否在您的按鈕定義中添加了android:focusable="false"?這爲我解決了類似的行爲。

<Button 
    android:id="@+id/widget31" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Button" 
    android:focusable="false" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentRight="true"> 
</Button> 
+0

保存我的一天... – 2012-03-29 07:28:11