亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

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
通化市| 民县| 丹东市| 颍上县| 元氏县| 五家渠市| 耒阳市| 克什克腾旗| 定边县| 郁南县| 廊坊市| 祁阳县| 乐都县| 菏泽市| 潼南县| 思茅市| 泾阳县| 奎屯市| 无极县| 曲周县| 凤山市| 日土县| 南江县| 惠东县| 曲阜市| 文登市| 新民市| 青河县| 盐亭县| 平和县| 云龙县| 荣成市| 澄城县| 祁门县| 华亭县| 蒙阴县| 门头沟区| 浏阳市| 德化县| 黑水县| 霍林郭勒市|