实现如上图所示的二级菜单:
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() {