關於ExpandableListView心得DEMO放在這給自己留個紀錄
1.設定group.xml
2.設定child.xml
3.設定MainActivity內容
4.設定BaseExpandableListAdapter
我直接PO出代碼, 有必要的部份再說明
1.設定group.xml
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:id="@+id/GroupTV"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:text="TextView"
android:textAlignment="center"
android:textColor="@android:color/black"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
2.設定child.xml
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:id="@+id/NameTV"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:text="TextView"
android:textAlignment="center"
android:textColor="@color/colorPrimaryDark"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
android:id="@+id/PhoneTV"
android:layout_width="113dp"
android:layout_height="21dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:text="TextView"
android:textAlignment="center"
android:textColor="@android:color/darker_gray"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.659"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/NameTV" />
3.設定MainActivity
package com.bryan4tw.expandblelvdemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
ExpandableListView expandableListView;
ExpandableListAdapter expandableListAdapter;
Listgroups;
Map>childs;
Map>child_phone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initdata();
expandableListView = (ExpandableListView)findViewById(R.id.ExListView);
expandableListAdapter =new myexpandableadapter(this,groups,childs,child_phone);
expandableListView.setAdapter(expandableListAdapter);
}
private void initdata(){
groups =new ArrayList<>();
childs =new HashMap<>();
child_phone =new HashMap<>();
groups.add("City A");
groups.add("City B");
ArrayList A =new ArrayList();
ArrayList B =new ArrayList();
A.add("Howard");
A.add("Bryan");
A.add("COCO");
B.add("Lima");
B.add("Allen");
B.add("Eric");
B.add("joey");
childs.put(groups.get(0),A);
childs.put(groups.get(1),B);
ArrayList a_p =new ArrayList();
ArrayList b_p =new ArrayList();
a_p.add("A11111");
a_p.add("B11111");
a_p.add("C11111");
b_p.add("A22222");
b_p.add("B22222");
b_p.add("C22222");
b_p.add("D22222");
child_phone.put(groups.get(0),a_p);
child_phone.put(groups.get(1),b_p);
}
}
4.設定BaseExpandableListAdapter(新增一個java class.命名:myexpandableadapter)
package com.bryan4tw.expandblelvdemo;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
import java.util.Map;
/**
* Created by dsall on 2018/1/19.
*/
public class myexpandableadapter extends BaseExpandableListAdapter{
Context context;
Listgroups;
Map>childs;
Map>child_phone;
public myexpandableadapter(Context context,Listgroups,Map>childs,Map>child_phone) {
this.context =context;
this.groups =groups;
this.childs =childs;
this.child_phone =child_phone;
}
@Override
public int getGroupCount() {
return groups.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return childs.get(groups.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
@Override
public Object getChild(int groupPosition,int childPosition) {
return childs.get(groups.get(groupPosition)).get(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) {
String group = (String)getGroup(groupPosition);
if(convertView ==null){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView =inflater.inflate(R.layout.group,null);
}
TextView grouptv = (TextView)convertView.findViewById(R.id.GroupTV);
grouptv.setText(group);
return convertView;
}
@Override
public View getChildView(int groupPosition,int childPosition,boolean isLastChild,View convertView,ViewGroup parent) {
String names = (String)getChild(groupPosition,childPosition);
final String phone = (String)child_phone.get(groups.get(groupPosition)).get(childPosition);
if(convertView ==null){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView =inflater.inflate(R.layout.child,null);
}
final TextView nametv = (TextView)convertView.findViewById(R.id.NameTV);
nametv.setText(names);
nametv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context,"Click " +nametv.getText().toString(),Toast.LENGTH_SHORT).show();
}
});
final TextView phonetv = (TextView)convertView.findViewById(R.id.PhoneTV);
phonetv.setText(phone);
phonetv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context,"Click " +phonetv.getText().toString(),Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition,int childPosition) {
return true;
}
}
以上都直接覆制贴上即可使用,
activity_main.xml裡自己拉一个ExpandableListView进去, id取名为 ExListView 即可执行
5.注意事項
控制child.xml的物件在myexpandableadapterl裡的getChildView做物件监听控制.
此代码同时传入两个值到child, 如果要增加, 可按相同模式增加, 记得MainActivity的传入与myexpandableadapterl的接收方都要一致才行.