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

先看下UI效果


技术分享

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


1:布局文件

<span style="font-size:18px;"><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"
 >

       <TextView 
                android:id="@+id/tv_text_age"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/choolse_bg"
                android:text="不限"
                android:paddingLeft="12dp"
                android:textColor="#9d72d2"
                android:layout_centerInParent="true"
                android:gravity="center_vertical"
                />
</RelativeLayout></span>
背景图片如下:

技术分享

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<String> 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<String>();
		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;
	}
}

实现效果:

技术分享

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。