android折叠展开自定义列表项测试

public class MainActivity extends Activity {
	private List<String> group;
	private List<List<String>> child;

	private ExpandableListAdapter adapter;
	private TextView timeView,durView,delView;
	private ImageView image;
	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>>();
		
		String g[]= { "test1", "test2", "test3","test3","test3" };
		String c[][] = {{ "aaa" },{ "a","b","c" },{},
				{ "111","222" ,"333","111","333","111","222" ,"333"},{}};
		addgroup( g );
		addchild( c );
		
		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);
					if(childPosition == 0){
						Log.d("tag", "group "+groupPosition+" first click");
					}
				}
				image = (ImageView) convertView.findViewById(R.id.image);
				timeView = (TextView) convertView.findViewById(R.id.title);
				durView = (TextView) convertView.findViewById(R.id.info);
				delView = (TextView) convertView.findViewById(R.id.delete);
				Log.d("tag", "group="+child.size()+",gp="+groupPosition);
				Log.d("tag", "child="+child.get(groupPosition).size()+",cp="+childPosition);
				if((child.size() > groupPosition)&&(child.get(groupPosition).size() > childPosition)){
					timeView.setText(child.get(groupPosition).get(childPosition).toString());
					durView.setText("test123456");
					timeView.setVisibility(View.VISIBLE);
					durView.setVisibility(View.VISIBLE);
					delView.setVisibility(View.VISIBLE);
					image.setVisibility(View.VISIBLE);
				}else {
					timeView.setVisibility(View.GONE);
					durView.setVisibility(View.GONE);
					delView.setVisibility(View.GONE);
					image.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.get(groupPosition).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(final 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;
				modifyChildVal(1, cnt);
				modifyGroupVal(0, 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 modifyChildVal(int index, int s) {
		List<String> tmpList = child.get(index);
		String tmp = s+"";
		tmpList.set(0, tmp);
		child.set(index, tmpList);
	}
	public void modifyGroupVal(int index, int s) {
		String tmp = "abc"+s;
		group.set(index, tmp);
	}
}

布局:

<?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" >

      <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/image"
            android:src="@drawable/icon" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">

                <TextView
                    android:id="@+id/title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="#666872" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >

                <TextView
                    android:id="@+id/info"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="#666872"
                    android:textSize="10dp" />
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right" >

            <TextView
                android:id="@+id/delete"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_centerVertical="true"
                android:layout_margin="12dp" 
                android:text="删除" />
        </LinearLayout>
    </LinearLayout>

</RelativeLayout>

效果:



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值