2012-02-28 56 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