我有一个可扩展的列表视图,如下所示。我这样做是为了在每个孩子被点击时弹出一条祝酒信息。我需要每个孩子启动他们自己的活动/片段,这需要单独的onclick()方法。有人知道这是如何实现的吗?谢谢。注意:我正在使用simonvt的slidingmenu库,我对android编程还很陌生。
main活动.java:
package press.linx.expandablelistdemo;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.xml.sax.SAXException;
import net.simonvt.menudrawer.MenuDrawer;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MainActivity extends Activity {
ExpandableListView exv;
MenuDrawer mDrawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDrawer = MenuDrawer.attach(this);
mDrawer.setContentView(R.layout.activity_main);
mDrawer.setMenuView(R.layout.leftmenu);
exv=(ExpandableListView)findViewById(R.id.expandableListView1);
exv.setAdapter(new MyAdapter(this));
exv.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
String itemclicked = MyAdapter.childList[groupPosition][childPosition];
Toast.makeText(getApplicationContext(), "you clicked " + itemclicked, Toast.LENGTH_SHORT).show();
return false;
}
});
}
private void setListAdapter(SimpleAdapter adapter) {
// TODO Auto-generated method stub
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
myadapter.java版
package press.linx.expandablelistdemo;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class MyAdapter extends BaseExpandableListAdapter {
private Context context;
Typeface typeface;
String []parentList = {"Tech", "Best Of", "Art & Design", "Other"};
static String [][] childList = {
{
"All Tech", "Reviews", "Gaming", "Gadgets"
},
{
"Android"
},
{
"Architecture"
},
{
"Infographics"
}
};
public MyAdapter(Context context) {
// TODO Auto-generated constructor stub
this.context=context;
}
@Override
public Object getChild(int arg0, int arg1) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getChildId(int arg0, int arg1) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
//typeface = Typeface.createFromAsset(context.getAssets(), "font/robotochild.ttf");
TextView tv = new TextView(context);
tv.setText(childList[groupPosition][childPosition]);
tv.setPadding(30, 10, 0, 10);
tv.setTextSize(15);
//tv.setTypeface(typeface);
tv.setTextColor(Color.WHITE);
return tv;
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return childList[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return parentList.length;
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TextView tv = new TextView(context);
typeface = Typeface.createFromAsset(context.getAssets(), "font/roboto.ttf");
tv.setText(parentList[groupPosition]);
tv.setPadding(50, 10, 0, 10);
tv.setTextSize(20);
tv.setTextColor(Color.WHITE);
tv.setTypeface(typeface);
return tv;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
}
menulistview.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_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"
tools:context=".MainActivity"
android:background="@drawable/geowall">
<ExpandableListView
android:id="@+id/expandableListView1"
android:groupIndicator="@null"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:padding="3dp" >
</ExpandableListView>
!
最佳答案
如果希望每个按钮具有不同的功能,请考虑将它们存储为buttons'tag
属性(在xml布局中)。View v
侦听器中的onChildClick
参数是被单击的子对象的View
;然后可以使用它来检索标记,如下所示:v.getTag();
您可以使用switch case块根据标记调用正确的活动,也可以将活动的确切名称存储在标记中并通过反射传入,以检索活动的类。或者,您可以存储一个HashMap
映射标记名到Activity
类/变量。