ExpandableListView(可扩展的listView)

代码:
class MainActivity extends AppCompatActivity {

    private String[] groups={"好友","同学"};
    private String[][] childs={{"Tom","Jerry","Jeck"},{"XY","WX","YH"}};
    ExpandableListView el;
    MyExpandableAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        el= (ExpandableListView) findViewById(R.id.expand_listview);
        adapter=new MyExpandableAdapter(getBaseContext(),groups,childs);
        el.setAdapter(adapter);
        el.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                Toast.makeText(MainActivity.this, "当前位置"+childs[groupPosition][childPosition], Toast.LENGTH_SHORT).show();
                return true;
            }
        });
    }
}
**********************************************************************
public class MyExpandableAdapter extends BaseExpandableListAdapter {

    private String[] groups;
    private String[][] childs;
    Context mcotext;
    public MyExpandableAdapter(Context mcotext,String[] groups,String[][] childs) {
        this.mcotext=mcotext;
        this.groups=groups;
        this.childs=childs;
    }

    @Override
    public int getGroupCount() {
        return groups.length;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childs[groupPosition].length;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groups[groupPosition];
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childs[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 false;
    }




    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if(convertView==null){
            convertView= LayoutInflater.from(mcotext).inflate(R.layout.group_layout,null);
        holder=new ViewHolder();
        holder.iv= (ImageView) convertView.findViewById(R.id.iv);
        holder.tv= (TextView) convertView.findViewById(R.id.tv);
        convertView.setTag(holder);
        }else{
            holder= (ViewHolder) convertView.getTag();
        }
        holder.tv.setText(groups[groupPosition]);
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if(convertView==null){
            convertView= LayoutInflater.from(mcotext).inflate(R.layout.childs_layout,null);
            holder=new ViewHolder();
            holder.iv= (ImageView) convertView.findViewById(R.id.iv);
            holder.tv= (TextView) convertView.findViewById(R.id.tv);
            convertView.setTag(holder);
        }else{
            holder= (ViewHolder) convertView.getTag();
        }
        holder.tv.setText(childs[groupPosition][childPosition]);
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    ViewHolder holder;
    class ViewHolder{
        TextView tv;
        ImageView iv;
    }
}
布局代码:
(activity_main)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.kekuozhanlistview.MainActivity">

    <ExpandableListView
        android:layout_width="match_parent"
        android:id="@+id/expand_listview"
        android:layout_height="match_parent">
    </ExpandableListView>
</LinearLayout>

(childs_layout)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:paddingLeft="50dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ImageView
        android:id="@+id/iv"
        android:layout_width="40dp"
        android:layout_margin="10dp"
        android:src="@mipmap/ic_user_photo_default"
        android:layout_height="40dp" />

    <TextView
        android:layout_margin="10dp"
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:text="heheheheheh"
        android:layout_height="40dp" />
</LinearLayout>

(activity_main)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:paddingLeft="20dp"
    android:layout_height="match_parent">


    <ImageView
        android:id="@+id/iv"
        android:layout_width="40dp"
        android:layout_margin="10dp"
        android:src="@mipmap/ic_user_photo_default"
        android:layout_height="40dp" />

    <TextView
        android:id="@+id/tv"
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:text="哈哈哈哈啊哈"
        android:layout_height="40dp" />
</LinearLayout>

运行结果:


猜你喜欢

转载自blog.csdn.net/yang__k/article/details/80216566