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

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;

	@Override
	public 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() {

			@Override
			public Object getChild(int groupPosition, int childPosition) {
				return child.get(childPosition);
			}

			@Override
			public long getChildId(int groupPosition, int childPosition) {
				return childPosition;
			}

			@Override
			public 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() {	
					@Override
					public void onClick(View v) {
						Toast.makeText(MainActivity.this, "group:"+groupPosition
								+",child:"+childPosition, Toast.LENGTH_SHORT).show();
					}
				});
				return convertView;
			}

			@Override
			public int getChildrenCount(int groupPosition) {
				return child.size();
			}

			@Override
			public Object getGroup(int groupPosition) {
				return group.get(groupPosition);
			}

			@Override
			public int getGroupCount() {
				return group.size();
			}

			@Override
			public long getGroupId(int groupPosition) {
				return groupPosition;
			}

			@Override
			public 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;
			}

			@Override
			public boolean hasStableIds() {
				return true;
			}

			@Override
			public 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值