Java Android中如何处理Expandablelistview中的子点击事件?
我正在Android中创建可扩展列表视图。我在相对布局中添加了三个textview,在水平线性布局中添加了两个按钮,这意味着这两个按钮添加在一行中,那么我如何在这两个按钮上触发单击事件呢 我想在可展开列表视图中单击按钮调用另一个活动。但我不知道如何在Java Android中如何处理Expandablelistview中的子点击事件?,java,android,expandablelistview,Java,Android,Expandablelistview,我正在Android中创建可扩展列表视图。我在相对布局中添加了三个textview,在水平线性布局中添加了两个按钮,这意味着这两个按钮添加在一行中,那么我如何在这两个按钮上触发单击事件呢 我想在可展开列表视图中单击按钮调用另一个活动。但我不知道如何在ExpandableListView中单击child 这是我的XML文件 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://sc
ExpandableListView
中单击child
这是我的XML文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white"
android:padding="2dp" >
<TextView
android:id="@+id/TextView_Projectdetails"
android:layout_width="match_parent"
android:layout_height="25dp"
android:background="@drawable/button_shape"
android:paddingLeft="10sp"
android:textColor="@android:color/black" />
<TextView
android:id="@+id/textOne"
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_below="@+id/TextView_Projectdetails"
android:layout_marginTop="2dp"
android:background="@drawable/button_shape"
android:paddingLeft="10sp"
android:textColor="@android:color/black" >
</TextView>
<TextView
android:id="@+id/textTwo"
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_below="@+id/textOne"
android:layout_marginTop="2dp"
android:background="@drawable/button_shape"
android:paddingLeft="10sp"
android:textColor="@android:color/black" >
</TextView>
<LinearLayout
android:id="@+id/linearlAYOUT"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textTwo"
android:layout_marginTop="5dp"
android:orientation="horizontal" >
<Button
android:id="@+id/button_EditedTask"
android:layout_width="0dp"
android:layout_height="27dp"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@drawable/button_shape_two"
android:gravity="center"
android:paddingBottom="3sp"
android:paddingTop="3sp"
android:text="ASSIGN TASK"
android:focusable="false"
android:textColor="@android:color/white"
android:textSize="12sp"
android:textStyle="normal" />
<Button
android:id="@+id/buttonViewTask"
android:layout_width="0dp"
android:layout_height="27dp"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:background="@drawable/button_shape_one"
android:gravity="center"
android:paddingBottom="3sp"
android:paddingTop="3sp"
android:text="VIEW TASK"
android:focusable="false"
android:textColor="@android:color/white"
android:textSize="12sp"
android:textStyle="normal" />
</LinearLayout>
</RelativeLayout>
这是我的活动代码-
public class My_Project extends ExpandableListActivity
{
expListView = (ExpandableListView) findViewById(android.R.id.list);
expListView.setOnGroupClickListener(new OnGroupClickListener()
{
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id)
{
return false;
}
});
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(),
dtrProjectNAmeSize.length + " Expanded",
Toast.LENGTH_SHORT).show();
}
});
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
dtrProjectNAmeSize.length + " Collapsed",
Toast.LENGTH_SHORT).show();
}
});
expListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
int cp = (int) mAdapter.getChildId(groupPosition, childPosition);
if(cp == 3)
{
switch (cp)
{
case 3:
break;
}
}
return false;
}
});
mAdapter = new SimpleExpandableListAdapter(
this,
groupData,
R.layout.list_group,
new String[] { Project_Name },
new int[] {R.id.lblListHeader },
childData,
R.layout.child_item,
new String[] { Project_Date_Created , Project_End_Date , Project_Is_Active},
new int[] { R.id.TextView_Projectdetails , R.id.textOne , R.id.textTwo }
);
expListView.setAdapter(mAdapter);
}
}
您可以设置可扩展listview的chid的setOnChildClickListener,然后根据用于创建可扩展listview的数组列表的值转到其他活动
menuExpandableListView
.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(
ExpandableListView parent, View v,
int groupPosition, int childPosition,
long id) {
GoCategory(mainMenusList.get(groupPosition)
.getPagesList().get(childPosition));
return false;
}
});
或者你可以这样做
@Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
Page page =(Page) getChild(groupPosition, childPosition);
convertView = inflater.inflate(R.layout.child_list_layout, null);
Button button1=(Button)convertView.findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Your code here....
}
});
return convertView;
}
你想在这里做什么-
expListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
int cp = (int) mAdapter.getChildId(groupPosition, childPosition);
if(cp == 3)
{
switch (cp)
{
case 3:
break;
}
}
return false;
}
});
此侦听器用于子项单击,在这部分代码中调用
startActivity(intent)
。@Ad Abdul Gafur,但我必须在子列表视图中并排添加两个按钮,以便如何在这些按钮上触发单击事件。@Ad Abdul Gafur,您能给我一个GoCategory函数的代码吗。@Ad Abdul Gafur,我已准备好尝试此代码,但我不知道如何从ExpandableListAdapter扩展的BaseExpandableListAdapter类中的getChildView调用另一个活动。我如何调用另一个活动。@Ad Abdul Gafur,请给我一个解决方案。第一个代码段对我来说一下子就奏效了。向上投票!
@Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
Page page =(Page) getChild(groupPosition, childPosition);
convertView = inflater.inflate(R.layout.child_list_layout, null);
Button button1=(Button)convertView.findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Your code here....
}
});
return convertView;
}
expListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
int cp = (int) mAdapter.getChildId(groupPosition, childPosition);
if(cp == 3)
{
switch (cp)
{
case 3:
break;
}
}
return false;
}
});