我是Android界的新手,我有以下情况:
我有一个Activity main.xml RelativeLayout并且在ExpandableListView中。
当我展开一个组时,它会显示给孩子的内容:这是example.xml。
现在我在带有id btn_send的example.xml-layout 1 Button上,我的问题是如何访问按钮?
我试图通过Example.java访问该按钮我收到错误,无法找到btn_send。我试过Main.java我得到了一些错误。
提前谢谢你,
BR 阿里
public class Main extends Activity {
ExpandableListView exv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
exv = (ExpandableListView) findViewById(R.id.exv_main);
exv.setAdapter(new MainAdapter(this));
exv.setOnGroupExpandListener(new OnGroupExpandListener() {
int previousGroup = -1;
@Override
public void onGroupExpand(int groupPosition) {
if(groupPosition != previousGroup)
exv.collapseGroup(previousGroup);
previousGroup = groupPosition;
}
});
}
}
在MainAdapter类
中@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout layout = new RelativeLayout(context);
ImageView iv = new ImageView(context);
RelativeLayout.LayoutParams llpImage = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
layout.setGravity(Gravity.CENTER | Gravity.TOP);
switch (groupPosition){
case 0:
layout = (RelativeLayout) inflater.inflate(childList[groupPosition][childPosition], null);
break;
case 1:
break;
case 2:
break;
}
return layout;
}
和main.xml
<ExpandableListView
android:id="@+id/exv_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="20dp"
android:background="#CDCDD0"
android:divider="@android:color/transparent"
android:listSelector="#CDCDD0"
android:dividerHeight="1dip"
android:cacheColorHint="#CDCDD0"
android:childDivider="#CDCDD0"
>
</ExpandableListView>
并在example.xml中
<Button
android:id="@+id/btn_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="send" />
和Example.java
public class示例扩展了Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.example);
final Button btn = (Button) findViewById(R.id.btn_send);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
btn.setText("huhu");
}
});
}
}
答案 0 :(得分:0)
您必须在getChildView方法中为您的按钮设置onClickListener:
public class MyExpandableAdapter extends BaseExpandableListAdapter {
private final SparseArray<Group> groups;
public Activity activity;
public LayoutInflater inflater;
public MyExpandableAdapter(Activity activity, SparseArray<Group> groups) {
this.groups = groups;
this.activity = activity;
this.inflater = activity.getLayoutInflater();
}
@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.rowlayout_details, null);
}
TextView text = (TextView) convertView.findViewById(R.id.textView1);
text.setText("content");
Button b = (Button) convertView.findViewById(R.id.button);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// implement functionality
}
});
return convertView;
}
}
答案 1 :(得分:0)
@Kuba Spatny感谢您的帮助,我解决了我的问题,如下面的代码......也许有其他办法解决它,但这是我的解决方案:
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
LayoutInflater inflater;
switch (groupPosition) {
case 0:
if (convertView == null) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(childLayout[groupPosition][childPosition], null);
Button btn1 = (Button) convertView.findViewById(R.id.btn_send);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.getContext().startActivity(new Intent(context, ExampTest1.class));
}
});
}
break;
case 1:
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(childLayout[groupPosition][childPosition], null);
Button btn1 = (Button) convertView.findViewById(R.id.btn_send);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.getContext().startActivity(new Intent(context, ExampTest2.class));
}
});
break;
case 2:
if (convertView == null) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(childLayout[groupPosition][childPosition], null);
Button btn1 = (Button) convertView.findViewById(R.id.btn_send);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.getContext().startActivity(new Intent(context, ExampTest3.class));
}
});
break;
}
return convertView;
}