如何实现在Android的一个ViewPager的ExpandableList? [英] How to implement an ExpandableList in a ViewPager in Android?

查看:96
本文介绍了如何实现在Android的一个ViewPager的ExpandableList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在ViewPager在Android中实施ExpandableList?

这是由谷歌提供了一个简单的1文件ExpandableList

This is a simple 1 file ExpandableList provided by google

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

}

这里是哪里我学会了用viewpager

And here is where i learned to use the viewpager

现在我想用这些在一起,但我不知道如何:(

now i want to use those together, but i don't know how :(

我试图把code在InstantiateIitem的viewpager内,但没有工作,我试图使viewpager的activty的extenda ExpandableListActivity,但它给了我一堆运行时异常一直试图Ø修复一次,但在年底达到nowwhere,我一直在想的东西,没有任何意义,我了,所以我停下来问这里

I've tried to put the code in InstantiateIitem within the viewpager, but that didn't work, i tried making the viewpager an activty that extenda ExpandableListActivity, but it gave me a bunch of runtime exceptions kept trying o fix one at a time, but reached nowwhere at the end, i kept trying stuff that didn't make sense to me anymore so i stopped and asked here

推荐答案

首先加做一个普通视图寻呼机和覆盖的方法instantiateItem

First of all add make a normal view pager and in the overridden method 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;
            }

我相信你会丢失,这就是为什么我做这个例子并张贴上的github

I am sure you are lost, this is why i made this example and posted it on github

这篇关于如何实现在Android的一个ViewPager的ExpandableList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆