android控件开发之ExpandableListActivity(二)
android控件开发之ExpandableListActivity(二)
import java.util.ArrayList;
import java.util.HashMap;
import android.os.Bundle;
import android.app.ListActivity;
import android.database.DataSetObserver;
import android.graphics.Color;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SimpleExpandableListAdapter;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.TextView;
/*
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//匿名内部类,重写ExpandableListAdapter类的方法
final ExpandableListAdapter ela = new ExpandableListAdapter() {
//设置组视图的图片
int[] logos = new int[] { R.drawable.ic_launcher, R.drawable.ic_launcher,R.drawable.ic_launcher};
//设置组视图的显示文字
private String[] generalsTypes = new String[] { "group1", "group2" };
//子视图显示文字
private String[][] generals = new String[][] {
{ "child1data1", "child1data2", "child1data3"},
{ "child2data1"}
};
//子视图图片
public int[][] generallogos = new int[][] {
{ R.drawable.list1, R.drawable.list2,
R.drawable.list3 },
{ R.drawable.list4 } };
//自己定义一个获得文字信息的方法
TextView getTextView() {
//设置textView的高度和宽度
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
TextView textView = new TextView(
MainActivity.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL);
textView.setPadding(10, 10, 10, 10);
textView.setTextSize(15);
textView.setTextColor(Color.BLACK);
return textView;
}
//重写ExpandableListAdapter中的各个方法
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return generalsTypes.length;
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return generalsTypes[groupPosition];
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return generals[groupPosition].length;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return generals[groupPosition][childPosition];
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//创建一个LinearLayout视图
LinearLayout ll = new LinearLayout(
MainActivity.this);
ll.setOrientation(0);
//创建一个ImageView对象
ImageView logo = new ImageView(MainActivity.this);
logo.setImageResource(logos[groupPosition]);
logo.setPadding(50, 0, 0, 0);
//在LinearLayout中添加一个ImageView
ll.addView(logo);
//创建一个TextView对象
TextView textView = getTextView();
textView.setTextColor(Color.BLACK);
textView.setText(getGroup(groupPosition).toString());
//将TextView添加到LinearLayout中
ll.addView(textView);
return ll;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//创建一个LinearLayout对象
LinearLayout ll = new LinearLayout(
MainActivity.this);
ll.setOrientation(0);
//创建一个ImageView对象
ImageView generallogo = new ImageView(
MainActivity.this);
generallogo.setImageResource(generallogos[groupPosition][childPosition]);
generallogo.setPadding(5, 5, 5, 5);
//将imageView对象添加到LinearLayout对象中
ll.addView(generallogo);
//创建一个TextView对象
TextView textView = getTextView();
textView.setText(getChild(groupPosition, childPosition)
.toString());
ll.addView(textView);
return ll;
}
@Override
public boolean isChildSelectable(int groupPosition,
int childPosition) {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean areAllItemsEnabled() {
// TODO Auto-generated method stub
return false;
}
@Override
public long getCombinedChildId(long groupId, long childId) {
// TODO Auto-generated method stub
return 0;
}
@Override
public long getCombinedGroupId(long groupId) {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
@Override
public void onGroupCollapsed(int groupPosition) {
// TODO Auto-generated method stub
}
@Override
public void onGroupExpanded(int groupPosition) {
// TODO Auto-generated method stub
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
}};
//给ExpandableListView设置SimpleExpandableListAdapter适配器
ExpandableListView expandableListView = (ExpandableListView)findViewById(android.R.id.list);
expandableListView.setAdapter(ela);
//绑定ExpandableListView child的监听器
expandableListView.setOnChildClickListener(new expandableChildLisenter());
}
//实现二级菜单监听器
class expandableChildLisenter implements OnChildClickListener{
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
System.out.println("group = " + groupPosition);
System.out.println("child = " + childPosition);
System.out.println("id = " + id);
return false;
}
}
@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;
}
}
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/myText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<!--设置ExpandableListView主界面的布局文件 -->
<ExpandableListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_below="@id/myText"
></ExpandableListView>
</RelativeLayout>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。