Android ExpandableListView 嵌套 GridView 二级菜单完整代码示例

本教程提供一个完整的 Android ExpandableListView 嵌套 GridView 的代码示例,展示如何创建二级菜单结构,并使用 GridView 显示子菜单内容。

1. 布局文件

activity_main.xml

<?xml version='1.0' encoding='utf-8'?>
<LinearLayout xmlns:android='http://schemas.android.com/apk/res/android'
    android:layout_width='match_parent'
    android:layout_height='match_parent'
    android:orientation='vertical'>

    <ExpandableListView
        android:id='@+id/expandableListView'
        android:layout_width='match_parent'
        android:layout_height='match_parent'
        android:groupIndicator='@null'
        android:dividerHeight='1dp'
        android:divider='@color/divider_color'/>

</LinearLayout>

group_item.xml

<?xml version='1.0' encoding='utf-8'?>
<LinearLayout xmlns:android='http://schemas.android.com/apk/res/android'
    android:layout_width='match_parent'
    android:layout_height='wrap_content'
    android:padding='16dp'
    android:orientation='horizontal'>

    <TextView
        android:id='@+id/groupTitle'
        android:layout_width='wrap_content'
        android:layout_height='wrap_content'
        android:textSize='18sp'
        android:textColor='@color/black'/>

</LinearLayout>

child_item.xml

<?xml version='1.0' encoding='utf-8'?>
<GridView xmlns:android='http://schemas.android.com/apk/res/android'
    android:id='@+id/gridView'
    android:layout_width='match_parent'
    android:layout_height='wrap_content'
    android:numColumns='3'
    android:horizontalSpacing='10dp'
    android:verticalSpacing='10dp'
    android:stretchMode='columnWidth'
    android:gravity='center'
    android:padding='10dp'/>

grid_item.xml

<?xml version='1.0' encoding='utf-8'?>
<TextView xmlns:android='http://schemas.android.com/apk/res/android'
    android:id='@+id/gridItem'
    android:layout_width='wrap_content'
    android:layout_height='wrap_content'
    android:textSize='16sp'
    android:textColor='@color/black'/>

2. Java 类

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private ExpandableListView expandableListView;
    private ExpandableListAdapter expandableListAdapter;
    private List<String> expandableListTitle;
    private HashMap<String, List<String>> expandableListDetail;

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

        expandableListView = findViewById(R.id.expandableListView);
        expandableListDetail = ExpandableListDataPump.getData();
        expandableListTitle = new ArrayList<>(expandableListDetail.keySet());
        expandableListAdapter = new CustomExpandableListAdapter(this, expandableListTitle, expandableListDetail);
        expandableListView.setAdapter(expandableListAdapter);
    }
}

ExpandableListDataPump.java

public class ExpandableListDataPump {

    public static HashMap<String, List<String>> getData() {
        HashMap<String, List<String>> expandableListDetail = new HashMap<>();

        List<String> fruits = new ArrayList<>();
        fruits.add('Apple');
        fruits.add('Banana');
        fruits.add('Mango');

        List<String> vegetables = new ArrayList<>();
        vegetables.add('Carrot');
        vegetables.add('Tomato');
        vegetables.add('Potato');

        expandableListDetail.put('Fruits', fruits);
        expandableListDetail.put('Vegetables', vegetables);

        return expandableListDetail;
    }
}

CustomExpandableListAdapter.java

public class CustomExpandableListAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List<String> expandableListTitle;
    private HashMap<String, List<String>> expandableListDetail;

    public CustomExpandableListAdapter(Context context, List<String> expandableListTitle,
                                       HashMap<String, List<String>> expandableListDetail) {
        this.context = context;
        this.expandableListTitle = expandableListTitle;
        this.expandableListDetail = expandableListDetail;
    }

    @Override
    public Object getChild(int listPosition, int expandedListPosition) {
        return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
                .get(expandedListPosition);
    }

    @Override
    public long getChildId(int listPosition, int expandedListPosition) {
        return expandedListPosition;
    }

    @Override
    public View getChildView(int listPosition, int expandedListPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        final String expandedListText = (String) getChild(listPosition, expandedListPosition);

        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.child_item, null);
        }

        GridView gridView = convertView.findViewById(R.id.gridView);
        final ArrayList<String> data = new ArrayList<>();
        for (int i = 0; i < 9; i++) {
            data.add(expandedListText + ' ' + (i+1));
        }
        gridView.setAdapter(new ArrayAdapter<>(this.context, R.layout.grid_item, data));

        return convertView;
    }

    @Override
    public int getChildrenCount(int listPosition) {
        return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
                .size();
    }

    @Override
    public Object getGroup(int listPosition) {
        return this.expandableListTitle.get(listPosition);
    }

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

    @Override
    public long getGroupId(int listPosition) {
        return listPosition;
    }

    @Override
    public View getGroupView(int listPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String listTitle = (String) getGroup(listPosition);
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.context.
                    getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.group_item, null);
        }
        TextView listTitleTextView = convertView
                .findViewById(R.id.groupTitle);
        listTitleTextView.setTypeface(null, Typeface.BOLD);
        listTitleTextView.setText(listTitle);
        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int listPosition, int expandedListPosition) {
        return true;
    }
}

3. 说明

本示例代码展示了如何使用 ExpandableListView 创建二级菜单,并使用 GridView 显示每个子菜单的内容。示例代码中:

  • ExpandableListDataPump.java 类用于生成菜单数据,其中包含两个组:'Fruits' 和 'Vegetables',以及每个组对应的子菜单项。
  • CustomExpandableListAdapter.java 类是 ExpandableListView 的自定义适配器,用于处理每个组和子菜单的视图显示。
  • child_item.xml 布局文件用于显示每个子菜单,它包含一个 GridView。
  • grid_item.xml 布局文件用于显示 GridView 中的每个子菜单项。

注意: 本示例代码仅用于演示 ExpandableListView 嵌套 GridView 的基本用法,实际应用中可能需要根据具体需求进行修改和扩展。

Android ExpandableListView 嵌套 GridView 二级菜单完整代码示例

原文地址: https://www.cveoy.top/t/topic/nG0K 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录