仿QQ好友列表的效果图
xml页面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--运用:如:QQ列表-->
<ExpandableListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/expandablelistview_1"></ExpandableListView>
</LinearLayout>
Java代码
package com.example.app11;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
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.TextView;
/**
* Created by Administrator on 2017/6/8 0008.
*/
public class ExpandableListViewActivity extends AppCompatActivity {
private ExpandableListView expandableListView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.expandablelistview);
expandableListView = (ExpandableListView) findViewById(R.id.expandablelistview_1);
expandable expandable=new expandable();
expandableListView.setAdapter(expandable);
}
class expandable implements ExpandableListAdapter {
int logals[] = {R.drawable.dog, R.drawable.elephant, R.drawable.fish};
String names[] = {"狗", "象", "鱼"};
String content[][] = {{"狗1", "狗2", "狗3"}, {"象1", "象2", "象3"}, {"鱼1", "鱼2", "鱼3"}};
@Override
public void registerDataSetObserver(DataSetObserver observer) {
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
}
@Override
public int getGroupCount() {
return names.length;
}
@Override
public int getChildrenCount(int groupPosition) {
return content[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
return names[groupPosition];
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return content[groupPosition][childPosition];
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@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(ExpandableListViewActivity.this);
ll.setOrientation(LinearLayout.HORIZONTAL);
ImageView logo = new ImageView(ExpandableListViewActivity.this);
logo.setImageResource(logals[groupPosition]);
logo.setPadding(36, 15, 0, 0);
ll.addView(logo);
TextView textView=getTextView();
textView.setText(getGroup(groupPosition).toString());
textView.setPadding(10,0,0,0);
ll.addView(textView);
return ll;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
TextView textView=getTextView();
textView.setText(getChild(groupPosition,childPosition).toString());
return textView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public void onGroupExpanded(int groupPosition) {
}
@Override
public void onGroupCollapsed(int groupPosition) {
}
@Override
public long getCombinedChildId(long groupId, long childId) {
return 0;
}
@Override
public long getCombinedGroupId(long groupId) {
return 0;
}
private TextView getTextView() {
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 64);
TextView textView = new TextView(ExpandableListViewActivity.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textView.setPadding(36, 0, 0, 0);
textView.setTextSize(20);
return textView;
}
}
}