Android - 在ExpandableListView中渲染子视图到父视图中
创始人
2024-09-30 09:30:22
0

在Android中使用ExpandableListView来渲染子视图到父视图中,可以按照以下步骤进行:

  1. 创建一个适配器类,继承自BaseExpandableListAdapter。
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List parentItems;
    private Map> childItems;

    public MyExpandableListAdapter(Context context, List parentItems, Map> childItems) {
        this.context = context;
        this.parentItems = parentItems;
        this.childItems = childItems;
    }

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

    @Override
    public int getChildrenCount(int groupPosition) {
        String parent = parentItems.get(groupPosition);
        return childItems.get(parent).size();
    }

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

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        String parent = parentItems.get(groupPosition);
        return childItems.get(parent).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) {
        String parentItem = (String) getGroup(groupPosition);

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_item_parent, null);
        }

        TextView parentTextView = convertView.findViewById(R.id.parentTextView);
        parentTextView.setText(parentItem);

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        String childItem = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_item_child, null);
        }

        TextView childTextView = convertView.findViewById(R.id.childTextView);
        childTextView.setText(childItem);

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
  1. 在布局文件中定义ExpandableListView。

  1. 在Activity中设置适配器和数据。
public class MainActivity extends AppCompatActivity {
    private ExpandableListView expandableListView;
    private MyExpandableListAdapter expandableListAdapter;
    private List parentItems;
    private Map> childItems;

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

        expandableListView = findViewById(R.id.expandableListView);

        // 设置父项数据
        parentItems = new ArrayList<>();
        parentItems.add("Parent 1");
        parentItems.add("Parent 2");
        parentItems.add("Parent 3");

        // 设置子项数据
        childItems = new HashMap<>();
        childItems.put("Parent 1", Arrays.asList("Child 1-1", "Child 1-2", "Child 1-3"));
        childItems.put("Parent 2", Arrays.asList("Child 2-1", "Child 2-2", "Child 2-3"));
        childItems.put("Parent 3", Arrays.asList("Child 3-1", "Child 3-2", "Child 3-3"));

        expandableListAdapter = new MyExpandableListAdapter(this, parentItems, childItems);
        expandableListView.setAdapter(expandableListAdapter);
    }
}

在上述代码中,MyExpandableListAdapter类继承自BaseExpandableListAdapter,并根据父项和子项的数据来渲染父视图和子视图。在Activity中创建一个ExpandableListView,并设置

相关内容

热门资讯

黑科技科技(wepoke智能a... 黑科技科技(wepoke智能ai)wepower使用说明书(wepOkE)素来是真的有挂(确实有挂)...
黑科技模拟器(wepoke智能... WePoker透视辅助工具核心要点解析‌,黑科技模拟器(wepoke智能ai)wepoke是机器发牌...
黑科技脚本(wepokeai代... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩家在这款游戏中打牌都...
黑科技辅助挂(wepoke真的... 黑科技辅助挂(wepoke真的有挂)wepoke 软件(wEPOKE)固有真的有挂(有挂猫腻)-哔哩...
黑科技辅助挂(wepoke黑科... 黑科技辅助挂(wepoke黑科技)wepoke软件透明挂演示(wEpOke)一直真的有挂(真的有挂)...
黑科技app(wepoke真的... 此外,数据分析德州()辅助神器app还具备辅助透视行为开挂功能,通过对客户透明挂的深入研究,你可以了...
黑科技智能ai(wepoke辅... 黑科技智能ai(wepoke辅助插件)wepoke软件透明(WEPOke)先前是有挂(果真有挂)-哔...
黑科技模拟器(wepoke透明... 黑科技模拟器(wepoke透明黑科技)wepoke德州扑克用ai代打(WEPOKE)最初存在有挂(有...
黑科技插件(wepoke辅助插... 黑科技插件(wepoke辅助插件)wepoke软件透明(WEPOke)从来是真的有挂(有挂解惑)-哔...
黑科技美元局(wepoke智能... 黑科技美元局(wepoke智能ai)wepoke辅助真的假的(wepoKE)原先真的有挂(竟然有挂)...