这是ExpandableListViewAdpater,包含下面的代码
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import java.util.HashMap;
import java.util.List;
public class ExpandableListViewAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> parentDataSource;
private HashMap<String, List<String>> childDataSource;
public ExpandableListViewAdapter(Context context, List<String> childParent, HashMap<String, List<String>> child) {
this.context = context;
this.parentDataSource = childParent;
this.childDataSource = child;
}
@Override
public int getGroupCount() {
return this.parentDataSource.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return this.childDataSource.get(this.parentDataSource.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return parentDataSource.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return this.childDataSource.get(parentDataSource.get(groupPosition)).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
View view = convertView;
if(view == null){
LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.parent_layout, parent, false);
}
String parentHeader = (String)getGroup(groupPosition);
TextView parentItem = (TextView)view.findViewById(R.id.parent_layout);
parentItem.setText(parentHeader);
return view;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View view = convertView;
if(view == null){
LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.child_layout, parent, false);
}
String childName = (String)getChild(groupPosition, childPosition);
TextView childItem = (TextView)view.findViewById(R.id.child_layout);
childItem.setText(childName);
return view;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
现在主要活动中的代码,所以我创建了一个名为Objetivo的新活动,我希望当我运行应用程序并单击Objetivo(可扩展列表视图中的子项)时,它会打开/转到创建的活动Objetivo。
package com.example.lucas.v3;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ExpandableListView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class MainActivity extends ActionBarActivity {
private ExpandableListView expandableListView;
private List<String>parentHeaderInformation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
parentHeaderInformation = new ArrayList<String>();
//Para adicionar item no menu, que abrira sub-menus.
parentHeaderInformation.add("Interferencia");
HashMap<String, List<String>> allChildItems = returnGroupedChildItems();
expandableListView = (ExpandableListView)findViewById(R.id.expandableListView);
ExpandableListViewAdapter expandableListViewAdapter = new ExpandableListViewAdapter(getApplicationContext(), parentHeaderInformation, allChildItems);
expandableListView.setAdapter(expandableListViewAdapter);
}
private HashMap<String, List<String>> returnGroupedChildItems(){
HashMap<String, List<String>> childContent = new HashMap<String, List<String>>();
// para cada capitulo, adicionar o codigo abaixo
List<String> Interferencia = new ArrayList<String>();
Interferencia.add("Tema");
Interferencia.add("Objetivo");
Interferencia.add("Desenvolvimento");
Interferencia.add("Aplicação");
Interferencia.add ("Exercicios");
Interferencia.add ("Referencia Bibliografica");
// para cada capitulo adicionado, adicionar o codigo seguinte onde o
//primeiro é o zero.
childContent.put(parentHeaderInformation.get(0), Interferencia);
return childContent;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:0)
您应该创建自己的自定义适配器。
创建一个新类,例如CustomExpandableListViewAdapter
延伸BaseExpandableListViewAdapter
。
在实施这些方法时,您必须覆盖getChildView
,并且您最终会得到类似的结果(不确定完全它,但你能够处理任何微小的差异)。
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View view = convertView;
convertView.setOnClickListener(new View.OnClickLister(){
@override
public void onClick(View v) {
if (childPosition==1) {
context.startActivity(...);
}
});
return view;
}
您必须通过构造函数传递context
,例如
CustomExpandableListViewAdapter(Context context) {
this.context = context;
}