1 .MainActivity.java |
package com.example.acer.myapplication; |
import android.graphics.Color; |
import android.support.v7.app.AppCompatActivity; |
import android.os.Bundle; |
import android.view.View; |
import android.view.ViewGroup; |
import android.widget.BaseExpandableListAdapter; |
import android.widget.ExpandableListAdapter; |
import android.widget.ExpandableListView; |
import android.widget.LinearLayout; |
import android.widget.TextView; |
public class MainActivity extends AppCompatActivity{ |
protected void onCreate(Bundle savedInstanceState) { |
super .onCreate(savedInstanceState); |
setContentView(R.layout.activity_main); |
final ExpandableListAdapter adapter = new BaseExpandableListAdapter() { |
//设置组视图的显示文字 |
private String[] country = new String[] { "魏" , "蜀" }; |
//子视图显示文字 |
private String[][] name = new String[][] { |
{ "夏侯惇" , "甄姬" , "许褚" }, |
{ "马超" , "张飞" , "刘备" }, |
}; |
|
//重写ExpandableListAdapter中的各个方法 |
@Override |
public int getGroupCount() { |
return country.length; |
} |
@Override |
public Object getGroup( int groupPosition) { |
return country[groupPosition]; |
} |
@Override |
public long getGroupId( int groupPosition) { |
return groupPosition; |
} |
@Override |
public int getChildrenCount( int groupPosition) { |
return name[groupPosition].length; |
} |
@Override |
public Object getChild( int groupPosition, int childPosition) { |
return name[groupPosition][childPosition]; |
} |
@Override |
public long getChildId( int groupPosition, int childPosition) { |
return childPosition; |
} |
@Override |
public boolean hasStableIds() { |
return true ; |
} |
@Override |
public View getGroupView( int groupPosition, boolean isExpanded, |
View convertView, ViewGroup parent) { |
LinearLayout ll = new LinearLayout(MainActivity. this ); |
TextView textView = new TextView(MainActivity. this ); |
textView.setText(getGroup(groupPosition).toString()); |
textView.setTextSize( 30 ); |
textView.setPadding( 80 , 0 , 0 , 0 ); |
ll.addView(textView); |
return ll; |
} |
@Override |
public View getChildView( int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { |
LinearLayout ll = new LinearLayout(MainActivity. this ); |
TextView textView = new TextView(MainActivity. this ); |
textView.setText(getChild(groupPosition, childPosition).toString()); |
textView.setTextColor(Color.BLUE); |
textView.setTextSize( 20 ); |
ll.addView(textView); |
return ll; |
} |
@Override |
public boolean isChildSelectable( int groupPosition, int childPosition) { |
return true ; |
} |
}; |
ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.list); |
expandableListView.setAdapter(adapter); |
} |
} |
2 .activity_main.xml |
<?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> |
by: 发表于:2017-09-29 14:57:20 顶(0) | 踩(0) 回复
??
回复评论