有个需求:一有数据就显示列表提示,让用户选择是哪个数据。由于spinner定制性差,所以选择popupWindow+listView实现
package com.blzt.test;
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import java.util.ArrayList;
public class PopupwindowActivity extends Activity {
private Button button;
private ArrayList list = new ArrayList();
private ListView listView;
private MyAdapter adapter;
private boolean isShow;
private PopupWindow popupWindow;
private TextView mPopupTv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_popupwindow);
button = (Button) findViewById(R.id.button);
mPopupTv = (TextView) findViewById(R.id.popup_tv);
initDate();
initPop();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(popupWindow.isShowing()){
popupWindow.dismiss();
}else{
// 控制popupwindow显示的位置,在textiew下面
popupWindow.showAsDropDown(mPopupTv);
}
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> adapterView, View view, int i, long l) {
// button.setText(list.get(i)+"");
mPopupTv.setText(list.get(i)+"");
popupWindow.dismiss();
}
});
}
@Override
protected void onResume() {
super.onResume();
// popupWindow.showAsDropDown(button);
}
private void initPop() {
View view= LayoutInflater.from(this).inflate(R.layout.listview,null);
adapter=new MyAdapter(this,list);
listView= (ListView) view.findViewById(R.id.listView);
listView.setAdapter(adapter);
popupWindow =new PopupWindow(view,
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setFocusable(true);
popupWindow.setContentView(view);
// popupWindow.showAtLocation(view, Gravity.LEFT,0,0);
}
private void initDate() {
list.add("0");
list.add("1");
list.add("2");
}
}
package com.blzt.test;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class MyAdapter extends BaseAdapter {
private Context context;
private ArrayList list;
public MyAdapter(Context context,ArrayList list){
this.context=context;
this.list=list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
ViewHolder vh;
if (convertView == null) {
vh = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.item, null);
vh.textview = (TextView) convertView.findViewById(R.id.tv_text);
convertView.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}
vh.textview.setText(list.get(position) + "");
return convertView;
}
static class ViewHolder {
TextView textview;
}
}
activity_popupwindow
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
>
<Button
android:id="@+id/button"
android:layout_width="80dp"
android:layout_height="40dp"
android:background="#10000000"
android:gravity="center_vertical"
android:text="选择状态" />
<TextView
android:id="@+id/popup_tv"
android:text="@string/add"
android:layout_width="200dp"
android:layout_height="200dp"/>
</LinearLayout>
item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:background="#10000000"
android:id="@+id/tv_text"
android:layout_width="match_parent"
android:layout_height="30dp" />
</LinearLayout>
listview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ListView
android:background="#10000000"
android:id="@+id/listView"
android:layout_width="80dp"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>