大家好我是android的新手我想知道如何使用多个父级实现expandablelistview
实际上我已经创建了一个,但问题是当我点击第二个父级中的某个datachild时它会占用第一个父级的datachild,所以我想要一个更准确的代码。
这是我的代码
package com.guide.directory.school.wcc.schooldirectoryandguidematerialdesign;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ExpandableListView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class ProgramsOffered extends ActionBarActivity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
@Override
public void onBackPressed() {
Intent intent = new Intent(ProgramsOffered.this, MainActivity.class);
startActivity(intent);
super.onBackPressed();
}
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
listDataHeader.add("Courses");
listDataHeader.add("WCC College Admision Test");
listDataHeader.add("WCC ATC Admission Requirements");
listDataHeader.add("Pricelist of Uniforms");
listDataHeader.add("Pricelist of Dormitories");
List<String> courses = new ArrayList<String>();
List<String> cat = new ArrayList<>();
List<String> atc = new ArrayList<>();
List<String> uniform = new ArrayList<>();
List<String> dorm = new ArrayList<>();
courses.add("BS Aviation Major in Commercial Flying");
courses.add("BS Aviation Major in Aviation Management");
courses.add("BS Aeronautical Engineering");
courses.add("BS Tourism with Flight Attendant Course");
courses.add("BS Aircraft Maintenance Technology");
courses.add("Diploma in Aircraft Maintenance Technology");
courses.add("Diploma in Avionics Technology");
cat.add("A. Testing Fee");
cat.add("B. Test Coverage");
cat.add("C. Time Coverage");
cat.add("D. Checking of CAT Paper");
cat.add("E. Releasing of CAT Result");
cat.add("F. Schedule of CAT");
atc.add("Freshmen");
atc.add("Transferees");
uniform.add("BS Comfly and AvMgt");
uniform.add("BS Tourism Mgt");
uniform.add("AeroEng, AMT, Dip AvTech");
dorm.add("Hotel");
dorm.add("Apartment");
dorm.add("Room");
listDataChild.put(listDataHeader.get(0), courses);
listDataChild.put(listDataHeader.get(1), cat);
listDataChild.put(listDataHeader.get(2), atc);
listDataChild.put(listDataHeader.get(3), uniform);
listDataChild.put(listDataHeader.get(4), dorm);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_programs_offered);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
expListView = (ExpandableListView) findViewById(R.id.list);
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
expListView.setAdapter(listAdapter);
expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
return false;
}
});
expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
int previousGroup = -1;
public void onGroupExpand(int paramInt) {
if (paramInt != this.previousGroup)
ProgramsOffered.this.expListView.collapseGroup(this.previousGroup);
this.previousGroup = paramInt;
Log.d("HAHA", paramInt + "");
}
});
expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
}
});
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView paramExpandableListView, View paramView, int paramInt1, int paramInt2, long paramLong) {
ProgramsOffered.this.listDataHeader.get(paramInt1);
ProgramsOffered.this.listDataChild.get(Integer.valueOf(paramInt2));
switch (paramInt2) {
default:
break;
case 0:
Intent intent = new Intent(ProgramsOffered.this, Course1.class);
startActivity(intent);
break;
case 1:
Intent intent1 = new Intent(ProgramsOffered.this, Course2.class);
startActivity(intent1);
break;
case 2:
Intent intent2 = new Intent(ProgramsOffered.this, Course3.class);
startActivity(intent2);
break;
case 3:
Intent intent3 = new Intent(ProgramsOffered.this, Course4.class);
startActivity(intent3);
break;
case 4:
Intent intent4 = new Intent(ProgramsOffered.this, Course5.class);
startActivity(intent4);
break;
case 5:
Intent intent5 = new Intent(ProgramsOffered.this, Course6.class);
startActivity(intent5);
break;
case 6:
Intent intent6 = new Intent(ProgramsOffered.this, Course7.class);
startActivity(intent6);
switch (paramInt2) {
default:
break;
case 0:
Intent intent7 = new Intent(ProgramsOffered.this, TestingFee.class);
startActivity(intent7);
break;
case 1:
Intent intent8 = new Intent(ProgramsOffered.this, TestCoverage.class);
startActivity(intent8);
}
}
return false;
}
});
expListView.expandGroup(0);
}
@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_programs_offered, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
Intent intent = new Intent(ProgramsOffered.this, MainActivity.class);
startActivity(intent);
break;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:0)
在可扩展列表视图中,每个子项都有一个groupPosition和一个childPosition,因此第一组中的第一个子组位于groupPosition 0,childPosition 0.第二组中的第一个子组是groupPosition 1,childPosition 0。 / p>
根据目前设置的方式,在onChildClickListener
中,paramInt1
是groupPosition,paramInt2
是子位置。您正在使用的Switch,Case仅检查paramInt2
,在我在第一段中提到的两种情况中,paramInt2
都为零。您需要检查哪个组(paramInt1)然后检查该组中的哪个孩子(paramInt2)。
执行此操作的一种方法是使用嵌套的Switch,Cases,因此在伪代码中:
switch(paramInt1) {
case 0:
switch(paramInt2) {
//this would be the first switch you used
}
break;
case 1:
switch (paramInt2) {
//this would be the switch for your second group
}
break;
}