ExpandableListView的使用

ExpandableListView的使用

1)、功能Activity:
public class Fourth extends Activity {
    private String url = "http://www.jcpeixun.com/app_client_api/course_lesson_list.aspx?courseid=2168&uid=450894";
    private ExpandableListView ex;
    private TextView text_fourth;//课程名
    private RequestQueue mRequestQueue;
    private Handler mHandler;
    private List<Item> course_name_list = new ArrayList<Item>();//总课程名
    private List<Item> chapter_list = new ArrayList<Item>();//章节名
    private List<Item> name_list;//列表项数据
    private List<ArrayList> list_item = new ArrayList<ArrayList>();//列表项汇总分类集合
    private
    class Item {
        String course_name;//总课程名
        String chapter;//章节名
        String name;//子列表项名
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.fourth);
        mRequestQueue = Volley.newRequestQueue(this);
        init();
        getData(url);
    }

    //初始化
    public void init() {
        text_fourth = (TextView) findViewById(R.id.text_fourth);
        ex = (ExpandableListView) findViewById(R.id.expandablelistview);
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.what == 0x001) {
                    assign((JSONObject) msg.obj);
                }
            }
        };
    }

    //赋值处理
    public void assign(JSONObject object) {
        try {
            Item item = new Item();
            item.course_name = object.getString("course_name");//总课程名
            course_name_list.add(item);//课程名

            //字体加粗
            TextPaint tp = text_fourth.getPaint();
            tp.setFakeBoldText(true);
            text_fourth.setText(course_name_list.get(0).course_name);

            JSONArray outline = object.getJSONArray("outline");
            for(int i=0;i<outline.length();i++){
                JSONObject object1 = outline.getJSONObject(i);
                Item item1 = new Item();
                item1.chapter = object1.getString("chapter");//章节
                chapter_list.add(item1);//每一批次章节名

                JSONArray array = object1.getJSONArray("lesson_list");
                name_list = new ArrayList<Item>();
                for(int j=0;j<array.length();j++){
                    JSONObject object2 = array.getJSONObject(j);
                    Item item2 = new Item();
                    item2.name = object2.getString("name");//子列表项
                    name_list.add(item2);//每一批次列表项内容
                    Log.e("name_list "+j, "" + name_list.get(j).name);
                }
                list_item.add((ArrayList) name_list);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        ExpandableAdapter mExpandableAdapter = new ExpandableAdapter();
        ex.setAdapter(mExpandableAdapter);

    }

    //获得json数据
    public void getData(String url) {
        JsonObjectRequest js = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        if (jsonObject.toString() != null) {
                            Message message = mHandler.obtainMessage(0x001);
                            message.obj = jsonObject;
                            mHandler.sendMessage(message);
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
        mRequestQueue.add(js);
    }
    //ExpandableListView适配器
    public class ExpandableAdapter extends BaseExpandableListAdapter{

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

        @Override
        public int getChildrenCount(int groupPosition) {
            return list_item.get(groupPosition).size();
        }

        @Override
        public Object getGroup(int groupPosition) {
            return chapter_list.get(groupPosition);
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return list_item.get(groupPosition).get(childPosition);
        }

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

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

        @Override
        public boolean hasStableIds() {
            //返回是否选中子选项
            return true;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertViewGroup, ViewGroup parent) {

            convertViewGroup = getLayoutInflater().inflate(R.layout.fourth_groupview,null);
            TextView text_groupView = (TextView) convertViewGroup.findViewById(R.id.text_groupView);
            text_groupView.setText(chapter_list.get(groupPosition).chapter);
            return convertViewGroup;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertViewChild, ViewGroup parent) {
            convertViewChild = getLayoutInflater().inflate(R.layout.fourth_childview, null);
            TextView text_childView = (TextView) convertViewChild.findViewById(R.id.text_childView);
            name_list = list_item.get(groupPosition);//强转为ArrayList<Item>

            text_childView.setText(name_list.get(childPosition).name);
            return convertViewChild;
        }

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

2)、activity布局fourth.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/text_fourth"
        android:layout_width="match_parent"
        android:gravity="center"
        android:layout_marginTop="14dp"
        android:textSize="22dp"
        android:layout_height="44dp" />

    <ExpandableListView
        android:id="@+id/expandablelistview"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="1dp"
        android:background="#ffffffff"
        android:isScrollContainer="false"/>

</LinearLayout>

3)、GroupView布局文件fourth_groupview.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="70dp">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Medium Text"
    android:id="@+id/text_groupView"
    android:layout_gravity="center_vertical"
    android:layout_marginLeft="35dp"
    android:textSize="24dp"
    android:textColor="#FF0000" />

</LinearLayout>

4)、ChildView布局fourth_childview.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="37dp"
    android:orientation="horizontal">


    <TextView
        android:id="@+id/text_childView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="1"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#666"
        android:layout_marginLeft="14dp"
        android:textSize="20dp" />

    <Button
        android:id="@+id/play_video"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp"
        android:background="#ffffffff"
        android:focusable="false"
        android:text="播放"
        android:textColor="#169dde" />
</LinearLayout>

5)、效果图:

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值