扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

Android涓璭xpandablelistview鎬庝箞浣跨敤

扬州沐宇科技
2023-07-10 15:02:58
ExpandableListView

鍦ˋndroid涓娇鐢‥xpandableListView锛岄鍏堥渶瑕佸垱寤轰竴涓狝dapter鏉ユ彁渚涙暟鎹拰瀹氫箟鍒楄〃椤圭殑甯冨眬銆傜劧鍚庯紝鍦ˋctivity鎴朏ragment涓紝灏咵xpandableListView涓嶢dapter杩涜缁戝畾骞惰缃偣鍑讳簨浠躲€?/p>

浠ヤ笅鏄竴涓娇鐢‥xpandableListView鐨勭ず渚嬶細

  1. 鍒涘缓涓€涓狤xpandableListView鐨勫竷灞€鏂囦欢锛堜緥濡俥xpandable_list_layout.xml锛夛細
<ExpandableListView
android:id="@+id/expandableListView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
  1. 鍒涘缓涓€涓狝dapter绫绘潵鎻愪緵鏁版嵁鍜屽畾涔夊垪琛ㄩ」鐨勫竷灞€锛堜緥濡侲xpandableListAdapter.java锛夛細
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> groupList;
private Map<String, List<String>> childMap;
public ExpandableListAdapter(Context context, List<String> groupList, Map<String, List<String>> childMap) {
this.context = context;
this.groupList = groupList;
this.childMap = childMap;
}
@Override
public int getGroupCount() {
return groupList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
String groupName = groupList.get(groupPosition);
return childMap.get(groupName).size();
}
@Override
public Object getGroup(int groupPosition) {
return groupList.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
String groupName = groupList.get(groupPosition);
return childMap.get(groupName).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 true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.group_item_layout, null);
}
TextView groupNameTextView = convertView.findViewById(R.id.groupNameTextView);
groupNameTextView.setText(groupList.get(groupPosition));
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.child_item_layout, null);
}
TextView childNameTextView = convertView.findViewById(R.id.childNameTextView);
String groupName = groupList.get(groupPosition);
String childName = childMap.get(groupName).get(childPosition);
childNameTextView.setText(childName);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
  1. 鍦ˋctivity鎴朏ragment涓紝灏咵xpandableListView涓嶢dapter杩涜缁戝畾骞惰缃偣鍑讳簨浠讹紙渚嬪MainActivity.java锛夛細
public class MainActivity extends AppCompatActivity {
private ExpandableListView expandableListView;
private ExpandableListAdapter expandableListAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
expandableListView = findViewById(R.id.expandableListView);
// 鍒涘缓鍒嗙粍鏁版嵁
List<String> groupList = new ArrayList<>();
groupList.add("Group 1");
groupList.add("Group 2");
groupList.add("Group 3");
// 鍒涘缓瀛愰」鏁版嵁
Map<String, List<String>> childMap = new HashMap<>();
List<String> childList1 = new ArrayList<>();
childList1.add("Child 1-1");
childList1.add("Child 1-2");
childList1.add("Child 1-3");
childMap.put("Group 1", childList1);
List<String> childList2 = new ArrayList<>();
childList2.add("Child 2-1");
childList2.add("Child 2-2");
childMap.put("Group 2", childList2);
List<String> childList3 = new ArrayList<>();
childList3.add("Child 3-1");
childMap.put("Group 3", childList3);
// 鍒涘缓Adapter
expandableListAdapter = new ExpandableListAdapter(this, groupList, childMap);
// 缁戝畾Adapter
expandableListView.setAdapter(expandableListAdapter);
// 璁剧疆鐐瑰嚮浜嬩欢
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
String groupName = (String) expandableListAdapter.getGroup(groupPosition);
String childName = (String) expandableListAdapter.getChild(groupPosition, childPosition);
Toast.makeText(MainActivity.this, "Clicked: " + groupName + " - " + childName, Toast.LENGTH_SHORT).show();
return true;
}
});
}
}

浠ヤ笂浠g爜鍒涘缓浜嗕竴涓狤xpandableListView锛屽叾涓寘鍚?涓垎缁勶紙Group 1銆丟roup 2銆丟roup 3锛夛紝姣忎釜鍒嗙粍涓嬮潰鏈夎嫢骞插瓙椤广€傜偣鍑诲瓙椤规椂锛屼細寮瑰嚭涓€涓猅oast鏄剧ず鍒嗙粍鍜屽瓙椤圭殑鍚嶇О銆?/p>

娉ㄦ剰锛氬湪涓婅堪浠g爜涓紝group_item_layout.xml鍜宑hild_item_layout.xml鏄垎缁勫拰瀛愰」鐨勫竷灞€鏂囦欢锛屽彲浠ユ牴鎹疄闄呴渶姹傝繘琛岃嚜瀹氫箟銆?/p>

扫码添加客服微信