1 . ExpandableListView布局:
<ExpandableListView
android:id="@+id/bbs_category_expandable_listview"
android:divider="#c0c0c0"
android:dividerHeight="1dip"
android:childDivider="#c0c0c0"
android:fadingEdgeLength="0dip"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:scrollbars="none"
android:listSelector="#00000000"/>
2 .ExpandableListView 父item布局:
里面的Imageview就是自定义的右边的箭头,这个位置可以随你需要自定义的
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/bbs_categrory_parent_list">
<TextView
android:id="@+id/bbs_category_group_listitem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:text="14sp"
android:textColor="#000000"
android:layout_gravity="left|center_vertical"/>
<ImageView
android:id="@+id/bbs_expandle_list_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:layout_marginRight="10dip"/>
</FrameLayout>
3. ExpandableListView 子item布局:
<?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="wrap_content"
android:background="@drawable/bbs_categrory_children_list">
<TextView
android:id="@+id/bbs_children_list_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_gravity="center_vertical"
android:textSize="14sp"
android:textColor="#777777"/>
</LinearLayout>
4.以下是Activity (l里面代码只是把expandablelistview相关代码贴出来了):
public class BbsCategoryListActivity extends BaseActivity {
private List<Forum> parentsForums=null; //存放板块二级数据
private List<List<Forum>> childrenForums=null; //存放板块三级数据
private List<Forum> forums;
private ExpandableListView expandablelistView;
private ExpandableListViewAdapter adapter ;
private TextView searchResultText;
private TextView searchResultIsNullText;
private long forumIndex;
private LayoutInflater layoutInflater =null;
private BbsApiService bbsApiService ;
private String forumType=null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bbs_category_list_activity);
expandablelistView = (ExpandableListView) this.findViewById(R.id.bbs_category_expandable_listview);
searchContent = (EditText) this.findViewById(R.id.bbs_search_keyword_field);
searchButton = (ImageButton) this.findViewById(R.id.bbs_forum_search_button);
backButton = (ImageButton) this.findViewById(R.id.bbs_category_listview_back);
searchResultText = (TextView) this.findViewById(R.id.bbs_category_searchresult_text);
searchResultIsNullText = (TextView) this.findViewById(R.id.bbs_category_searchresult_isnulltext);
searchResultText.setVisibility(View.GONE);
searchResultIsNullText.setVisibility(View.GONE);
adapter = new ExpandableListViewAdapter(this);
new HandleForumTread().start();
/**
* expandablelistView 三级数据点击事件
*/
expandablelistView.setOnChildClickListener(new OnChildClickListener() {
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
if(childrenForums.size()>0 && groupPosition>=0 && groupPosition<childrenForums.size() &&
childPosition>=0 && childPosition<childrenForums.get(groupPosition).size()){
long pid = childrenForums.get(groupPosition).get(childPosition).getPid();
String name = childrenForums.get(groupPosition).get(childPosition).getPname();
String forumLogoUrl = childrenForums.get(groupPosition).get(childPosition).getLogo();
if("favorite".equals(forumType)){//如果收藏板块
BbsMainFavoriteActivity.forumId = pid;
BbsMainFavoriteActivity.forumName = name;
BbsMainFavoriteActivity.forumLogoUrl = forumLogoUrl;
BbsMainFavoriteActivity.fromActivity="BbsCategoryListActivity";
finish();
}else{
//传递数据到帖子列表页面
Intent intent = new Intent(BbsCategoryListActivity.this,BbsTopicListActivity.class);
intent.putExtra("childrenForumId", pid);
intent.putExtra("forumName", name);
intent.putExtra("forumLogoUrl", forumLogoUrl);
startActivity(intent);
}
}
return false;
}
});
/**
* expandablelistView 二级数据点击事件
*/
expandablelistView.setOnGroupClickListener(new OnGroupClickListener() {
public boolean onGroupClick(ExpandableListView parent, View v,int groupPosition, long id) {
if(parentsForums.size()>0 && (parentsForums.get(groupPosition).getChildren()==null
|| !(parentsForums.get(groupPosition).getChildren().size()>0))){
long pid = parentsForums.get(groupPosition).getPid();
String name = parentsForums.get(groupPosition).getPname();
String forumLogoUrl = parentsForums.get(groupPosition).getLogo();
if("favorite".equals(forumType)){//如果收藏板块
BbsMainFavoriteActivity.forumId = pid;
BbsMainFavoriteActivity.forumName = name;
BbsMainFavoriteActivity.forumLogoUrl = forumLogoUrl;
BbsMainFavoriteActivity.fromActivity="BbsCategoryListActivity";
finish();
}else{
//传递数据到帖子列表页面
Intent intent = new Intent(BbsCategoryListActivity.this,BbsTopicListActivity.class);
intent.putExtra("childrenForumId", pid);
intent.putExtra("forumName", name);
intent.putExtra("forumLogoUrl", forumLogoUrl);
startActivity(intent);
}
}
return false;
}
});
//去掉默认的箭头
expandablelistView.setGroupIndicator(null);
}
/**
* ExpandableListview (显示树型列表)容器
* @author user
*
*/
public class ExpandableListViewAdapter extends BaseExpandableListAdapter {
Activity activity;
public ExpandableListViewAdapter(Activity activity){
this.activity = activity;
layoutInflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
//child method stub
public Object getChild(int ParentPosition, int childPosition) {
return childrenForums.get(ParentPosition).get(childPosition);
}
public long getChildId(int ParentPosition, int childPosition) {
return childPosition;
}
public int getChildrenCount(int ParentPosition) {
if(childrenForums.size()==0 || childrenForums.get(ParentPosition)==null){
return 0;
}
return childrenForums.get(ParentPosition).size();
}
//填充子item
public View getChildView(int ParentPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView==null){
holder = new ViewHolder();
convertView=(View)layoutInflater.inflate(R.layout.bbs_category_expandable_child_list_item, null);
holder.fourmText=(TextView)convertView.findViewById(R.id.bbs_children_list_item);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
if(childrenForums.size()>0 && ParentPosition<childrenForums.size()){
if(isLastChild){
convertView.setBackgroundResource(R.drawable.bbs_categrory_last_children_list);
}else{
convertView.setBackgroundResource(R.drawable.bbs_categrory_children_list);
}
if(childPosition<childrenForums.get(ParentPosition).size()){
Forum childrenForum = childrenForums.get(ParentPosition).get(childPosition);
holder.fourmText.setText(childrenForum.getPname());
}
}
return convertView;
}
//group method stub
public Object getGroup(int ParentPosition) {
return parentsForums.get(ParentPosition);
}
public int getGroupCount() {
if(null==parentsForums || parentsForums.size()==0){
return 0;
}
return parentsForums.size();
}
public long getGroupId(int ParentPosition) {
return ParentPosition;
}
//填充父item
public View getGroupView(int ParentPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView==null){
holder = new ViewHolder();
convertView=(View)layoutInflater.inflate(R.layout.bbs_category_expandable_group_list_item, null);
holder.imageView =(ImageView) convertView.findViewById(R.id.bbs_expandle_list_image);
holder.fourmText=(TextView)convertView.findViewById(R.id.bbs_category_group_listitem);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
if(parentsForums.size()>0 && ParentPosition<parentsForums.size()){
Forum parentForum = parentsForums.get(ParentPosition);
holder.fourmText.setText(parentForum.getPname());
//设置父item右边的箭头
if(parentsForums.get(ParentPosition).getChildren()!=null && parentsForums.get (ParentPosition).getChildren().size()!=0){
holder.imageView.setBackgroundResource(R.drawable.bbs_forum_parent_default_image);
}else{
holder.imageView.setBackgroundResource(0);
}
//如果展开时 替换右边的箭头
if(isExpanded && childrenForums.size()>0 && childrenForums.get(ParentPosition)!=null){
holder.imageView.setBackgroundResource(R.drawable.bbs_forum_parent_expand_image);
}
}
return convertView;
}
public boolean hasStableIds() {
return false;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
class ViewHolder {
ImageView imageView;
TextView fourmText;
}
}
转载于:https://www.cnblogs.com/code4app/p/4617823.html