android折叠展开列表动态修改显示测试

来源:互联网 发布:163邮箱设置smtp端口 编辑:程序博客网 时间:2024/05/16 02:26
package com.test;import java.util.ArrayList;import java.util.List;import java.util.Timer;import java.util.TimerTask;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.AbsListView;import android.widget.BaseExpandableListAdapter;import android.widget.ExpandableListAdapter;import android.widget.ExpandableListView;import android.widget.LinearLayout;import android.widget.RelativeLayout;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {private List<String> group;private List<List<String>> child;private ExpandableListAdapter adapter;private TextView tv;private Handler handler;private int cnt=0;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);handler = new Handler();group = new ArrayList<String>();child = new ArrayList<List<String>>();addgroup( new String[] { "test1", "test2", "test3","test3" });addchild( new String[][] {{ "aaa" },{ "a","b","c" },{ "111","222" ,"333" }});adapter = new BaseExpandableListAdapter() {@Overridepublic Object getChild(int groupPosition, int childPosition) {return child.get(childPosition);}@Overridepublic long getChildId(int groupPosition, int childPosition) {return childPosition;}@Overridepublic View getChildView(final int groupPosition, final int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {if (null == convertView || !(convertView instanceof RelativeLayout)) {convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.item, null);}tv = (TextView) convertView.findViewById(R.id.download_filename);if((child.size() > groupPosition)&&(child.get(groupPosition).size() > childPosition)){tv.setText(child.get(groupPosition).get(childPosition).toString());tv.setVisibility(View.VISIBLE);}else {tv.setVisibility(View.GONE);}convertView.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(MainActivity.this, "group:"+groupPosition+",child:"+childPosition, Toast.LENGTH_SHORT).show();}});return convertView;}@Overridepublic int getChildrenCount(int groupPosition) {return child.size();}@Overridepublic Object getGroup(int groupPosition) {return group.get(groupPosition);}@Overridepublic int getGroupCount() {return group.size();}@Overridepublic long getGroupId(int groupPosition) {return groupPosition;}@Overridepublic View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {LinearLayout ll = new LinearLayout(MainActivity.this);TextView textView = getTextView();textView.setText(getGroup(groupPosition).toString());ll.addView(textView);return ll;}public TextView getTextView() {AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 100);TextView textView = new TextView(MainActivity.this);textView.setLayoutParams(lp);textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);textView.setPadding(80, 0, 0, 0);return textView;}@Overridepublic boolean hasStableIds() {return true;}@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {return true;}};ExpandableListView expandListView = (ExpandableListView) findViewById(R.id.list);expandListView.setAdapter(adapter);test();}private void test(){new Timer().schedule(new TimerTask() {public void run() {cnt=cnt++>20?0:cnt;modifyVal(1, cnt);Runnable updater = new Runnable() {public void run() {((BaseExpandableListAdapter) adapter).notifyDataSetChanged();}};handler.post(updater);};}, 0, 3000);}public void addgroup(String[] c) {for (int i = 0; i < c.length; i++) {group.add(c[i]);}}public void addchild(String[][] c) {for (int i = 0; i < c.length; i++) {List<String> tmpList = new ArrayList<String>();for (int j = 0; j < c[i].length; j++) {tmpList.add(c[i][j]);}child.add(tmpList);}}public void modifyVal(int index, int s) {List<String> tmpList = child.get(index);String tmp = s+"";tmpList.set(0, tmp);child.set(index, tmpList);}}

布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <ExpandableListView        android:id="@+id/list"        android:layout_width="fill_parent"        android:layout_height="fill_parent" >    </ExpandableListView></LinearLayout>
子项布局:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="wrap_content" >    <TextView        android:id="@+id/download_filename"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_marginLeft="60dp"        android:maxLines="1"        android:textAppearance="?android:attr/textAppearanceMedium" >    </TextView></RelativeLayout>

效果:



0 0