如何在Android的ViewPager中实现可扩展列表?

如何在Android的ViewPager中实现可扩展列表?,android,Android,如何在Android中实现ViewPager中的可扩展列表? 这是谷歌提供的一个简单的1文件可扩展列表 /* * 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 Li

如何在Android中实现ViewPager中的可扩展列表?

这是谷歌提供的一个简单的1文件可扩展列表

/*
 * 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);
    }

}
/*
*版权所有(C)2007安卓开源项目
*
*根据Apache许可证2.0版(以下简称“许可证”)获得许可;
*除非遵守许可证,否则不得使用此文件。
*您可以通过以下方式获得许可证副本:
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
*除非适用法律要求或书面同意,软件
*根据许可证进行的分发是按“原样”进行分发的,
*无任何明示或暗示的保证或条件。
*请参阅许可证以了解管理权限和权限的特定语言
*许可证下的限制。
*/
包com.example.android.api.view;
导入android.app.ExpandableListActivity;
导入android.os.Bundle;
导入android.widget.ExpandableListAdapter;
导入android.widget.SimpleExpandableListAdapter;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
/**
*演示由简单的基于地图的适配器支持的可扩展列表
*/
公共类ExpandableList3扩展了ExpandableListActivity{
私有静态最终字符串NAME=“NAME”;
private static final String是_偶=“是_偶”;
私有可扩展列表适配器mAdapter;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
List groupData=new ArrayList();
List childData=new ArrayList();
对于(int i=0;i<20;i++){
Map curGroupMap=newhashmap();
groupData.add(curGroupMap);
curGroupMap.put(名称,“组”+i);
curGroupMap.put(是偶数,(i%2==0)?“此组是偶数”:“此组是奇数”);
List children=new ArrayList();
对于(int j=0;j<15;j++){
Map curChildMap=newhashmap();
添加(curChildMap);
curChildMap.put(名称,“Child”+j);
curChildMap.put(是偶数,(j%2==0)?“此子项是偶数”:“此子项是奇数”);
}
添加(子项);
}
//设置适配器
mAdapter=新的SimpleExpandableListAdapter(
这
分组数据,
android.R.layout.simple\u可扩展\u列表\u项目\u 1,
新字符串[]{NAME,是_偶数},
新int[]{android.R.id.text1,android.R.id.text2},
儿童数据,
android.R.layout.simple\u可扩展\u列表\u项目\u 2,
新字符串[]{NAME,是_偶数},
新int[]{android.R.id.text1,android.R.id.text2}
);
setListAdapter(mAdapter);
}
}
这也是我学会使用viewpager的地方

现在我想将它们结合使用,但我不知道如何:(


我曾尝试将代码放入viewpager中的InstanceItem中,但没有成功,我尝试使viewpager成为extenda ExpandableListActivity的一个活动,但它给了我一系列运行时异常,我一直试图一次修复一个,但到现在为止,我一直在尝试对我来说没有意义的东西,所以我停止了ed and Ask here

首先添加一个普通的视图寻呼机,并在重写的方法中实例化它们

                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;
            }

我肯定你迷路了,这就是我制作并发布它的原因。

首先添加一个普通的查看页面,并在重写的方法实例化它们

                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;
            }

我肯定你迷路了,这就是我制作并发布它的原因。

谢谢你给我指出那个链接,我已经添加了我尝试过的内容。很抱歉,看起来很愚蠢,但是:你在哪里添加了额外的信息?你的代码在哪里?你不笨,似乎我写的东西成功了。在找到如何做的方法后,我回答了自己的问题很好,希望你喜欢;)谢谢你给我指出那个链接,我已经添加了我尝试过的内容。很抱歉看起来很愚蠢,但是:你在哪里添加了额外的信息?你的代码在哪里?你不笨,看来我写的东西没问题了。在找到正确的方法后,我回答了自己的问题,希望你喜欢;)谢谢你的例子。我需要在我的viewpager中实现ListView。你可以发布一些例子,这样它会非常有用。提前感谢非常抱歉m8,我的约会即将开始,但我确信如果你做了一个可扩展的列表,然后看到了我的示例,然后看到了列表视图的教程,你将能够轻松完成。谢谢你的示例。我需要在我的viewpager中实现ListView。你可以发布一些例子,这样它会非常有用。提前感谢非常抱歉m8,我的约会即将开始,但我确信,如果你做了一个可扩展的列表,然后看到了我的示例,然后看到了列表视图的教程,你将能够轻松地完成它。