《自訂群組件》下拉快速選擇組件,《自訂群組件》下拉
下拉快速選擇組件
相信大家都有使用QQ的最近的登入的下拉框,點擊可以顯示最近登入的效果,點擊選項可以將內容提交到輸入框,實現快速輸入資料的效果。
今天我們就來實現這個效果。
首先使用先對布局實現介面
<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" android:gravity="center_horizontal" tools:context=".MainActivity" > <EditText android:gravity="center_horizontal" android:id="@+id/tv_msg" android:layout_width="250dp" android:layout_height="wrap_content" android:hint="點擊右側快速選擇" /> <ImageView android:clickable="true" android:layout_marginTop="10dp" android:layout_alignRight="@id/tv_msg" android:id="@+id/iv_down_arrow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/down_arrow" /></RelativeLayout>
使用到listview,listview的布局
<?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="match_parent" android:orientation="vertical" > <LinearLayout android:layout_marginBottom="5dp" android:gravity="center_vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageView android:id="@+id/iv_user" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/user" /> <TextView android:id="@+id/tv_msg" android:gravity="center_horizontal" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="000000000" /> <ImageView android:id="@+id/iv_delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/delete" /> </LinearLayout></LinearLayout>
<span style="font-size:18px;">package comflyou.down.select;import java.util.ArrayList;import android.app.Activity;import android.os.Bundle;import android.renderscript.Type.CubemapFace;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.EditText;import android.widget.ImageView;import android.widget.ListView;import android.widget.PopupWindow;import android.widget.TextView;public class MainActivity extends Activity { private EditText msg; private ImageView image; private PopupWindow popupWindow; private ArrayList<String> dataList; private ListView myListview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); msg = (EditText) findViewById(R.id.tv_msg); image = (ImageView) findViewById(R.id.iv_down_arrow); myListview = new ListView(MainActivity.this); popupWindow = new PopupWindow(getApplicationContext()); // 填充資料 dataList = new ArrayList<String>(); for (int i = 0; i < 20; i++) { dataList.add("flyou" + i); } // 初始化資料 init(); // 初始化lictview initListView(); } private void init() { image.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 設定PopupWindow的寬度 popupWindow.setWidth(msg.getWidth()); // 設定PopupWindow的高度 popupWindow.setHeight(300); popupWindow.setContentView(myListview); popupWindow.setOutsideTouchable(true); popupWindow.showAsDropDown(msg, 0, 0); } }); } private void initListView() { myListview.setBackgroundResource(R.drawable.listview_background); // 設定listView // 背景 myListview.setDivider(null); // 設定條目之間的分隔線為null myListview.setVerticalScrollBarEnabled(false); // 關閉 myListview.setAdapter(new MyAdapter()); } public class MyAdapter extends BaseAdapter { @Override public int getCount() { return dataList.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return 0; } @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = View.inflate(getApplicationContext(), R.layout.list_item, null); holder = new ViewHolder(); holder.iv_delete = (ImageView) convertView.findViewById(R.id.iv_delete); holder.iv_user = (ImageView) convertView.findViewById(R.id.iv_user); holder.tv_msg=(TextView)convertView.findViewById(R.id.tv_msg); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.tv_msg.setText(dataList.get(position)); holder.iv_delete.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 移除資料 dataList.remove(position); //更新listview MyAdapter.this.notifyDataSetChanged(); } }); convertView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { msg.setText(dataList.get(position)); popupWindow.dismiss(); } }); return convertView; } } //資料緩衝 private class ViewHolder { ImageView iv_delete; TextView tv_msg; ImageView iv_user; }}</span>