android PopupWindow嵌套ListView(模仿分类下拉菜单效果)

先看下UI效果


android PopupWindow嵌套ListView(模仿分类下拉菜单效果)_第1张图片

这就是使用PopupWindow嵌套ListView实现的,这个简单,不做多介绍直接上代码


1:布局文件



       
背景图片如下:

package com.example.demo;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {
	private TextView tv_text_age;
	private PopupWindow pop;
	ListView listView = null;
	private List datas;
	private SelectAgeAdapter mSelectAdapter;
	private LayoutInflater inflater;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initData();
		inflater = LayoutInflater.from(this);
		tv_text_age = (TextView) findViewById(R.id.tv_text_age);
		tv_text_age.setOnClickListener(this);
	}
	private void initData() {
		datas = new ArrayList();
		for(int i=0;i<50;i++){
			datas.add("helloworld-"+i);
		}
	}
	@Override
	public void onClick(View v) {
		if(v.getId()==R.id.tv_text_age){
			if(pop == null){
				listView = new ListView(this);
				listView.setDividerHeight(1);
				listView.setBackgroundResource(R.drawable.kge_feek_bg);
				listView.setCacheColorHint(0x00000000);
				mSelectAdapter = new SelectAgeAdapter();
				pop = new PopupWindow(listView, tv_text_age.getWidth(), LayoutParams.WRAP_CONTENT, true);
			}
			listView.setAdapter(mSelectAdapter);
			pop.setBackgroundDrawable(new ColorDrawable(Color.RED));//
			pop.showAsDropDown(tv_text_age, 0, 0);
			
			listView.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView parent, View view,
						int position, long id) {
					tv_text_age.setText(datas.get(position));
					dismissPopWindow();
				}
			});
		}
	}
	public void dismissPopWindow(){
		if(pop!=null&&pop.isShowing()){
			pop.dismiss();
		}
	}
	
	class SelectAgeAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return datas!=null&&datas.size()>0?datas.size():0;
		}

		@Override
		public Object getItem(int position) {
			return datas.get(position);
		}

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

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if(convertView==null){
				holder = new ViewHolder();
				convertView = inflater.inflate(R.layout.item, null);
				holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
				convertView.setTag(holder);
				
			}else{
				holder = (ViewHolder) convertView.getTag();
			}
			holder.tv_name.setText(datas.get(position));
			return convertView;
		}
	}
	class ViewHolder{
		TextView tv_name;
	}
}

实现效果:

android PopupWindow嵌套ListView(模仿分类下拉菜单效果)_第2张图片

你可能感兴趣的:(android)