android 2级listview的实现

android开发中,会遇见分类下面还有小分类的情况,这个可以很简单的用ExpandableListView来实现.

截图如下:


实现代码:主界面,main.java

package com.expandablelistview;

import java.util.ArrayList;
import java.util.List;

import com.hck.adpter.AllAdpter;


import android.app.Activity;
import android.os.Bundle;
import android.widget.ExpandableListView;

public class MainActivity extends Activity {
    private AllAdpter adpter;  //绑定数据的adpter
	private ExpandableListView listView; //控件
	private ArrayList
    
    
     
      fatherList;   //放置父类数据
	private List
     
     
      
      
       
       > childList;  //子类数据
	private List
       
       
        
         list;   //中间数据保存量
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		listView = (ExpandableListView) findViewById(R.id.all);  //获取控件对象
		initDate(); //初始化数据
		setAdpter();  //绑定数据
	}
	private void initDate() {
		fatherList=new ArrayList
        
        
          (); childList=new ArrayList 
          
          
            >(); /** * 初始化父listview */ fatherList.add("搞笑"); fatherList.add("原创"); fatherList.add("娱乐"); fatherList.add("资讯"); fatherList.add("汽车"); fatherList.add("体育"); /** * 初始化子listview */ for (int i = 0; i < fatherList.size(); i++) { initChild(i); //为每一个父类增加子类数据 } } private void initChild(int id) { list=new ArrayList 
           
             (); switch (id) { case 0: list.add("疯狂搞笑"); list.add("搞笑综艺"); list.add("原创搞笑"); list.add("经典搞笑"); childList.add(list); break; case 1: list.add("原创影视"); list.add("音乐动画"); list.add("火星搞笑"); list.add("网络红人"); childList.add(list); break; case 2: list.add("明星八卦"); list.add("影视资讯"); childList.add(list); break; case 3: list.add("社会"); list.add("国内"); childList.add(list); default: break; } } private void setAdpter() //绑定数据 { if (adpter==null) { adpter = new AllAdpter(fatherList, childList, this); listView.setAdapter(adpter); } } } 
            
           
          
        
       
       
      
      
     
     
    
    

适配器:AllAdpter


package com.hck.adpter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.expandablelistview.R;

public class AllAdpter extends BaseExpandableListAdapter {
    public List
   
   
    
     father;
	public List
    
    
     
     
      
      > chilerd;
	private Context context;

	public AllAdpter(List
      
      
       
        faList, List
       
        
        
          > chList, Context context) { //初始化数据 this.father = faList; this.chilerd = chList; this.context = context; } @Override public Object getChild(int groupPosition, int childPosition) { return chilerd.get(groupPosition).get(childPosition); //获取父类下面的每一个子类项 } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; //子类位置 } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { //显示子类数据的iew View view = null; view = LayoutInflater.from(context).inflate( R.layout.all_expand_list_item, null); TextView textView = (TextView) view .findViewById(R.id.all_list_text_item_id); textView.setText(chilerd.get(groupPosition).get(childPosition)); return view; } @Override public int getChildrenCount(int groupPosition) { return chilerd.get(groupPosition).size(); //子类item的总数 } @Override public Object getGroup(int groupPosition) { //父类数据 return father.get(groupPosition); } @Override public int getGroupCount() { return father.size(); 父类item总数 } @Override public long getGroupId(int groupPosition) { return groupPosition; //父类位置 } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { View view = LayoutInflater.from(context).inflate( R.layout.all_expand_list, null); TextView textView = (TextView) view.findViewById(R.id.all_list_text_id); textView.setText(father.get(groupPosition)); return view; } @Override public boolean hasStableIds() { return true; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { //点击子类触发事件 Toast.makeText(context, "第" + groupPosition + "大项,第" + childPosition + "小项被点击了", Toast.LENGTH_LONG).show(); return true; } } 
         
       
      
      
     
     
    
    
   
   


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"
    android:background="#EBEBEB"
    >
<TextView
     android:gravity="center_vertical"
    android:paddingLeft="25dp"
    android:textColor="#000000"
    android:layout_width="fill_parent"
    android:layout_height="45dp"
    android:textSize="16sp"
    android:id="@+id/all_list_text_item_id"
    />
</LinearLayout>




<?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:gravity="center_vertical"
    android:textColor="#000000"
    android:paddingLeft="3dp"
    android:layout_width="fill_parent"
    android:layout_height="35dp"
    android:textSize="20sp"
    android:id="@+id/all_list_text_id"
    />
</LinearLayout>




<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
     >

   <ExpandableListView
        android:paddingLeft="3dp"
        android:cacheColorHint="#00000000"
        android:groupIndicator="@null"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/all"
        />
</RelativeLayout>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值