And*_*Dev 25 android expandablelistview onclicklistener
不知道为什么我的onChildClick没有触发.一切都很完美,除了当其中一个儿童用品被敲击时,绝对没有任何反应.否则,可扩展组按预期工作.
我已经回顾了我对子xml文件中复选框的使用.当我删除此复选框时,onChildClick将按预期触发.但我需要此复选框来显示此活动的功能.我做错了什么?谢谢!
public class MySettings extends Activity {
private ExpandListAdapter expAdapter;
private ArrayList<ExpandListGroup> expListItems;
private ExpandableListView expandableList;
private String client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_settings);
expandableList = (ExpandableListView) findViewById(R.id.expandable_list);
expListItems = SetStandardGroups(); //works fine - can show code if needed
expAdapter = new ExpandListAdapter(MySettings.this, expListItems);
expandableList.setAdapter(expAdapter);
expandableList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
//Nothing here ever fires
System.err.println("child clicked");
Toast.makeText(getApplicationContext(), "child clicked", Toast.LENGTH_SHORT).show();
return true;
}
});
}
Run Code Online (Sandbox Code Playgroud)
这是xml文件:
activity_my_settings.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/background" >
<ImageView
android:id="@+id/logo"
android:layout_marginTop="5dip"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_gravity="left"
android:contentDescription="@string/blank"
android:src="@raw/logo" >
</ImageView>
<TextView
android:id="@+id/my_settings"
android:textColor="#000000"
android:layout_below="@id/logo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="@string/my_settings"
android:textSize="30sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/my_settings" >
<ExpandableListView
android:id="@+id/expandable_list"
android:scrollingCache="false"
android:layout_marginTop="20dip"
android:layout_height="wrap_content"
android:layout_width="match_parent" />
</LinearLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
expandlist_group_item.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="55dip"
android:background="#FF7C7C7C" >
<TextView
android:id="@+id/group_header"
android:layout_marginLeft="40dp"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:gravity="center_vertical"
android:textColor="#000000"
android:textSize="22sp" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
expandlist_child_item.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="55dip"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/check_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/expand_list_item"
android:paddingLeft="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="@dimen/smart_finder_settings_font_size"
android:textColor="#FFFFFF" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
Jan*_*Jan 54
请务必覆盖可扩展列表适配器的isChildSelectable方法并返回true,如下所示:
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
...
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
...
}
Run Code Online (Sandbox Code Playgroud)
And*_*Dev 25
我知道了.我所要做的就是添加
android:focusable="false"
Run Code Online (Sandbox Code Playgroud)
在我的expandlist_child_item.xml文件的CheckBox部分中.
我希望这有助于某人.
S.D*_*.D. 12
看起来还不错,仍然:
检查您是否未将list侦听器设置为listview的任何父视图.
检查isChildSelectable()适配器返回true.也areAllItemsEnabled()应该返回true.
| 归档时间: |
|
| 查看次数: |
32006 次 |
| 最近记录: |