package com.loaderman.dropbox; import android.app.Activity; import android.graphics.drawable.ColorDrawable; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.EditText; import android.widget.ImageView; import android.widget.ListView; import android.widget.PopupWindow; import android.widget.TextView; import java.util.ArrayList; public class MainActivity extends Activity { private ImageView ivDrop; private ListView lvList; private EditText etInput; private ArrayList<String> mList; private PopupWindow popup; private MyAdapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ivDrop = (ImageView) findViewById(R.id.iv_drop); etInput = (EditText) findViewById(R.id.et_input); ivDrop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showPopup(); } }); lvList = new ListView(this); lvList.setBackgroundResource(R.drawable.listview_background); initData(); lvList.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { etInput.setText(mList.get(position)); popup.dismiss(); } }); } private void initData() { mList = new ArrayList<>(); for (int i = 0; i < 200; i++) { mList.add("用户名" + i); } mAdapter = new MyAdapter(); lvList.setAdapter(mAdapter); } private void showPopup() { popup = new PopupWindow(lvList, etInput.getWidth(), 200, true); popup.setBackgroundDrawable(new ColorDrawable()); popup.showAsDropDown(etInput, 0, 0); } class MyAdapter extends BaseAdapter { @Override public int getCount() { return mList.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = View.inflate(MainActivity.this, R.layout.list_item, null); holder = new ViewHolder(); holder.tvName = (TextView) convertView.findViewById(R.id.tv_name); holder.ivDelete = (ImageView) convertView.findViewById(R.id.iv_delete); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.tvName.setText(mList.get(position)); holder.ivDelete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mList.remove(mList.get(position)); mAdapter.notifyDataSetChanged(); } }); return convertView; } } static class ViewHolder { public TextView tvName; public ImageView ivDelete; } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:padding="10dp" tools:context="com.loaderman.dropbox.MainActivity"> <RelativeLayout android:layout_width="400dp" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_centerHorizontal="true"> <EditText android:id="@+id/et_input" android:layout_width="match_parent" android:layout_height="wrap_content"/> <ImageView android:id="@+id/iv_drop" android:layout_width="30dp" android:layout_height="30dp" android:layout_alignParentRight="true" android:layout_marginRight="5dp" android:layout_centerVertical="true" android:src="@drawable/down_arrow"/> </RelativeLayout> </RelativeLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal" android:padding="10dp"> <!--list_item--> <ImageView android:id="@+id/imageView" android:layout_width="30dp" android:layout_height="30dp" android:src="@drawable/user"/> <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="文字" android:textSize="18sp"/> <ImageView android:id="@+id/iv_delete" android:layout_width="30dp" android:layout_height="30dp" android:src="@drawable/delete"/> </LinearLayout>
效果图: