Select 单击 ExpandableListView 中的按钮时的项目
Select project when clicking in a button within an ExpandableListView
我是 Android 的新人。对不起,如果这是一个愚蠢的问题。
我想在用户单击按钮时获取 ExpandableListView 中包含的项目数据。
enter image description here
例:当用户点击Project 1按钮时,我想获取Project 1的数据。
这是我的 HeaderView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp">
<TextView
android:id="@+id/project_title"
android:textSize="16sp"
android:paddingLeft="35dp"
android:gravity="center_vertical"
android:background="@color/colorinnotholic"
android:textColor="@color/white"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="45sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="45dp"
android:background="@color/colorinnotholic">
<Button
android:id="@+id/select_project"
android:background="@drawable/ic_menu_send"
android:shadowColor="@null"
android:layout_marginRight="10dp"
android:backgroundTint="@android:color/white"
android:layout_gravity="center_vertical"
android:layout_width="25dp"
android:layout_height="25dp"
android:focusable="false"
android:onClick="selectProject"/>
</LinearLayout>
现在,您可以看到,为了执行操作,我在 XML 文件中包含了 onClick 方法。
selectProject 方法是
public void selectProject(View v) {
switch (v.getId()){
case R.id.select_project:
// final Project selectedProject = (Project) listAdapter.getChild(0,0);
default:
break;
}
}
在评论中,您可以看到我可以手动获取项目。但是,我无法从用户操作中动态获取。我的 expandableListView.getSelectedItemPosition returns -1。我想这可能是因为我直接访问了按钮的onclick方法。但是,我真的不知道如何以不同的方式访问。我也尝试过使用 setOnChildClickListener,它提供 groupPosition 和 childPosition。
有人能帮忙吗?
如果您认为还有更好的方法而不是包含按钮,我对此持开放态度。让我知道我是否应该包含来自自定义 ExpandableListAdapter 的代码。
提前致谢。
谢谢大家,
我终于找到了我要找的东西。我创建了一个界面,正如@Rami 在 here 中所解释的那样。希望对其他人有帮助。
我是 Android 的新人。对不起,如果这是一个愚蠢的问题。 我想在用户单击按钮时获取 ExpandableListView 中包含的项目数据。 enter image description here
例:当用户点击Project 1按钮时,我想获取Project 1的数据。
这是我的 HeaderView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp">
<TextView
android:id="@+id/project_title"
android:textSize="16sp"
android:paddingLeft="35dp"
android:gravity="center_vertical"
android:background="@color/colorinnotholic"
android:textColor="@color/white"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="45sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="45dp"
android:background="@color/colorinnotholic">
<Button
android:id="@+id/select_project"
android:background="@drawable/ic_menu_send"
android:shadowColor="@null"
android:layout_marginRight="10dp"
android:backgroundTint="@android:color/white"
android:layout_gravity="center_vertical"
android:layout_width="25dp"
android:layout_height="25dp"
android:focusable="false"
android:onClick="selectProject"/>
</LinearLayout>
现在,您可以看到,为了执行操作,我在 XML 文件中包含了 onClick 方法。
selectProject 方法是
public void selectProject(View v) {
switch (v.getId()){
case R.id.select_project:
// final Project selectedProject = (Project) listAdapter.getChild(0,0);
default:
break;
}
}
在评论中,您可以看到我可以手动获取项目。但是,我无法从用户操作中动态获取。我的 expandableListView.getSelectedItemPosition returns -1。我想这可能是因为我直接访问了按钮的onclick方法。但是,我真的不知道如何以不同的方式访问。我也尝试过使用 setOnChildClickListener,它提供 groupPosition 和 childPosition。
有人能帮忙吗? 如果您认为还有更好的方法而不是包含按钮,我对此持开放态度。让我知道我是否应该包含来自自定义 ExpandableListAdapter 的代码。
提前致谢。
谢谢大家, 我终于找到了我要找的东西。我创建了一个界面,正如@Rami 在 here 中所解释的那样。希望对其他人有帮助。