如何为子布局设置onClickListener ...每个孩子都必须打开一些新活动。我需要设置每个孩子可点击以打开新活动。我需要放置代码......我是新手 这是我的适配器:
public MyExpandableAdapter(ArrayList<String> parents, ArrayList<Object> childern)
{
this.parentItems = parents;
this.childtems = childern;
}
public void setInflater(LayoutInflater inflater, Activity activity)
{
this.inflater = inflater;
this.activity = activity;
}
// method getChildView is called automatically for each child view.
// Implement this method as per your requirement
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
{
child = (ArrayList<String>) childtems.get(groupPosition);
TextView textView = null;
ImageView img = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.child_view, null);
}
// get the textView reference and set the value
textView = (TextView) convertView.findViewById(R.id.textViewChild);
textView.setText(child.get(childPosition));
img = (ImageView) convertView.findViewById(R.id.childImage);
if (childPosition == 0 && groupPosition==0) {
img.setImageResource(R.drawable.slika1);
}
if (childPosition == 1 && groupPosition==0) {
img.setImageResource(R.drawable.slika2);
}
if (childPosition == 2 && groupPosition==0) {
img.setImageResource(R.drawable.slika1);
}
if (childPosition == 3 && groupPosition==0) {
img.setImageResource(R.drawable.slika2);
}
if (childPosition == 4 && groupPosition==0) {
img.setImageResource(R.drawable.slika1);
}
if (childPosition == 5 && groupPosition==0) {
img.setImageResource(R.drawable.slika2);
}
if (childPosition == 6 && groupPosition==0) {
img.setImageResource(R.drawable.slika1);
}
if (childPosition == 7 && groupPosition==0) {
img.setImageResource(R.drawable.slika2);
}
if (childPosition == 8 && groupPosition==0) {
img.setImageResource(R.drawable.slika1);
}
if (childPosition == 0 && groupPosition==1) {
img.setImageResource(R.drawable.slika2);
}
if (childPosition == 1 && groupPosition==1) {
img.setImageResource(R.drawable.slika1);
}
if (childPosition == 2 && groupPosition==1) {
img.setImageResource(R.drawable.slika2);
}
if (childPosition == 3 && groupPosition==1) {
img.setImageResource(R.drawable.slika1);
}
if (childPosition == 0 && groupPosition==2) {
img.setImageResource(R.drawable.slika2);
}
if (childPosition == 1 && groupPosition==2) {
img.setImageResource(R.drawable.slika1);
}
if (childPosition == 2 && groupPosition==2 ) {
img.setImageResource(R.drawable.slika2);
}
if (childPosition == 3 && groupPosition==2) {
img.setImageResource(R.drawable.slika1);
}
if (childPosition == 0 && groupPosition==3) {
img.setImageResource(R.drawable.slika2);
}
if (childPosition == 1 && groupPosition==3) {
img.setImageResource(R.drawable.slika1);
}
if (childPosition == 2 && groupPosition==3) {
img.setImageResource(R.drawable.slika2);
}
if (childPosition == 3 && groupPosition==3) {
img.setImageResource(R.drawable.slika1);
}
// set the ClickListener to handle the click event on child item
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(activity, child.get(childPosition),
Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
// method getGroupView is called automatically for each parent item
// Implement this method as per your requirement
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
{
if (convertView == null) {
convertView = inflater.inflate(R.layout.parent_view, null);
}
((CheckedTextView) convertView).setText(parentItems.get(groupPosition));
((CheckedTextView) convertView).setChecked(isExpanded);
return convertView;
}
答案 0 :(得分:0)
mExpandableList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(final ExpandableListView parent, final View v, final int groupPosition, final int childPosition, final long id) {
return false;
}
});
答案 1 :(得分:0)
您可以使用以下代码设置背景和点击监听器,而不是if条件:
Intent intent = null;
switch (groupPosition){
case 0:
switch (childPosition){
case 0:
img.setImageResource(R.drawable.slika1);
intent = new Intent(activity, Activity1.class);
break;
....
}
break;
case 1:
switch (childPosition){
case 0:
img.setImageResource(R.drawable.slika2);
intent = new Intent(activity, Activity2.class);
break;
....
}
break;
}
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
activity.startActivity(intent);
}
});
希望这有帮助。