android 二级菜单、双ListView 仿美团、购物App二级菜单

android 二级菜单、双ListView 仿美团、购物二级菜单

下载Demo

效果如下图,下载后直接粘贴就可以用

android 二级菜单、双ListView 仿美团、购物App二级菜单_第1张图片    android 二级菜单、双ListView 仿美团、购物App二级菜单_第2张图片

Bean.java

package com.mengle.app;

import java.util.List;

public class Bean {

	/**
	 * result : Y address :
	 * [{"name":"北京","custId":"010","area":["东城区","西城区","崇文区"
	 * ,"宣武区","延庆县"]},{"name"
	 * :"上海","custId":"021","area":["黄浦区","卢湾区","徐汇区","长宁区","静安区","其他"]}]
	 */

	private String result;
	/**
	 * name : 北京 custId : 010 area : ["东城区","西城区","崇文区","宣武区","延庆县"]
	 */

	private List address;

	public void setResult(String result) {
		this.result = result;
	}

	public void setAddress(List address) {
		this.address = address;
	}

	public String getResult() {
		return result;
	}

	public List getAddress() {
		return address;
	}

	public static class AddressEntity {
		private String name;
		private String custId;
		private List area;

		public void setName(String name) {
			this.name = name;
		}

		public void setCustId(String custId) {
			this.custId = custId;
		}

		public void setArea(List area) {
			this.area = area;
		}

		public String getName() {
			return name;
		}

		public String getCustId() {
			return custId;
		}

		public List getArea() {
			return area;
		}
	}
}
MainActivity.java

package com.mengle.app;

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

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.mengle.app.Bean.AddressEntity;

public class MainActivity extends Activity {

    private DisplayMetrics dm;
    private PopupWindow popWindow;
    private MoreAdapter moreAdapter;
    private ListView morelist;
    private TextView tv;
    private List mainList;
    private ListView mainlist;
    private MainAdapter mainAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        dm = new DisplayMetrics();
        // 取得窗口属性
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        initModle();// 添加数据
        tv = (TextView) findViewById(R.id.tv);
        tv.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                downPopwindow();
            }
        });
    }

    private void downPopwindow() {
        // showAsDropDown(View anchor);相对某个控件的位置(正左下方),无偏移
        // showAsDropDown(View anchor, int x, int
        // y);相对某个控件的位置,有偏移;x表示相对x轴的偏移,正表示向左,负表示向右;y表示相对y轴的偏移,正是向下,负是向上;
        View contentView = LayoutInflater.from(this).inflate(R.layout.pop_down,
                null);
        int screenHeight = dm.heightPixels * 2 / 3;
        // 这里就给具体大小的数字,要不然位置不好计算
        popWindow = new PopupWindow(contentView, LayoutParams.MATCH_PARENT,
                screenHeight);
        // popWindow.setAnimationStyle(R.style.anim);// 淡入淡出动画
        // popWindow.setTouchable(false);// 是否响应touch事件
        popWindow.setFocusable(true);// 是否具有获取焦点的能力
        // 点击PopupWindow以外的区域,PopupWindow是否会消失。
        popWindow.setBackgroundDrawable(new BitmapDrawable());
        popWindow.setOutsideTouchable(true);

        mainlist = (ListView) contentView.findViewById(R.id.classify_mainlist);
        morelist = (ListView) contentView.findViewById(R.id.classify_morelist);

        mainAdapter = new MainAdapter(this, mainList);
        mainAdapter.setSelectItem(0);
        mainlist.setAdapter(mainAdapter);

        mainlist.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView parent, View view,
                    int position, long id) {
                List lists = mainList.get(position).getArea();
                initAdapter(lists);
                mainAdapter.setSelectItem(position);
                mainAdapter.notifyDataSetChanged();
            }
        });
        mainlist.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        // 一定要设置这个属性,否则ListView不会刷新
        initAdapter(mainList.get(0).getArea());

        morelist.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView parent, View view,
                    int position, long id) {
                String s = (String) moreAdapter.getItem(position);
                Toast.makeText(getApplicationContext(), s, 1).show();
                moreAdapter.setSelectItem(position);
                moreAdapter.notifyDataSetChanged();
            }
        });
        popWindow.showAsDropDown(tv);
    }

    private void initAdapter(List lists) {
        moreAdapter = new MoreAdapter(this, lists);
        morelist.setAdapter(moreAdapter);
        moreAdapter.notifyDataSetChanged();
    }

    private void initModle() {
        mainList = new ArrayList();
        Gson gson = new Gson();
        String json = "{\"result\":\"Y\", \"address\":[{\"name\":\"北京\",\"custId\":\""
                + R.drawable.ic_category_1
                + "\", \"area\":[\"东城区\",\"西城区\",\"崇文区\",\"宣武区\",\"延庆县\"]},{\"name\":\"上海\", \"custId\":\""
                + R.drawable.ic_category_2
                + "\",\"area\":[\"黄浦区\",\"卢湾区\",\"徐汇区\",\"长宁区\",\"静安区\",\"其他\"]}]}";
        java.lang.reflect.Type type = new TypeToken() {
        }.getType();
        Bean b = gson.fromJson(json, type);
        mainList.addAll(b.getAddress());
    }

}


activity_main.xml




    

pop_down.xml



    

    

MainAdapter.java

package com.mengle.app;

import java.util.List;

import com.mengle.app.Bean.AddressEntity;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainAdapter extends BaseAdapter {

	private Context context;
	private List list;
	private int position = 0;
	private Holder hold;

	public MainAdapter(Context context, List list) {
		this.context = context;
		this.list = list;
	}

	public int getCount() {
		return list.size();
	}

	public Object getItem(int position) {
		return list.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int arg0, View view, ViewGroup viewGroup) {

		if (view == null) {
			view = View.inflate(context, R.layout.item_mainlist, null);
			hold = new Holder(view);
			view.setTag(hold);
		} else {
			hold = (Holder) view.getTag();
		}
		hold.img.setImageResource(Integer.parseInt(list.get(arg0).getCustId()));
		hold.txt.setText(list.get(arg0).getName());
		hold.layout.setBackgroundColor(0xFFEBEBEB);
		if (arg0 == position) {
			hold.layout.setBackgroundColor(0xFFFFFFFF);
		}
		return view;
	}

	public void setSelectItem(int position) {
		this.position = position;
	}

	public int getSelectItem() {
		return position;
	}

	private static class Holder {
		LinearLayout layout;
		ImageView img;
		TextView txt;

		public Holder(View view) {
			txt = (TextView) view.findViewById(R.id.mainitem_txt);
			img = (ImageView) view.findViewById(R.id.mainitem_img);
			layout = (LinearLayout) view.findViewById(R.id.mainitem_layout);
		}
	}
}

item_mainlist.xml




    

        

        
    

MoreAdapter.java

package com.mengle.app;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class MoreAdapter extends BaseAdapter {

	private Context context;
	private int position = 0;
	Holder hold;
	private List lists;

	public MoreAdapter(Context context, List lists) {
		this.context = context;
		this.lists = lists;
	}

	public int getCount() {
		return lists.size();
	}

	public Object getItem(int position) {
		return lists.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int arg0, View view, ViewGroup viewGroup) {

		if (view == null) {
			view = View.inflate(context, R.layout.item_morelist, null);
			hold = new Holder(view);
			view.setTag(hold);
		} else {
			hold = (Holder) view.getTag();
		}
		hold.txt.setText(lists.get(arg0).toString());
		hold.txt.setTextColor(0xFF666666);
		if (arg0 == position) {
			hold.txt.setTextColor(0xFFFF8C00);
		}
		return view;
	}

	public void setSelectItem(int position) {
		this.position = position;
	}

	private static class Holder {
		TextView txt;

		public Holder(View view) {
			txt = (TextView) view.findViewById(R.id.moreitem_txt);
		}
	}
}
item_morelist.xml




    

你可能感兴趣的:(android,ListView)