ListView实现二级菜单

实现如上图所示的二级菜单:

1上面是通过两个ListView实现的,父LIstView控制子ListView,其中配置文件如下

<cn.doublemenu.com.view.CategoryList
    android:orientation="vertical" android:background="#88000000"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/categorylist"
    android:visibility="gone">
    <ImageView android:layout_height="wrap_content"
        android:layout_width="wrap_content" android:src="@drawable/trangle_indicate"
        android:layout_marginBottom="-22.0dip" android:layout_marginLeft="40.0dip" />
    <RelativeLayout android:layout_height="fill_parent"
        android:layout_width="fill_parent" android:background="@drawable/addresslist_bg"
        android:layout_marginRight="15.0dip" android:layout_marginLeft="15.0dip"
        android:layout_marginTop="15.0dip" android:layout_marginBottom="40.0dip">
        <LinearLayout android:layout_height="fill_parent"
            android:layout_width="fill_parent" android:orientation="horizontal"
            android:paddingTop="2.0dip" android:paddingLeft="1.0dip"
            android:paddingRight="1.0dip" android:paddingBottom="1.0dip">
            <ListView android:id="@+id/list_group" android:scrollbars="none"
                android:background="#00000000" android:layout_width="0.0px"
                android:layout_height="fill_parent" android:cacheColorHint="#00000000"
                android:divider="#ffbbbab7" android:dividerHeight="1.0px"
                android:layout_weight="1.0" />
            <ListView android:id="@+id/list_child" android:background="#f4f4f4"
                android:layout_width="0.0px" android:layout_height="fill_parent"
                android:cacheColorHint="#00000000" android:divider="@null"
                android:layout_weight="1.0" />
        </LinearLayout>
    </RelativeLayout>
</cn.doublemenu.com.view.CategoryList>


2自定义的cn.doublemenu.com.view.CategoryList如下:

package cn.doublemenu.com.view;

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

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import cn.doublemenu.com.activity.R;
import cn.doublemenu.com.bean.Category;
import cn.doublemenu.com.db.GenericDAO;

public class CategoryList extends LinearLayout {
    private List<Category> topCategories;
    private Map<Integer, List<Category>> categoriesMap;
    private GenericDAO dao;
    private Context context;
    private Listener listener;
    private ListView groupListView;
    private ListView childListView;
    private ChildAdapter adapter;
    private int positionOut;

    public interface Listener {
        public void onSelected(Category category, Category parentCategory);

        public void onScroll(Category category);
    }

    public CategoryList(Context c, AttributeSet attrs) {
        super(c, attrs);
        dao = GenericDAO.getInstance(c);
        context = c;
    }

    public void init(final Listener listener, Category category) {
        topCategories = dao.listCategories();
        categoriesMap = new HashMap<Integer, List<Category>>();
        this.listener = listener;
        adapter = new ChildAdapter(new ArrayList<Category>());
        groupListView = (ListView) findViewById(R.id.list_group);
        childListView = (ListView) findViewById(R.id.list_child);
        groupListView.setCacheColorHint(0);
        groupListView.setAdapter(new GroupAdapter());
        childListView.setAdapter(adapter);
        List<Category> childCategoryList = getCategories(0);
        adapter.setCategoryList(childCategoryList);
        adapter.notifyDataSetChanged();
        groupListView.setOnItemClickListener(new OnItemClickListener() {

         

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值