在expandableListView中添加可单击的imageView

我在android中遇到了expandableListView的问题(我使用android studio)。 我在网上找到了一个关于expandableListVIew的教程,一切正常,但现在我想在每个ListGroup中添加一个可点击的图像。

我在每个组中添加了一个小修改图标,但现在我真的不知道当用户点击它时如何在主程序中执行某些操作。

MainActivity.java

package com.luca.mattia.password; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.ExpandableListView; import android.widget.ExpandableListView.OnChildClickListener; import android.widget.ExpandableListView.OnGroupClickListener; import android.widget.ExpandableListView.OnGroupCollapseListener; import android.widget.ExpandableListView.OnGroupExpandListener; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.Toast; public class MainActivity extends Activity { ExpandableListAdapter listAdapter; ExpandableListView expListView; List listDataHeader; HashMap<String, List> listDataChild; Boolean aperto[] = new Boolean[500]; int img_cont = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); for(int i = 0; i < 500; i ++) { aperto[i] = false; } // get the listview expListView = (ExpandableListView) findViewById(R.id.lvExp); // preparing list data prepareListData(); listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild); // setting list adapter expListView.setAdapter(listAdapter); //listener for child row click expListView.setOnChildClickListener(myListItemClicked); expListView.setOnGroupClickListener(myListGroupClicked); } /* * Preparing the list data */ private void prepareListData() { listDataHeader = new ArrayList(); listDataChild = new HashMap<String, List>(); // Adding child data listDataHeader.add("uno"); listDataHeader.add("due"); listDataHeader.add("tre"); // Adding child data List top250 = new ArrayList(); top250.add("E-mail: e-mail"); top250.add("Username: un"); top250.add("Password: QWERTY"); top250.add("Chiave: __________"); add_img(); List nowShowing = new ArrayList(); nowShowing.add("The Conjuring"); nowShowing.add("Despicable Me 2"); nowShowing.add("Turbo"); nowShowing.add("Grown Ups 2"); nowShowing.add("Red 2"); nowShowing.add("The Wolverine"); add_img(); List comingSoon = new ArrayList(); comingSoon.add("2 Guns"); comingSoon.add("The Smurfs 2"); comingSoon.add("The Spectacular Now"); comingSoon.add("The Canyons"); comingSoon.add("Europa Report"); add_img(); listDataChild.put(listDataHeader.get(0), top250); // Header, Child data listDataChild.put(listDataHeader.get(1), nowShowing); listDataChild.put(listDataHeader.get(2), comingSoon); } //our child listener private OnChildClickListener myListItemClicked = new OnChildClickListener() { public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { Toast.makeText(getBaseContext(), "Child clicked", Toast.LENGTH_LONG).show(); return false; } }; private OnGroupClickListener myListGroupClicked = new OnGroupClickListener() { public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { ImageView img = (ImageView) findViewById(R.id.icona_modifica); if(aperto[(int)id] == false) { img.setVisibility(View.VISIBLE); aperto[(int)id] = true; } else { img.setVisibility(View.INVISIBLE); aperto[(int)id] = false; } return false; } }; } 

ExpandableListAdapter.java

 package com.luca.mattia.password; /** * Created by Mattia on 29/11/2014. */ import java.util.HashMap; import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.ImageView; import android.widget.TextView; public class ExpandableListAdapter extends BaseExpandableListAdapter { private Context _context; private List _listDataHeader; // header titles // child data in format of header title, child title private HashMap<String, List> _listDataChild; public ExpandableListAdapter(Context context, List listDataHeader, HashMap<String, List> listChildData) { this._context = context; this._listDataHeader = listDataHeader; this._listDataChild = listChildData; } @Override public Object getChild(int groupPosition, int childPosititon) { return this._listDataChild.get(this._listDataHeader.get(groupPosition)) .get(childPosititon); } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { final String childText = (String) getChild(groupPosition, childPosition); if (convertView == null) { LayoutInflater infalInflater = (LayoutInflater) this._context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = infalInflater.inflate(R.layout.list_item, null); } TextView txtListChild = (TextView) convertView .findViewById(R.id.lblListItem); txtListChild.setText(childText); return convertView; } @Override public int getChildrenCount(int groupPosition) { return this._listDataChild.get(this._listDataHeader.get(groupPosition)) .size(); } @Override public Object getGroup(int groupPosition) { return this._listDataHeader.get(groupPosition); } @Override public int getGroupCount() { return this._listDataHeader.size(); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { String headerTitle = (String) getGroup(groupPosition); if (convertView == null) { LayoutInflater infalInflater = (LayoutInflater) this._context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = infalInflater.inflate(R.layout.list_group, null); } ImageView lblListHeader = (ImageView) convertView .findViewById(R.id.lblListHeader); return convertView; } @Override public boolean hasStableIds() { return false; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } 

list_item.xml

     

list_group.xml

   <!--  -->    

谢谢大家。

PS抱歉我的英文

您可以在getGroupView()方法中添加图像clickListener:

 @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { String headerTitle = (String) getGroup(groupPosition); if (convertView == null) { LayoutInflater infalInflater = (LayoutInflater) this._context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = infalInflater.inflate(R.layout.list_group, null); } ImageView lblListHeader = (ImageView) convertView.findViewById(R.id.lblListHeader); ImageView iconaModifica= (ImageView) convertView.findViewById(R.id.icona_modifica); iconaModifica.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // do your stuff here Toast.makeText(_context, "Group position: "+groupPosition, Toast.LENGTH_LONG).show(); } }); return convertView; } 

更新

如果要在活动中获取clickListener,可以通过实现接口来实现。

为此,您需要:

1)创建您的接口类

 public interface IExpandableListInterface { public void onClickIconaModifica(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent); // you can change the method parameters if you want } 

2)在您的活动中实现界面并添加方法:

  public class MainActivity extends Activity implements IExpandableListInterface{ public void onClickIconaModifica(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent){ // do your stuff here Toast.makeText(MainActivity.this, "Group position: "+groupPosition, Toast.LENGTH_LONG).show(); } } 

3)将接口传递给适配器:

  listAdapter = new ExpandableListAdapter(this, this, listDataHeader, listDataChild); //PS: the first "this" is for the context and the second "this" is for the interface 

4)更改适配器构造函数:

 private Context _context; private List _listDataHeader; // header titles // child data in format of header title, child title private HashMap> _listDataChild; private IExpandableListInterface mMyInterface; public ExpandableListAdapter(Context context, IExpandableListInterface myInterface, List listDataHeader, HashMap> listChildData) { this._context = context; this._listDataHeader = listDataHeader; this._listDataChild = listChildData; this.mMyInterface = myInterface; } 

5)在适配器内使用你的接口(在你的情况下你想在getGroupView()中调用它):

 @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { String headerTitle = (String) getGroup(groupPosition); if (convertView == null) { LayoutInflater infalInflater = (LayoutInflater) this._context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = infalInflater.inflate(R.layout.list_group, null); } ImageView lblListHeader = (ImageView) convertView.findViewById(R.id.lblListHeader); ImageView iconaModifica= (ImageView) convertView.findViewById(R.id.icona_modifica); iconaModifica.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mMyInterface.onClickIconaModifica(groupPosition, isExpanded, convertView, parent) } }); return convertView; } 

6)好吧,没有第6步。 只需运行您的应用程序,我希望能帮助您!