效果如下图所示:
(个人使用 禁止商业使用)
public class ExUtils {
private static final String TAG_GET = "exutils--->";
public static String connectGet() {
HttpURLConnection urlConnection = null;
try {
URL url = new URL("http://www.handybest.com/index.php?m=Wx&c=Index&a=get_topic_info&id=34&view=");
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
// 判断请求是否成功
if (urlConnection.getResponseCode() == 200) {
// // 获取返回的数据
return readInStreams(in);
} else {
Log.i(TAG_GET, "Get方式请求失败");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
urlConnection.disconnect();
}
return null;
}
private static String readInStreams(InputStream in) {
Scanner scanner = new Scanner(in).useDelimiter("\\A");
return scanner.hasNext() ? scanner.next() : "";
}
/**
* 模拟数据-先使用本地json数据
*
* @return
*/
private String getJson() {
//Json数据的读写
try {
// InputStream is = this.getAssets().open("test.json");//eclipse
InputStream is = getClass().getClassLoader().getResourceAsStream("assets/" + "data.json");//android studio
BufferedReader bufr = new BufferedReader(new InputStreamReader(is));
String line;
StringBuilder builder = new StringBuilder();
while ((line = bufr.readLine()) != null) {
builder.append(line);
}
is.close();
bufr.close();
return builder.toString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
/***
* Gson 解析数据
*/
public LcBean gsonData() {
String strContentSS = getJson();
LcBean lcBean = null;
try {
//String strContent = connectGet();
Gson gson = new Gson();
lcBean = gson.fromJson(strContentSS, LcBean.class);
return lcBean;
} catch (JsonSyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
1)为一级列表添加数据方法:
/**一级列表数据**/
public ArrayList getGeneralsTypes() {
ArrayList generalsTypes = new ArrayList();
for (int i = 0; i < detailsList.size(); i++) {
generalsTypes.add(detailsList.get(i).getTag_title());
}
return generalsTypes;
}
private ArrayList generalsTypes = getGeneralsTypes();
2)为二级列表添加数据
public ArrayList> getGenerals(){
// Objects[][] objectsesGener=new Objects[detailsList.size()][];
ArrayList> generalss = new ArrayList>();
for (int i=0;iget(i);
List pict = detailsBean.getPict();
ArrayList dpict=new ArrayList();
for (int j=0;jget(j));
}
generalss.add(dpict);
}
Log.i("exutils---2>",generalss.toString());
return generalss;
}
private ArrayList> generals=getGenerals();
4.ExpandableListAdapter中的实现如下:
final ExpandableListAdapter adapter = new BaseExpandableListAdapter() {
//设置组视图的图片
// int[] logos = new int[] { R.drawable.ic_launcher, R.drawable.ic_launcher,R.drawable.ic_launcher};
// private String [][] generals=ExUtils.getItemChild();
//子视图图片
/*public int[][] generallogos = new int[][] {
{ R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher },
{ R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher },
{ R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher } };*/
public List detailsList= ExUtils.gsonData().getData().getDetails();
/**一级列表数据**/
public ArrayList getGeneralsTypes(){
ArrayList generalsTypes=new ArrayList();
for(int i=0;ireturn generalsTypes;
}
private ArrayList generalsTypes=getGeneralsTypes();
/**二级列表数据**/
public ArrayList> getGenerals(){
// Objects[][] objectsesGener=new Objects[detailsList.size()][];
ArrayList> generalss = new ArrayList>();
for (int i=0;i pict = detailsBean.getPict();
ArrayList dpict=new ArrayList();
for (int j=0;j"exutils---2>",generalss.toString());
return generalss;
}
private ArrayList> generals=getGenerals();
//自己定义一个获得文字信息的方法
TextView getTextView() {
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
TextView textView = new TextView(
MainActivity.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL);
textView.setPadding(36, 0, 0, 0);
textView.setTextSize(20);
textView.setTextColor(Color.BLACK);
return textView;
}
//重写ExpandableListAdapter中的各个方法
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return generalsTypes.size();
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return generalsTypes.get(groupPosition);
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return generals.get(groupPosition).size();
}
@Override
public DetailsBean.PictBean getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return generals.get(groupPosition).get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LinearLayout ll = new LinearLayout(
MainActivity.this);
// ll.setOrientation(0);
ImageView logo = new ImageView(MainActivity.this);
// logo.setImageResource(logos[groupPosition]);
logo.setPadding(50, 0, 0, 0);
ll.addView(logo);
TextView textView = getTextView();
textView.setTextColor(Color.BLACK);
textView.setText(getGroup(groupPosition).toString());
ll.addView(textView);
return ll;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LinearLayout ll = new LinearLayout(
MainActivity.this);
// ll.setOrientation(0);
// ImageView generallogo = new ImageView(
// MainActivity.this);
// generallogo
// .setImageResource(generallogos[groupPosition][childPosition]);
// ll.addView(generallogo);
TextView textView = getTextView();
textView.setText(getChild(groupPosition, childPosition).getContent());
ll.addView(textView);
Log.i("exutils---1>","groupPosition+childPosition=["+groupPosition+","+childPosition+"]");
Log.i("exutils---1>",getChild(groupPosition, childPosition).getContent());
return ll;
}
@Override
public boolean isChildSelectable(int groupPosition,
int childPosition) {
// TODO Auto-generated method stub
return true;
}
};
ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.list);
expandableListView.setAdapter(adapter);
//设置item点击的监听器
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(
MainActivity.this,
"你点击了" + adapter.getChild(groupPosition, childPosition),
Toast.LENGTH_SHORT).show();
return false;
}
});
}
代码下载地址:点我