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

溫馨提示×

如何自定義ExpandableListView

小樊
82
2024-10-14 13:28:10
欄目: 編程語言

要自定義ExpandableListView,你可以按照以下步驟進行:

  1. 創建布局文件:首先,你需要為ExpandableListView創建一個布局文件。這個文件將定義每個子項和組頭的布局。例如,你可以創建一個名為list_item_group.xml的文件來定義組頭的布局,以及一個名為list_item_child.xml的文件來定義子項的布局。
  2. 創建適配器:接下來,你需要創建一個適配器來填充和管理ExpandableListView的數據。你可以繼承BaseExpandableListAdapter類來實現自定義適配器。在適配器中,你需要實現getGroupViewgetChildView方法來分別返回組頭和子項的視圖。同時,你還需要實現getGroupIdgetGroupCountgetChildrenCountgetChildIdhasStableIds等方法來管理組和子項的數據。
  3. 設置適配器:最后,你需要在代碼中將自定義適配器設置給ExpandableListView。你可以通過調用setAdapter方法來實現這一點。

下面是一個簡單的示例代碼,展示了如何自定義ExpandableListView

// 創建布局文件
// list_item_group.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/group_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceListItemSmall" />
</LinearLayout>

// list_item_child.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingLeft="40dp">

    <TextView
        android:id="@+id/child_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceListItemSmall" />
</LinearLayout>

// 自定義適配器
public class CustomExpandableListAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List<String> groupList;
    private List<List<String>> childList;

    public CustomExpandableListAdapter(Context context, List<String> groupList, List<List<String>> childList) {
        this.context = context;
        this.groupList = groupList;
        this.childList = childList;
    }

    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childList.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childList.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 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.list_item_group, null);
        }

        TextView groupTitle = convertView.findViewById(R.id.group_title);
        groupTitle.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.list_item_child, null);
        }

        TextView childTitle = convertView.findViewById(R.id.child_title);
        childTitle.setText(childList.get(groupPosition).get(childPosition));

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

// 在Activity中使用自定義ExpandableListView
public class MainActivity extends AppCompatActivity {

    private ExpandableListView expandableListView;
    private CustomExpandableListAdapter adapter;
    private List<String> groupList;
    private List<List<String>> childList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        expandableListView = findViewById(R.id.expandable_list_view);

        // 初始化數據和適配器
        groupList = new ArrayList<>();
        childList = new ArrayList<>();

        // 添加組和子項數據
        groupList.add("Group 1");
        groupList.add("Group 2");
        groupList.add("Group 3");

        List<String> child1 = new ArrayList<>();
        child1.add("Child 1.1");
        child1.add("Child 1.2");

        List<String> child2 = new ArrayList<>();
        child2.add("Child 2.1");
        child2.add("Child 2.2");

        List<String> child3 = new ArrayList<>();
        child3.add("Child 3.1");
        child3.add("Child 3.2");

        childList.add(child1);
        childList.add(child2);
        childList.add(child3);

        // 設置適配器
        adapter = new CustomExpandableListAdapter(this, groupList, childList);
        expandableListView.setAdapter(adapter);
    }
}

以上示例代碼展示了如何創建自定義布局文件、自定義適配器和在Activity中使用自定義ExpandableListView。你可以根據自己的需求修改和擴展這些代碼。

0
石嘴山市| 菏泽市| 隆安县| 莱阳市| 江华| 长顺县| 林甸县| 塘沽区| 德兴市| 柳林县| 麻阳| 巫山县| 花莲县| 宣汉县| 勃利县| 博湖县| 松原市| 天全县| 丁青县| 亚东县| 仙居县| 南平市| 寻乌县| 洪江市| 云和县| 通州市| 大宁县| 英超| 观塘区| 鄂伦春自治旗| 车险| 灵石县| 广河县| 九寨沟县| 宜城市| 旌德县| 青冈县| 那曲县| 高邮市| 普定县| 车致|