91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

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
育儿| 舞阳县| 阳城县| 东乌珠穆沁旗| 商水县| 石台县| 广宁县| 庆云县| 襄城县| 松桃| 汤阴县| 新晃| 乐平市| 永顺县| 右玉县| 留坝县| 永济市| 安福县| 惠东县| 威信县| 桃园县| 故城县| 象山县| 慈溪市| 宁国市| 厦门市| 高碑店市| 临猗县| 曲靖市| 昌宁县| 五常市| 翁牛特旗| 长子县| 蓝山县| 南和县| 蓬安县| 黑山县| 吴川市| 宽城| 阿鲁科尔沁旗| 昌江|