多级组和子级的可扩展列表视图

时间:2014-03-19 12:04:36

标签: android expandablelistview expandablelistadapter

我想制作一个多级(超过3个)群组和孩子的Expandablelistview。

我在这里使用了这个项目http://www.phonesdevelopers.com/1723152

如果组和

我如何更改MyExpandableListAdapter类来完成我的任务 孩子们以阵列方式?

有人可以指导我吗?

public String[] groups = { "", "", "", "" };  
public String[][] children = {
{ "", "1", "2", "3" },
{ "", "" }, { "1", "2", "3" },
{ "", "1" } };

4 个答案:

答案 0 :(得分:0)

this tutoria,解释非常明确!

答案 1 :(得分:0)

您必须按以下方式覆盖以下方法

@Override
public Object getChild(int groupPosition, int childPosititon) {
    return this.children[groupPosition][childPosition];
}

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


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


@Override
public Object getGroup(int groupPosition) {
    return this.groups[groupPosition];
}

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

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

答案 2 :(得分:0)

**parent_view.xml**

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/textViewGroupName"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_marginLeft="5dp"
        android:gravity="center_vertical"
        android:text="@string/hello_world"
        android:textSize="23dp"
        android:textColor="#000000"
        android:padding="10dp"
        android:textSelectHandleLeft="@string/hello_world"
        android:background="#ffffff"

        android:drawableRight="@drawable/downward"
        android:textStyle="bold" /> 



**child_view.xml**

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="@android:color/white"
    android:clickable="true"
    android:orientation="vertical"
    android:paddingLeft="15dp"
    tools:context=".MainActivity" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="49dp"
        android:gravity="center_vertical" >



        <TextView
            android:id="@+id/textViewChild"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="55dp"
            android:text="@string/hello_world"
           android:layout_marginTop="10dp"
            android:textSize="17sp"
            android:textColor="#1919A3"
            android:textStyle="bold" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginTop="10dp"
            android:layout_marginRight="16dp"
            android:src="@drawable/next" />
        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_alignParentLeft="true"

            android:layout_marginLeft="10dp"
            android:background="@drawable/buy1" />

    </RelativeLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:color/white" />

</LinearLayout>

答案 3 :(得分:-1)

please follow code

public class ExpandableListMainActivity extends ExpandableListActivity {
    // Create ArrayList to hold parent Items and Child Items
    private ArrayList<String> parentItems = new ArrayList<String>();
    private ArrayList<Object> childItems = new ArrayList<Object>();
    private ArrayList<Integer> childimage = new ArrayList<Integer>();
    ImageView imageView2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Create Expandable List and set it's properties
        ExpandableListView expandableList = getExpandableListView();
        expandableList.setDividerHeight(2);
        expandableList.setGroupIndicator(null);
        expandableList.setClickable(true);
        // Set the Items of Parent
        setGroupParents();
        // Set The Child Data
        setChildData();
        // Create the Adapter

        MyExpandableAdapter adapter = new MyExpandableAdapter(parentItems,
                childItems, childimage);
        adapter.setInflater(
                (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE),
                this);
        // Set the Adapter to expandableList
        expandableList.setAdapter(adapter);
        expandableList.setOnChildClickListener(this);

    }

    // method to add parent Items
    public void setGroupParents() {

        parentItems.add("Chemistry");
        parentItems.add("Mathematics");
        parentItems.add("Physics");
    }

    // method to set child data of each parent
    public void setChildData() {
        // Add Child Items for Fruits
        ArrayList<String> child = new ArrayList<String>();

        child.add("Some Basic Concepts");
        child.add("States of Matter");
        child.add("Atomic Structure");
        childItems.add(child);
        childimage.add(R.drawable.check);
        childimage.add(R.drawable.buy2);
        childimage.add(R.drawable.buy1);

        child = new ArrayList<String>();

        child.add("Sets,Relations and Function");
        child.add("Complex Numbers");
        child.add("Quadratic Equations");

        childItems.add(child);
        childimage.add(R.drawable.check);
        childimage.add(R.drawable.check);
        childimage.add(R.drawable.buy1);

        child = new ArrayList<String>();
        child.add("Physics and Measurement");
        child.add("Kinematics");
        child.add("Laws of Motion");

        childItems.add(child);
        childimage.add(R.drawable.check);
        childimage.add(R.drawable.check);
        childimage.add(R.drawable.buy1);

    }

    public class MyExpandableAdapter extends BaseExpandableListAdapter {
        private Activity activity;
        private ArrayList<Object> childtems;
        private ArrayList<Integer> childimage;
        private LayoutInflater inflater;
        private ArrayList<String> parentItems, child;

        public MyExpandableAdapter(ArrayList<String> parents,
                ArrayList<Object> childern, ArrayList<Integer> childimage) {
            this.parentItems = parents;
            this.childtems = childern;
            this.childimage = childimage;
        }

        public void setInflater(LayoutInflater inflater, Activity activity) {
            this.inflater = inflater;
            this.activity = activity;
        }

        @SuppressWarnings("unchecked")
        @Override
        public View getChildView(int groupPosition, final int childPosition,
                boolean isLastChild, View convertView, ViewGroup parent) {

            child = (ArrayList<String>) childtems.get(groupPosition);
            TextView textView = null;

            if (convertView == null) {
                convertView = inflater.inflate(R.layout.child_view, null);
            }
            textView = (TextView) convertView.findViewById(R.id.textViewChild);
            String a = child.get(childPosition);

            textView.setText(child.get(childPosition));
            imageView2 = (ImageView) convertView.findViewById(R.id.imageView2);

            imageView2.setBackgroundResource(childimage.get(childPosition));

            convertView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (childPosition == 0 || childPosition == 1) {

                        Intent intent = new Intent(
                                ExpandableListMainActivity.this,
                                TestInstactionActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putString("papername", child.get(childPosition));
                        bundle.putString("testduration", "30 minute");
                        bundle.putString("totalquestion", "25");
                        bundle.putString("testmark", "100");
                        intent.putExtras(bundle);
                        startActivity(intent);
                    } else
                        Toast.makeText(getBaseContext(), "Please purchase",
                                Toast.LENGTH_SHORT).show();
                }
            });
            return convertView;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.parent_view, null);
            }
            ((CheckedTextView) convertView).setText(parentItems
                    .get(groupPosition));
            ((CheckedTextView) convertView).setChecked(isExpanded);
            return convertView;
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return null;
        }

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

        @SuppressWarnings("unchecked")
        @Override
        public int getChildrenCount(int groupPosition) {
            return ((ArrayList<String>) childtems.get(groupPosition)).size();
        }

        @Override
        public Object getGroup(int groupPosition) {
            return null;
        }

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

        @Override
        public void onGroupCollapsed(int groupPosition) {
            super.onGroupCollapsed(groupPosition);
        }

        @Override
        public void onGroupExpanded(int groupPosition) {
            super.onGroupExpanded(groupPosition);
        }

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

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

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

    }

}