您的位置:首页 > 移动开发 > Android开发

Android ExpandableListView简单例子(附加时间模拟器)-FenGKun

2014-11-17 17:34 155 查看

android中常常要用到ListView,有时也要用到ExpandableListView,如在手机设置中,对于分类有很好的效果,会用ListView的人一定会用ExpandableListView,因为

ExpandableListView extends ListView的,下面来看个简单的例子

运行效果图:



一、创建布局文件main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#ccc"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<TextView
        android:id="@+id/tvTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
<ExpandableListView android:id="@+id/expendlist"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ExpandableListView>

</LinearLayout>
二、父控件栏的控件布局title_content.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="match_parent"
android:orientation="horizontal" >

<!-- 标题名 -->
<TextView android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"/>

</LinearLayout>
三、子控件栏的控件布局context_content.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="match_parent"
android:orientation="horizontal"
android:gravity="center_vertical" >

<!-- 内容图片 -->
<ImageView android:id="@+id/contextImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<!-- 内容文本 -->
<TextView android:id="@+id/contextText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"/>

</LinearLayout>


四、在主Activity中,MainActivity.java
package com.example.mytime;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

/** 现实时间控件 */
private TextView m_tvTime;
Handler handler;

/** 可扩展ListView */
private ExpandableListView m_expandableListView;

/** 标题文本 */
private List<String> list1 = new ArrayList<String>();

/** 内容文本 */
private List<String> contextList = new ArrayList<String>();

/** 内容图片 */
private List<Integer> contextList1 = new ArrayList<Integer>();

/** 文本集合 */
private List<List<String>> text_list = new ArrayList<List<String>>();

/** 图片集合 */
private List<List<Integer>> img_list = new ArrayList<List<Integer>>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUi();
}

/**
* 初始化数据
*/
private void initUi() {
m_tvTime=(TextView) findViewById(R.id.tvTime);
startAutoFlowTimer();

// 赋值文本
list1.add("A");
list1.add("B");
list1.add("C");

// 内容文本
contextList.add("Q");
contextList.add("W");
contextList.add("E");

// 文本集合
text_list.add(contextList);
text_list.add(contextList);
text_list.add(contextList);

// 内容图片
contextList1.add(R.drawable.ic_launcher);
contextList1.add(R.drawable.ic_launcher);
contextList1.add(R.drawable.ic_launcher);

// 图片集合
img_list.add(contextList1);
img_list.add(contextList1);
img_list.add(contextList1);

// 可扩展ListView
m_expandableListView = (ExpandableListView) findViewById(R.id.expendlist);
MyAdapter myAdapter = new MyAdapter();
m_expandableListView.setAdapter(myAdapter);

}

/**
* 自定义适配器
*/
class MyAdapter extends BaseExpandableListAdapter {

// 子控件的内容
public Object getChild(int arg0, int arg1) {
// TODO Auto-generated method stub
return text_list.get(arg0).get(arg1);
}

// 子控件的位置
public long getChildId(int arg0, int arg1) {
// TODO Auto-generated method stub
return arg1;
}

// 子控件内的控件赋值
public View getChildView(int arg0, int arg1, boolean arg2, View arg3,
ViewGroup arg4) {
if(arg3 == null) {
arg3 = LayoutInflater.from(MainActivity.this).inflate(R.layout.context_content, null);
}
// 内容图片
ImageView contextImg = (ImageView) arg3.findViewById(R.id.contextImage);
contextImg.setImageResource(img_list.get(arg0).get(arg1));
// 内容文本
TextView contextText = (TextView) arg3.findViewById(R.id.contextText);
contextText.setText(text_list.get(arg0).get(arg1));

return arg3;
}

// 子控件的个数
public int getChildrenCount(int arg0) {
// TODO Auto-generated method stub
return text_list.get(arg0).size();
}

// 父控件的内容
public Object getGroup(int arg0) {
// TODO Auto-generated method stub
return list1.get(arg0);
}

// 父控件的个数
public int getGroupCount() {
// TODO Auto-generated method stub
return list1.size();
}

// 父控件的位置
public long getGroupId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}

// 父控件内的控件赋值
public View getGroupView(int arg0, boolean arg1, View arg2,
ViewGroup arg3) {
if(arg2 == null) {
arg2 = LayoutInflater.from(MainActivity.this).inflate(R.layout.title_content, null);
}
TextView titText = (TextView) arg2.findViewById(R.id.tvName);
titText.setText(list1.get(arg0));
return arg2;
}

@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}

@Override
public boolean isChildSelectable(int arg0, int arg1) {
// TODO Auto-generated method stub
return true;
}

}

public void startAutoFlowTimer(){
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
String times = "";
times = times();
if(!m_tvTime.getText().toString().equals(times)) {
m_tvTime.setText(times);
}
sendEmptyMessageDelayed(1, 1000);
}
};
handler.sendEmptyMessage(1);
}

public String times(){
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date curDate = new Date(System.currentTimeMillis());//获取当前时间
String str = formatter.format(curDate);
Calendar calendar = Calendar.getInstance();
int week = calendar.get(Calendar.DAY_OF_WEEK);
switch (week) {
case 1:
str = str+" 星期日";
break;
case 2:
str = str+" 星期一";
break;
case 3:
str = str+" 星期二";
break;
case 4:
str = str+" 星期三";
break;
case 5:
str = str+" 星期四";
break;
case 6:
str = str+" 星期五";
break;
case 7:
str = str+" 星期六";
break;
default:
break;
}
return str;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: