2012-02-28 42 views
0

嘿,我設計了一個幫助活動,我有父母(groupItems),每個都有一個特定的孩子..到目前爲止沒有問題。但是,默認情況下,可擴展列表視圖的childItems是可點擊的,以便在您點擊它們時閃爍。而這正是我想要避免的!這是我用兒童的佈局ExpandableListActivity使childItems不可點擊

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 

    <TextView android:id="@+id/grp_child" 
     android:paddingLeft="20dp" 
     android:textSize="15dp" 
     android:textStyle="normal" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 
</LinearLayout> 

這裏是我的源代碼的一切變得充滿內容(這工作):

public class HelpActivity extends ExpandableListActivity { 
private static final String TAG = "HelpActivity"; 

private static final String PARENT = "parent"; 
private static final String CHILD = "child"; 

private List<HashMap<String,String>> parents; 
private List<List<HashMap<String, String>>> childs; 

public void onCreate(Bundle savedInstanceState) { 
    Log.i(TAG, "Instantiated new " + this.getClass()); 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.help); 
    parents = createParentList(); 
    childs = createChildList(); 
    SimpleExpandableListAdapter expListAdapter = new SimpleExpandableListAdapter(
     this, 
     parents,      // Describing data of group List 
     R.layout.help_group_item,  // Group item layout XML. 
     new String[] {PARENT},   // the key of group item. 
     new int[] {R.id.row_name},  // ID of each group item.-Data under the key goes into this TextView. 
     childs,       // childData describes second-level entries. 
     R.layout.help_child_item,  // Layout for sub-level entries(second level). 
     new String[] {CHILD},   // Keys in childData maps to display. 
     new int[] {R.id.grp_child}  // Data under the keys above go into these TextViews. 
    ); 
    setListAdapter(expListAdapter);  // setting the adapter in the list. 
} 

/* Creating the Hashmap for the row */ 
private List<HashMap<String,String>> createParentList() { 
    ArrayList<HashMap<String,String>> result = new ArrayList<HashMap<String,String>>(); 
    HashMap<String,String> m = new HashMap<String,String>(); 
    m.put(PARENT,getResources().getString(R.string.help_parent_1)); 
    result.add(m); 
    m = new HashMap<String,String>(); 
    m.put(PARENT,getResources().getString(R.string.help_parent_2)); 
    result.add(m); 
    m = new HashMap<String,String>(); 
    m.put(PARENT,getResources().getString(R.string.help_parent_3)); 
    result.add(m); 
    return result; 
} 

/* creatin the HashMap for the children */ 
private List<List<HashMap<String, String>>> createChildList() { 
    ArrayList<List<HashMap<String, String>>> result = new ArrayList<List<HashMap<String, String>>>(); 
    for (int i=0;i<parents.size();i++){ 
     HashMap<String, String> sec = parents.get(i); 
     ArrayList<HashMap<String, String>> secChild = new ArrayList<HashMap<String, String>>(); 
     HashMap<String,String> child = new HashMap<String,String>(); 
     if(sec.containsValue(getResources().getString(R.string.help_parent_1))){ 
      child.put(CHILD, getResources().getString(R.string.help_child_1)); 
     }else if(sec.containsValue(getResources().getString(R.string.help_parent_2))){ 
      child.put(CHILD, getResources().getString(R.string.help_child_2)); 
     }else if(sec.containsValue(getResources().getString(R.string.help_parent_3))){ 
      child.put(CHILD, getResources().getString(R.string.help_child_3)); 
     }else if(sec.containsValue(getResources().getString(R.string.help_parent_4))){ 
      child.put(CHILD, getResources().getString(R.string.help_child_4)); 
     } 
     secChild.add(child); 
     result.add(secChild); 
    } 
    return result; 
} 

}

我想辦是使textview的孩子不可點擊:我已經嘗試了textview屬性,但沒有人有效

android:clickable="false" 
android:focusable="false" 
android:enabled="false" 

提前感謝任何可能有想法的人!

回答

1

您需要自定義您自己的ExpandableListAdapter,例如通過擴展SimpleExpandableListAdapter並覆蓋.newChildView()和.getChildView()方法。在這些被覆蓋的方法中,您可以通過將子視圖的OnClickListener和OnLongClickListener設置爲null來進行自定義。

@Override 
public View newChildView(boolean isLastChild, ViewGroup parent) { 
    View v = super.newChildView(isLastChild, parent); 
    v.setOnClickListener(null); 
    v.setOnLongClickListener(null); 
    return v; 
} 

@Override 
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 
    View v = super.getChildView(groupPosition, childPosition, isLastChild, convertView, parent); 
    v.setOnClickListener(null); 
    v.setOnLongClickListener(null); 
    return v; 
} 

視圖的可點擊屬性僅確定它是否是狀態將更改爲「壓」在單擊時,它不會在查看是否能獲得點擊或不能發揮作用。一個ExpandableListAdapter設置默認監聽到它的組和子視圖,所以你必須將其刪除,以消除任何「反應」點擊,焦點等

+0

太好了,我已經想到它couldn不那麼困難。剛試過 - 完美的作品。非常感謝你。 – Vossi 2012-02-28 14:13:54

1

試試這個:

getExpandableListView().setOnChildClickListener(null); 
+0

我也檢查了你的解決方案,但它不工作 - 反正謝謝你的回覆。 – Vossi 2012-02-28 14:14:51