中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

Android中expandablelistview怎么使用

小億
90
2023-07-10 15:02:58
欄目: 編程語言

在Android中使用ExpandableListView,首先需要創建一個Adapter來提供數據和定義列表項的布局。然后,在Activity或Fragment中,將ExpandableListView與Adapter進行綁定并設置點擊事件。

以下是一個使用ExpandableListView的示例:

  1. 創建一個ExpandableListView的布局文件(例如expandable_list_layout.xml):
<ExpandableListView
android:id="@+id/expandableListView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
  1. 創建一個Adapter類來提供數據和定義列表項的布局(例如ExpandableListAdapter.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. 在Activity或Fragment中,將ExpandableListView與Adapter進行綁定并設置點擊事件(例如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;
}
});
}
}

以上代碼創建了一個ExpandableListView,其中包含3個分組(Group 1、Group 2、Group 3),每個分組下面有若干子項。點擊子項時,會彈出一個Toast顯示分組和子項的名稱。

注意:在上述代碼中,group_item_layout.xml和child_item_layout.xml是分組和子項的布局文件,可以根據實際需求進行自定義。

0
中超| 东乌| 登封市| 泽库县| 五家渠市| 鲜城| 太康县| 乌拉特后旗| 黄陵县| 广饶县| 沽源县| 电白县| 探索| 女性| 淳安县| 兖州市| 潞城市| 宜州市| 冕宁县| 双流县| 隆回县| 青阳县| 清苑县| 平塘县| 新蔡县| 临夏市| 福建省| 卢龙县| 三亚市| 宜兴市| 叙永县| 屏东市| 即墨市| 陇南市| 军事| 萝北县| 洱源县| 棋牌| 永嘉县| 红原县| 松桃|