如何在Android的ViewPager中实现ExpandableList?

这是由Google提供的一个简单的1文件ExpandableList

/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.apis.view;

import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.widget.ExpandableListAdapter;
import android.widget.SimpleExpandableListAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Demonstrates expandable lists backed by a Simple Map-based adapter
 */
public class ExpandableList3 extends ExpandableListActivity {
    private static final String NAME = "NAME";
    private static final String IS_EVEN = "IS_EVEN";

    private ExpandableListAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
        List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
        for (int i = 0; i < 20; i++) {
            Map<String, String> curGroupMap = new HashMap<String, String>();
            groupData.add(curGroupMap);
            curGroupMap.put(NAME, "Group " + i);
            curGroupMap.put(IS_EVEN, (i % 2 == 0) ? "This group is even" : "This group is odd");

            List<Map<String, String>> children = new ArrayList<Map<String, String>>();
            for (int j = 0; j < 15; j++) {
                Map<String, String> curChildMap = new HashMap<String, String>();
                children.add(curChildMap);
                curChildMap.put(NAME, "Child " + j);
                curChildMap.put(IS_EVEN, (j % 2 == 0) ? "This child is even" : "This child is odd");
            }
            childData.add(children);
        }

        // Set up our adapter
        mAdapter = new SimpleExpandableListAdapter(
                this,
                groupData,
                android.R.layout.simple_expandable_list_item_1,
                new String[] { NAME, IS_EVEN },
                new int[] { android.R.id.text1, android.R.id.text2 },
                childData,
                android.R.layout.simple_expandable_list_item_2,
                new String[] { NAME, IS_EVEN },
                new int[] { android.R.id.text1, android.R.id.text2 }
                );
        setListAdapter(mAdapter);
    }

}


here是我学会使用viewpager的地方

现在我想一起使用它们,但我不知道如何:(

我试图将代码放入viewpager中的InstantiateIitem中,但是那没有用,我试图使viewpager成为可扩展ExpandableListActivity的活动,但是它给了我很多运行时异常,并且试图一次修复一个异常,但最终到达了现在,我一直在尝试那些对我来说不再有意义的东西,所以我停下来问这里

最佳答案

首先添加一个普通视图的传呼机,并在重写的方法InstantiateItem中添加

                if(position == 2) {// third page
                        View v = inflater.inflate(R.layout.expander, null, false);
                        ExpandableListView elv1 = (ExpandableListView) v.findViewById(R.id.elv1);
                        mAdapter = new BaseExpandableListAdapter() {

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

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

                    @Override
                    public int getChildrenCount(int groupPosition) {
                        return 3; // Size of children usually taken from
                                    // .length or .size()
                    }

                    @Override
                    public View getChildView(int groupPosition,
                            int childPosition, boolean isLastChild,
                            View convertView, ViewGroup parent) {
                        View v = inflater.inflate(R.layout.twolinelistitem,
                                null, false);
                        TextView tv = (TextView) v
                                .findViewById(R.id.simple_expandable_list_item_2_text1);
                        tv.setText("Child " + (childPosition + 1)
                                + " of group " + (groupPosition + 1));
                        return v;
                    }

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

                    @Override
                    public int getGroupCount() {
                        return 12; // Groups count usually taken from
                                    // .length or .size()
                    }

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

                    @Override
                    public View getGroupView(int groupPosition,
                            boolean isExpanded, View convertView,
                            ViewGroup parent) {
                        View v = inflater.inflate(R.layout.twolinelistitem,
                                parent, false);
                        TextView tv = (TextView) v
                                .findViewById(R.id.simple_expandable_list_item_2_text1);
                        tv.setText("Group " + (groupPosition + 1));
                        return v;
                    }

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

                    @Override
                    public boolean isChildSelectable(int groupPosition,
                            int childPosition) {
                        return true;
                    }
                };
                elv1.setAdapter(ViewPagerPlusExpandableListActivity.this.mAdapter);

                ((ViewPager) collection).addView(v, 0);
                return v;
            }


我确定您迷路了,这就是为什么我制作this example并将其发布在github上的原因

关于android - 如何在Android的ViewPager中实现可扩展列表?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7991632/

10-13 04:11