Java 在expandableListView中添加可单击的图像视图
我在android中遇到了expandableListView问题(我使用的是android studio)。 我在网上找到了一个关于expandableListVIew的教程,所有东西都可以使用,但是现在我想在每个列表组中添加一个可点击的图像 我在每个组中都添加了一个小的修改图标,但现在我真的不知道当用户单击主程序时如何在主程序中执行某些操作 MainActivity.javaJava 在expandableListView中添加可单击的图像视图,java,android,Java,Android,我在android中遇到了expandableListView问题(我使用的是android studio)。 我在网上找到了一个关于expandableListVIew的教程,所有东西都可以使用,但是现在我想在每个列表组中添加一个可点击的图像 我在每个组中都添加了一个小的修改图标,但现在我真的不知道当用户单击主程序时如何在主程序中执行某些操作 MainActivity.java package com.luca.mattia.password; import android.app.Acti
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<String> listDataHeader;
HashMap<String, List<String>> 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<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("uno");
listDataHeader.add("due");
listDataHeader.add("tre");
// Adding child data
List<String> top250 = new ArrayList<String>();
top250.add("E-mail: e-mail");
top250.add("Username: un");
top250.add("Password: QWERTY");
top250.add("Chiave: __________");
add_img();
List<String> nowShowing = new ArrayList<String>();
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<String> comingSoon = new ArrayList<String>();
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;
}
};
}
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<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> 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;
}
package com.luca.mattia.password;
导入android.app.Activity;
导入android.os.Bundle;
导入android.view.Menu;
导入android.view.MenuItem;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入android.app.Activity;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.ExpandableListView;
导入android.widget.ExpandableListView.OnChildClickListener;
导入android.widget.ExpandableListView.OnGroupClickListener;
导入android.widget.ExpandableListView.OnGroupCollapseListener;
导入android.widget.ExpandableListView.OnGroupExpandListener;
导入android.widget.ImageView;
导入android.widget.RelativeLayout;
导入android.widget.Toast;
公共类MainActivity扩展了活动{
可扩展列表适配器;
ExpandableListView解释视图;
列表列表数据头;
HashMapListDataChild;
布尔值到[]=新布尔值[500];
int img_cont=0;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
对于(int i=0;i<500;i++)
{
aperto[i]=假;
}
//获取列表视图
expListView=(ExpandableListView)findViewById(R.id.lvExp);
//准备列表数据
prepareListData();
listAdapter=新的ExpandableListAdapter(此,listDataHeader,listDataChild);
//设置列表适配器
expListView.setAdapter(listAdapter);
//子行的侦听器单击
expListView.setOnChildClickListener(myListItemClicked);
expListView.setOnGroupClickListener(myListGroupClicked);
}
/*
*准备列表数据
*/
私有void prepareListData(){
listDataHeader=新的ArrayList();
listDataChild=newHashMap();
//添加子数据
添加(“uno”);
添加(“到期”);
添加(“tre”);
//添加子数据
List top250=新的ArrayList();
top250.添加(“电子邮件:电子邮件”);
top250.添加(“用户名:un”);
添加(“密码:QWERTY”);
top250.添加(“交叉:uuuuuuuuuuuuuuuuuuuuuuuuuuu”);
添加_img();
List nowShowing=新建ArrayList();
现在显示。添加(“变戏法”);
添加(“卑鄙的我2”);
现在显示。添加(“Turbo”);
现在显示。添加(“成年人2”);
现在显示。添加(“红色2”);
现在显示。添加(“狼獾”);
添加_img();
List comingSoon=新建ArrayList();
很快就来。添加(“2支枪”);
很快就来。添加(“蓝精灵2”);
即将到来。添加(“现在的壮观”);
即将到来。添加(“峡谷”);
即将出版。添加(“欧罗巴报告”);
添加_img();
listDataChild.put(listDataHeader.get(0),top250);//头,子数据
listDataChild.put(listDataHeader.get(1),现在显示);
listDataChild.put(listDataHeader.get(2),即将出现);
}
//我们的孩子听众
private OnChildClickListener myListItemClicked=new OnChildClickListener(){
公共布尔onChildClick(ExpandableListView父视图,视图v,
int groupPosition、int childPosition、long id){
Toast.makeText(getBaseContext(),“已单击子对象”,
Toast.LENGTH_LONG).show();
返回false;
}
};
private OnGroupClickListener myListGroupClicked=新建OnGroupClickListener(){
公共布尔值onGroupClick(ExpandableListView父视图,视图v,
int groupPosition,长id){
ImageView img=(ImageView)findViewById(R.id.icona_modifica);
if(aperto[(int)id]==false)
{
img.setVisibility(视图可见);
aperto[(int)id]=真;
}
其他的
{
img.setVisibility(视图不可见);
aperto[(int)id]=假;
}
返回false;
}
};
}
ExpandableListAdapter.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<String> listDataHeader;
HashMap<String, List<String>> 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<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("uno");
listDataHeader.add("due");
listDataHeader.add("tre");
// Adding child data
List<String> top250 = new ArrayList<String>();
top250.add("E-mail: e-mail");
top250.add("Username: un");
top250.add("Password: QWERTY");
top250.add("Chiave: __________");
add_img();
List<String> nowShowing = new ArrayList<String>();
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<String> comingSoon = new ArrayList<String>();
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;
}
};
}
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<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> 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;
}
package com.luca.mattia.password;
/**
*由Mattia于2014年11月29日创建。
*/
导入java.util.HashMap;
导入java.util.List;
导入android.content.Context;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.BaseExpandableListAdapter;
导入android.widget.ImageView;
导入android.widget.TextView;
公共类ExpandableListAdapter扩展了BaseExpandableListAdapter{
私人语境(private Context)(私人语境);;
私有列表_listDataHeader;//头标题
//标题标题、子标题格式的子数据
私有HashMap_listDataChild;
公共ExpandableListAdapter(上下文、列表listDataHeader、,
HashMap listChildData){
这._context=context;
这。_listDataHeader=listDataHeader;
这。_listDataChild=listChildData;
}
@凌驾
公共对象getChild(int-groupPosition、int-ChildPosition){
返回此。_listDataChild.get(此。_listDataHeader.get(groupPosition))
.get(childpositionon);
}
@凌驾
公共长getChildId(int-groupPosition,int-childPosition){
返回子位置;
}
@凌驾
公共视图getChildView(int groupPosition,final int childPosition,
布尔值isLastChild、视图转换视图、视图组父级){
最终字符串childText=(字符串)getChild(groupPosition,childPosition);
if(convertView==null){
LayoutInflater infalInflater=(LayoutInflater)this.\u上下文
.getSystemService(上下文布局\充气机\服务);
convertView=infalInflater.充气(R.layout.list_项,空);
}
TextView txtListChild=(TextView)convertView
.findviewbyd(R.id.lblListItem);
setText(childText);
返回视图;
}
@凌驾
酒吧
@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;
}
public interface IExpandableListInterface {
public void onClickIconaModifica(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent); // you can change the method parameters if you want
}
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();
}
}
listAdapter = new ExpandableListAdapter(this, this, listDataHeader, listDataChild);
//PS: the first "this" is for the context and the second "this" is for the interface
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
private IExpandableListInterface mMyInterface;
public ExpandableListAdapter(Context context, IExpandableListInterface myInterface, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
this.mMyInterface = myInterface;
}
@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;
}