popupWindow+listView实现qq登录下拉最近登录账号
效果图:
因为比较简单,我只贴出一部分稍微重要的代码块哈!!!
---------------主要思路:
## 1、建一个空的xml里面只有ListView、再建一个用于显示每一条记录的xml
文件名:list_view_layout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/item_list_view" android:layout_width="match_parent" android:layout_height="wrap_content"></ListView> </LinearLayout>
文件名:account_item_layout.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:clickable="true" android:id="@+id/relative_account" android:background="@drawable/item_relative" android:layout_width="300dp" android:layout_height="wrap_content" android:layout_marginTop="10dp" > <ImageView android:id="@+id/avatar" android:layout_width="50dp" android:layout_height="50dp" android:background="@drawable/gco" android:layout_marginLeft="10dp" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" /> <TextView android:id="@+id/account_item" android:layout_width="150dp" android:layout_height="wrap_content" android:textSize="20dp" android:layout_toEndOf="@id/avatar" android:layout_marginTop="20dp" android:layout_marginLeft="20dp" android:textColor="#fff" /> <TextView android:id="@+id/clear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/qya" android:layout_marginRight="20dp" android:layout_alignParentEnd="true" android:layout_marginTop="20dp" android:layout_marginLeft="20dp" android:textSize="0dp" /> </RelativeLayout>
## 2、在需要显示的xml的Java类中使用LayoutInflater得到ListView所在的xml,然后找出ListView为其设置一个适配器。实例一个PopupWindow,为其设置需要显示View、显示在什么位置、背景、焦点即可
LayoutInflater inflate=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
//把xml文件转换成view
View tempView=inflate.inflate(R.layout.list_view_layout,null);
// 从view里面找到item_list_view
listview = (ListView) tempView.findViewById(R.id.item_list_view);
//自定义一个适配器,用于显示我们需要下拉的内容
da=new CustomAdapter(list,login_activity.this,this);
//为listView 设置一个适配器
listview.setAdapter(da);
//实例一个popupWindow 设置宽度 设置高度
pw=new PopupWindow(tempView,accountEditText.getWidth(), ViewGroup.LayoutParams.WRAP_CONTENT); //设置背景
pw.setBackgroundDrawable(getResources().getDrawable(R.drawable.item_background)); pw.showAsDropDown(accountEditText);//显示在哪里 pw.setFocusable(true);//设置能否获得焦点 pw.update(); //监听PopupWindow关闭则改变图标 pw.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { itemicon.setBackground(getResources().getDrawable(R.drawable.fas)); } });
public class CustomAdapter extends BaseAdapter { List<UserEntity> list; LayoutInflater layoutInflater; AccountItemClickListener aic; Context context; Bitmap bitmap; int avatar; //定义一个构造方法 public CustomAdapter(List list, Context context,AccountItemClickListener aic){ this.list=list; this.context=context; layoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); //获取account_item_layout布局文件转换成view this.aic=aic; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return (list.get(position)).id; } @Override public View getView(int position, View convertView, ViewGroup parent) { View itemView=layoutInflater.inflate(R.layout.account_item_layout,null);//得到itemView里面的自定义好了的布局 RelativeLayout account_relative=(RelativeLayout)itemView.findViewById(R.id.relative_account);TextView tv=(TextView)account_relative.findViewById(R.id.account_item); tv.setText((list.get(position)).account); ImageView cus=(ImageView)account_relative.findViewById(R.id.avatar);cus.setBackgroundResource((list.get(position)).account);return itemView; }}