ExpandableListActivity使childItems无法点击

时间:2012-02-28 11:03:24

标签: android android-activity

嘿,我设计了一个帮助活动,我有一些父母(groupItems),每个孩子都有一个特定的孩子。到目前为止,没有任何问题。但是,默认情况下,ExpandableListView的childItem可以单击,以便在您单击它们时它们会闪烁。而这正是我想要避免的!这是我用于孩子的布局

<?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"

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

2 个答案:

答案 0 :(得分: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;
}

View的Clickable属性仅确定在单击时它的状态是否会更改为“press”,它在View是否可以被点击时不起作用。 ExpandableListAdapter将默认侦听器设置为其组和子视图,因此您必须删除它们以消除对点击,焦点等的任何“反应”。

答案 1 :(得分:1)

试试这个:

getExpandableListView().setOnChildClickListener(null);