android中expandablelistview的学习

android中经常需要用到expanablelistview,今天学习了expandablelistview,并附上源码

  首先定义parent布局的xml文件expandable_parent_item.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" >
    
	<ImageView 
	    android:id="@+id/imageid"
	    android:layout_width="0dp"
	    android:layout_height="wrap_content"
	    android:layout_weight="1"
	    />
	<TextView 
	    android:id="@+id/textid"
	    android:layout_width="0dp"
	    android:layout_height="wrap_content"
	    android:layout_weight="4"
	    android:gravity="center"
	    />
</LinearLayout>

child的布局文件如下,比较简单,只包含一个TextView,expandalbe_child_item.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"
     >
    
	<TextView 
	    android:id="@+id/textid"
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    />
</LinearLayout>

在实现自定义的adapter,继承自BaseExpandableListAdapter,,,,MyExpandableAdapter.java如下:

package com.example.expandablelistview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyExpandableAdapter extends BaseExpandableListAdapter {
	private Context context;
	
	private String [][]child = new String[][]{
			{"firstone","firsttwo","firstthree","firstfour"},
			{"secondone","secondtwo","secondthree","secondfour"},
			{"thirdone","thirdtwo","thirdthree"}
			
	};
	private int[]images = new int[]{R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};
	private String[]parent = new String[]{"parentone","parenttwo","parentthree"};
	
	
	public MyExpandableAdapter(Context context) {
		super();
		this.context = context;
	}

	@Override
	public Object getChild(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return child[arg0][arg1];
	}

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

	@Override
	public View getChildView(int arg0, int arg1, boolean arg2, View arg3,
			ViewGroup arg4) {
		// TODO Auto-generated method stub
		View view = LayoutInflater.from(context).inflate(R.layout.expandalbe_child_item,null);
		TextView textView = (TextView) view.findViewById(R.id.textid);
		textView.setText(child[arg0][arg1]);
		return view;
	}

	@Override
	public int getChildrenCount(int arg0) {
		// TODO Auto-generated method stub
		return child[arg0].length;
	}

	@Override
	public Object getGroup(int arg0) {
		// TODO Auto-generated method stub
		return parent[arg0];
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return parent.length;
	}

	@Override
	public long getGroupId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getGroupView(int arg0, boolean arg1, View arg2, ViewGroup arg3) {
		// TODO Auto-generated method stub
		View view = LayoutInflater.from(context).inflate(R.layout.expandable_parent_item,null);
		ImageView imageView = (ImageView) view.findViewById(R.id.imageid);
		TextView textView = (TextView) view.findViewById(R.id.textid);
		imageView.setImageResource(images[arg0]);
		textView.setText(parent[arg0]);
		return view;<pre name="code" class="java">package com.example.expandablelistview;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ExpandableListView;

public class MainActivity extends Activity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ExpandableListView listView = (ExpandableListView) findViewById(R.id.expandlistid);
		MyExpandableAdapter adapter = new MyExpandableAdapter(this);
		listView.setAdapter(adapter);
	}

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

}

}@Overridepublic boolean hasStableIds() {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean isChildSelectable(int arg0, int arg1) {// TODO Auto-generated method stubreturn false;}}



最后,MainActivity.java如下;

package com.example.expandablelistview;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ExpandableListView;

public class MainActivity extends Activity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ExpandableListView listView = (ExpandableListView) findViewById(R.id.expandlistid);
		MyExpandableAdapter adapter = new MyExpandableAdapter(this);
		listView.setAdapter(adapter);
	}

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

}

源码下载链接: http://download.csdn.net/detail/mockingbirds/8052699



你可能感兴趣的:(Android开发,ListView)