大家晚上好!
我目前正在使用ExpandableListView。父级包含1个ImageView。子级包含1个TextView和3个按钮。在显示方面,没问题:一切都可以按我的意愿进行。
但是,我想向按钮添加操作,并且我意识到,当我将SetOnclickListener放入getChildView时,我无法启动新的INTENT。
我在互联网上找到的示例似乎只显示了对动态元素的点击。但是,就我而言,按钮不是动态的。我计划使用变量来执行SWITCH,从而启动正确的INTENT。但是由于它不起作用,所以我想知道我的方向是否正确。
MaintActivity.java
package com.evo.tab2escape;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;
public class jeux2 extends Activity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<Integer> headerData;
HashMap<Integer,ArrayList<ChildDataModel>> childData;
ChildDataModel childDataModel;
Context mContext;
ArrayList<ChildDataModel> logo0,logo1,logo2,logo3,logo4,logo5,logo6,logo7,logo8,logo9,logo10,logo11;
private int lastExpandedPosition = -1;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.jeux2);
mContext = this;
headerData = new ArrayList<>();
childData = new HashMap<Integer, ArrayList<ChildDataModel>>();
logo0 = new ArrayList<>();
logo1 = new ArrayList<>();
logo2 = new ArrayList<>();
logo3 = new ArrayList<>();
logo4 = new ArrayList<>();
logo5 = new ArrayList<>();
logo6 = new ArrayList<>();
logo7 = new ArrayList<>();
logo8 = new ArrayList<>();
logo9 = new ArrayList<>();
logo10 = new ArrayList<>();
logo11 = new ArrayList<>();
// get the listview
expListView = (ExpandableListView) findViewById(R.id.ExpandableListView);
// populate data
// Adding header data
headerData.add(R.drawable.logo0);
headerData.add(R.drawable.logo1);
headerData.add(R.drawable.logo2);
headerData.add(R.drawable.logo3);
headerData.add(R.drawable.logo4);
headerData.add(R.drawable.logo5);
headerData.add(R.drawable.logo6);
headerData.add(R.drawable.logo7);
headerData.add(R.drawable.logo8);
headerData.add(R.drawable.logo9);
headerData.add(R.drawable.logo10);
headerData.add(R.drawable.logo11);
// Adding child data
childDataModel = new ChildDataModel(1,"Test0","logo0");
logo0.add(childDataModel);
childData.put(headerData.get(0),logo0);
childDataModel = new ChildDataModel(1,"Test1","logo1");
logo1.add(childDataModel);
childData.put(headerData.get(1),logo1);
childDataModel = new ChildDataModel(1,"Test2","logo2");
logo2.add(childDataModel);
childData.put(headerData.get(2),logo2);
childDataModel = new ChildDataModel(1,"Test3","logo3");
logo3.add(childDataModel);
childData.put(headerData.get(3),logo3);
childDataModel = new ChildDataModel(1,"Test4","logo4");
logo4.add(childDataModel);
childData.put(headerData.get(4),logo4);
childDataModel = new ChildDataModel(1,"Test5","logo5");
logo5.add(childDataModel);
childData.put(headerData.get(5),logo5);
childDataModel = new ChildDataModel(1,"Test6","logo6");
logo6.add(childDataModel);
childData.put(headerData.get(6),logo6);
childDataModel = new ChildDataModel(1,"Test7","logo7");
logo7.add(childDataModel);
childData.put(headerData.get(7),logo7);
childDataModel = new ChildDataModel(1,"Test8","logo8");
logo8.add(childDataModel);
childData.put(headerData.get(8),logo8);
childDataModel = new ChildDataModel(1,"test09","logo9");
logo9.add(childDataModel);
childData.put(headerData.get(9),logo9);
childDataModel = new ChildDataModel(1,"test10","logo10");
logo10.add(childDataModel);
childData.put(headerData.get(10),logo10);
childDataModel = new ChildDataModel(1,"test11","logo11");
logo11.add(childDataModel);
childData.put(headerData.get(11),logo11);
listAdapter = new ExpandableListAdapter(this, headerData, childData);
// setting list adapter
expListView.setAdapter(listAdapter);
//--------------------------child click listener--------------------------
expListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id)
{
Log.d("TEST", "onChildClick: " + groupPosition + " " + childPosition + " " + id + " " + parent);
//Toast.makeText(getApplicationContext(), "heeeeeeeeerrrrreeeee!", Toast.LENGTH_SHORT).show();
return true;
}
});
//group expanded
expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int headPosition) {
if (lastExpandedPosition != -1
&& headPosition != lastExpandedPosition) {
expListView.collapseGroup(lastExpandedPosition);
}
lastExpandedPosition = headPosition;
}
});
//group collapsed
expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int headPosition) {
}
});
//--------------------------RETOUR--------------------------
Button retour = (Button) findViewById(R.id.retour);
retour.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Intent Intent_next = new Intent(jeux2.this, MainActivity.class);
startActivity(Intent_next);
}
});
//--------------------------HISTO--------------------------
TextView histo = (TextView) findViewById(R.id.histo);
histo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Intent Intent_next = new Intent(jeux2.this, historique.class);
startActivity(Intent_next);
}
});
}
}
class ChildDataModel
{
long id;
String txt;
String aventure;
public ChildDataModel(int id, String explications, String aventure) {
this.setId(id);
this.setTxt(explications);
}
public long getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTxt() {
return txt;
}
public void setTxt(String explications) {
this.txt = explications;
}
public String getTxt_aventure() {
return aventure;
}
@Override
public String toString()
{
return super.toString();
}
}
ExpandableListAdapter.java
package com.evo.tab2escape;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<Integer> headerData; // Images
// child data in format of header , child
private HashMap<Integer, ArrayList<ChildDataModel>> childData;
public ExpandableListAdapter(Context context, List<Integer> listDataHeader,
HashMap<Integer, ArrayList<ChildDataModel>> childData) {
this._context = context;
this.headerData = listDataHeader;
this.childData = childData;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this.childData.get(this.headerData.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) {
ChildDataModel child = (ChildDataModel) 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 explications = (TextView) convertView.findViewById(R.id.explications);
explications.setText(child.getTxt());
Button play = (Button) convertView.findViewById(R.id.play);
Button doc = (Button) convertView.findViewById(R.id.doc);
Button param = (Button) convertView.findViewById(R.id.param);
final String aventure = child.getTxt_aventure();
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Log.d("TAG", "poulet: ");
}
});
doc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Log.d("TAG", "poulet: ");
}
});
param.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Log.d("TAG", "poulet: ");
}
});
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this.childData.get(this.headerData.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
return this.headerData.get(groupPosition);
}
@Override
public int getGroupCount() {
return this.headerData.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent)
{
if (convertView == null)
{
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
ImageView logo = (ImageView) convertView.findViewById(R.id.logo);
int imageId = this.headerData.get(groupPosition);
logo.setImageResource(imageId);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
list_group.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Space
android:layout_width="match_parent"
android:layout_height="20dp">
</Space>
<ImageView
android:id="@+id/logo"
android:layout_width="match_parent"
android:scaleType="fitXY"
android:layout_height="100dp"/>
</LinearLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal"
android:id="@+id/divers">
<TextView
android:id="@+id/explications"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:gravity="center_vertical"
android:textStyle="bold"
android:layout_weight="1.5">
</TextView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal">
<Button
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Jouer"
android:layout_gravity="center"
android:background="@drawable/bouton_main"
android:layout_weight="1">
</Button>
<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
</Space>
<Button
android:id="@+id/doc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Documents"
android:layout_gravity="center"
android:background="@drawable/bouton_main"
android:layout_weight="1">
</Button>
<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
</Space>
<Button
android:id="@+id/param"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Paramètres"
android:layout_gravity="center"
android:background="@drawable/bouton_main"
android:layout_weight="1">
</Button>
<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
</Space>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
somoene可以帮助我吗?我要在单击按钮上开始新的意图。
编辑:我在MainActivity上添加子级Click侦听器,但仅当我在布局list_item.xml中的3个按钮上设置了clickable,focusable = false时,它才起作用。而且比单击的是完整的子项。我无法确定是单击按钮1、2还是3。我们可以解决它吗?
问题已解决。我在getChildView的ExpandableListAdapter中执行此操作。
param.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent next = new Intent(parent.getContext(), parametres.class);
parent.getContext().startActivity(next);
}
});