我创建了一个ExpandableList View
,其中包含一些组项和相应的子项。
我可以访问子视图,并在groupPosition
和childPosition
的帮助下访问子视图属性。
现在我必须在每个子视图上放置两个ImageButton
,并在点击每个视图时触发不同的Intents。
我尝试编码为每个图片按钮设置onClickListeners
并获得相应的结果,但我在返回值中面临困难,其中getChildView
只返回一个View
如何获得每个ImageButton
点击的响应?
我的代码如下:
public View getChildView (final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
{
final View providbtn = super.getChildView(groupPosition, childPosition, isLastChild, convertView, parent);
final View lecvidbtn = super.getChildView(groupPosition, childPosition, isLastChild, convertView, parent);
final ImageButton providbtn1 = (ImageButton)providbtn.findViewById(R.id.provid);
final ImageButton lecvidbtn1 = (ImageButton)lecvidbtn.findViewById(R.id.lecvid);
//final View childItemClicked = super.getChildView(groupPosition, childPosition, isLastChild, convertView, parent);
//final ImageButton ib = (ImageButton)childItemClicked.findViewById(R.id.)
//ImageButton provid = (ImageButton)findViewById(R.id.provid);
providbtn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplication(), "Topic Proc video is: "+topic[groupPosition]+" and Chapter is "+chapter[groupPosition][childPosition], Toast.LENGTH_LONG).show();
flag = "p";
Bundle bundle = new Bundle();
bundle.putString("topic",topic[groupPosition] );
bundle.putString("chapter",chapter[groupPosition][childPosition] );
Intent goHome = new Intent(getApplicationContext(), VideoWeb.class);
goHome.putExtras(bundle);
startActivity(goHome);
}
});
lecvidbtn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplication(), "Topic Lec video is: "+topic[groupPosition]+" and Chapter is "+chapter[groupPosition][childPosition], Toast.LENGTH_LONG).show();
flag = "l";
Bundle bundle = new Bundle();
bundle.putString("topic",topic[groupPosition] );
bundle.putString("chapter",chapter[groupPosition][childPosition] );
Intent goHome = new Intent(getApplicationContext(), VideoWeb.class);
goHome.putExtras(bundle);
startActivity(goHome);
}
});
return lecvidbtn; //My confusion is here. How to capture the return value for any of the image buttons clicked above.?
}
答案 0 :(得分:1)
试试这种方式
public View getChildView(int groupPosition,int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View v = null;
if( convertView != null )
v = convertView;
else
v = inflater.inflate(R.layout.child_row, parent, false);
String c = (String)getChild( groupPosition, childPosition );
final int grpPos = groupPosition;
final int childPos = childPosition;
ImageButton button1 = (ImageButton)v.findViewById( R.id.imageButton1);
ImageButton button2 = (ImageButton)v.findViewById( R.id.imageButton2);
// and set onClickListener to this two button
button1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplication(), "Topic Proc video is: "+topic[groupPosition]+" and Chapter is "+chapter[groupPosition][childPosition], Toast.LENGTH_LONG).show();
flag = "p";
Bundle bundle = new Bundle();
bundle.putString("topic",topic[groupPosition] );
bundle.putString("chapter",chapter[groupPosition][childPosition] );
Intent goHome = new Intent(getApplicationContext(), VideoWeb.class);
goHome.putExtras(bundle);
startActivity(goHome);
}
)};
button2.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplication(), "Topic Lec video is: "+topic[groupPosition]+" and Chapter is "+chapter[groupPosition][childPosition], Toast.LENGTH_LONG).show();
flag = "l";
Bundle bundle = new Bundle();
bundle.putString("topic",topic[groupPosition] );
bundle.putString("chapter",chapter[groupPosition][childPosition] );
Intent goHome = new Intent(getApplicationContext(), VideoWeb.class);
goHome.putExtras(bundle);
startActivity(goHome);
}
)};
return v;
}
和 child_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:background="#ebebeb"
android:layout_height="50dp">
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="22dp"
android:src="@drawable/button1" />
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginLeft="46dp"
android:layout_toRightOf="@+id/imageButton1"
android:src="@drawable/button2" />
</RelativeLayout>