android - 如何在另一个线性布局的线性布局中创建可扩展 ListView

标签 android expandablelistview expandablelistadapter

我尝试了很多次,但我不知道为什么我做不到。在我声明可扩展列表后查看:

listView =(ExpandableListView)findViewById(R.id.expandableListView1);

然后我创建我的适配器;

public class MyExpandableListAdapter extends BaseExpandableListAdapter {
        // Sample data set.  children[i] contains the children (String[]) for groups[i].
        private String[] groups = { "People Names", "Dog Names", "Cat Names", "Fish Names" };
        private String[][] children = {
                { "Arnold", "Barry", "Chuck", "David" },
                { "Ace", "Bandit", "Cha-Cha", "Deuce" },
                { "Fluffy", "Snuggles" },
                { "Goldy", "Bubbles" }
        };

        public Object getChild(int groupPosition, int childPosition) {
            return children[groupPosition][childPosition];
        }

        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        public int getChildrenCount(int groupPosition) {
            return children[groupPosition].length;
        }

        public TextView getGenericView() {
            // Layout parameters for the ExpandableListView
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                    ViewGroup.LayoutParams.FILL_PARENT, 64);

            TextView textView = new TextView(ExpandableList1.this);
            textView.setLayoutParams(lp);
            // Center the text vertically
            textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
            // Set the text starting position
            textView.setPadding(36, 0, 0, 0);
            return textView;
        }

        public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
                View convertView, ViewGroup parent) {
            TextView textView = getGenericView();
            textView.setText(getChild(groupPosition, childPosition).toString());
            return textView;
        }

        public Object getGroup(int groupPosition) {
            return groups[groupPosition];
        }

        public int getGroupCount() {
            return groups.length;
        }

        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
                ViewGroup parent) {
            TextView textView = getGenericView();
            textView.setText(getGroup(groupPosition).toString());
            return textView;
        }

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

        public boolean hasStableIds() {
            return true;
        }

    }

然后我将我的适配器设置为 listView。

mAdapter = new MyExpandableListAdapter();
        setListAdapter(mAdapter);

但是有太多的问题我无法创造; 如果你帮助我,我会很高兴。 谢谢。

最佳答案

除非您发布错误日志但尝试引用这些链接,否则我们找不到错误:

ExpandableListView on Android

Simple ExpandableListView Demo

Custom ExpandableListView on Android

希望对你有帮助

关于android - 如何在另一个线性布局的线性布局中创建可扩展 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10690439/

相关文章:

android - 如何更改 SimpleExpandableListAdapter 中的文本颜色

android - 单击按钮时以编程方式将 fragment 添加到 Activity

java - 使用 Proguard 后通过反射访问方法时出现 NoSuchMethodException

android - ExpandableListView 和 getItemViewType

java - 在 ExpandableListView 中,如何在子结果的末尾显示额外的一行?

Android Expandable ListView - 如何平滑滚动到组内的子位置?

android - ExpandableListView 的 SimpleCursorTreeAdapter 和 CursorLoader

Android 键盘与 EditText 重叠

java - 我需要解释 Android 的 JNI 代码行

java - 如何使 expandablelistview 的 child 可点击并做其他事情?