当前位置: 首页 > 工具软件 > PopupView > 使用案例 >

PopupView实现两级下拉列表

李文轩
2023-12-01

主要实现思想是定义两个textView作为下拉列表的点击入口,将PopupView作为下拉的列表,在点击的时候打开PopupView。

首先在布局文件中定义两个TextView,如下所示:

<LinearLayout
	android:id="@+id/start_lay"
	android:layout_width="match_parent"
	android:layout_height="wrap_content"
	android:layout_marginTop="10dp"
	android:orientation="horizontal" >

	<TextView
		android:id="@+id/start_line"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:background="@drawable/new_bg_metro_item"
		android:drawablePadding="10dp"
		android:drawableRight="@drawable/ic_cellclose"
		android:padding="10dp"
		android:text="1号线"
		android:textColor="@color/metro_black_text_color"
		android:textSize="18sp" />

	<TextView
		android:id="@+id/start_station"
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:layout_marginLeft="10dp"
		android:background="@drawable/new_bg_metro_item"
		android:drawablePadding="10dp"
		android:drawableRight="@drawable/ic_cellclose"
		android:padding="10dp"
		android:text="请选择站点"
		android:textColor="@color/metro_black_text_color"
		android:textSize="18sp" />
</LinearLayout>

然后在代码中初始化TextView及PopupView:

@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.popup_test);
		startLine = (TextView) findViewById(R.id.start_line);
		startStation = (TextView) findViewById(R.id.start_station);
		startLine.setOnClickListener(this);
		startStation.setOnClickListener(this);
		
		lineList=new ArrayList<String>();
				linelist.add("1号线");
				linelist.add("2号线");
		stationListArray=new SparseArray<List<String>>();
		for(int i=0;i<lineList.size();i++){
		    List<String> stationList=new ArrayList<String>();
			stationList.add("木渎站"+i);
			stationList.add("东环路"+i);
			stationList.add("临顿路"+i);
			stationList.add("时代广场站"+i);
			stationList.add("火车站"+i);
			stationListArray.put(i, stationList);
		}
    }
@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.start_line:{
				popupDataType=TYPE_LINE;//标记选择的是第一个还是第二个下拉列表			
				initPopupData(startLine);
				break;
			}
			case R.id.start_station:{
				popupDataType=TYPE_STATION;
				initPopupData(startStation);
				break;
			}
		}
	}
	//显示下拉列表
    private void initPopupData(final View targetView){
		int width=targetView.getWidth();
		View view=LayoutInflater.from(this).inflate(R.layout.layout_popup_listview, null);
		ListView listview=(ListView) view.findViewById(R.id.pop_list);
		if(popupDataType.equals(TYPE_LINE)){
		   if(popupAdapter==null){
				popupAdapter=new PopupAdapter(this, lineList);
				listview.setAdapter(popupAdapter);
			}else{
				popupAdapter.setData(lineList);
				listview.setAdapter(popupAdapter);
			}
		}else{
			if(stationAdapter==null){
				stationAdapter=new StationAdapter(this, currentStationList);
				listview.setAdapter(stationAdapter);
			}else{
				stationAdapter.setData(currentStationList);
				listview.setAdapter(stationAdapter);
			}
		}		
		//重新计算列表的宽度
		LayoutParams params=listview.getLayoutParams();
		params.width=width;
		listview.setLayoutParams(params);
		
		spinnerPopupWindow=new PopupWindow(view,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,true);
		//设置该背景,使点击其他地方时PopupView可消失
		spinnerPopupWindow.setBackgroundDrawable(new ColorDrawable(00000000));
		spinnerPopupWindow.showAsDropDown(targetView);//将列表显示在TextView的下方
		listview.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if(popupDataType.equals(TYPE_LINE)){
				//若当前点击的第一级下拉列表的item项,则重新给第二级下拉列表赋值				
					currentStationList=stationListArray.get(i));//获取当前线路下的站点信息
					((TextView)targetView).setText(parent.getAdapter().getItem(position).toString());
				}else{
				    if(currentStationList!=null){
					    ((TextView)targetView).setText(currentStationList.get(position).getStationName());
						((TextView)targetView).setTag(currentStationList.get(position));
					}
				}
				spinnerPopupWindow.dismiss();
			}
		});
	}





 类似资料: