实现下拉选择框
直接上代码
Activity.java
package com.example.shaofei.customerviewdemo1;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class ShowDownMenuDemoActivity extends AppCompatActivity {
private EditText mEt_input;
private ImageButton mIb_show_down;
private ArrayList<String> mListData;
private PopupWindow mPopupWindow;
private boolean isShow = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_popup_window_demo);
initView();
}
private void initView() {
mEt_input = (EditText) findViewById(R.id.et_input);
mIb_show_down = (ImageButton) findViewById(R.id.ib_show_down);
mIb_show_down.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isShow) {
mPopupWindow.dismiss();
isShow = false;
} else {
showPopupWindow();
isShow = true;
}
}
});
}
/**
* 显示PopupWindow
*/
private void showPopupWindow() {
ListView listView = new ListView(this);
mListData = new ArrayList<>();
for (int i = 0; i < 30; i++) {
mListData.add("10000" + i);
}
//设置数据适配器
listView.setAdapter(new MyAdapter());
listView.setDividerHeight(0);
mPopupWindow = new PopupWindow(listView, mEt_input.getWidth(), 800);
//设置点击空白处隐藏
mPopupWindow.setOutsideTouchable(true);
//在哪个位置,x偏移多少,y偏移多少
mPopupWindow.showAsDropDown(mEt_input, 0, 0);
//让Popupwindown可以获取焦点
mPopupWindow.setFocusable(true);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
mEt_input.setText(mListData.get(i));
mPopupWindow.dismiss();
isShow = false;
}
});
}
private class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return mListData.size();
}
@Override
public Object getItem(int i) {
return mListData.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
ViewHolder holder = null;
if (view == null) {
holder = new ViewHolder();
view = View.inflate(viewGroup.getContext(), R.layout.item_popup, null);
holder.mTextView = view.findViewById(R.id.tv_center);
holder.mImageButton = view.findViewById(R.id.ib_delete);
view.setTag(holder);
}
holder = (ViewHolder) view.getTag();
holder.mTextView.setText(mListData.get(i));
holder.mImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(ShowDownMenuDemoActivity.this, "点击删除", Toast.LENGTH_SHORT).show();
mListData.remove(mListData.get(i));
notifyDataSetChanged();
if(mListData.size()==0) {
//如果删除到空集合,那么就隐藏
mPopupWindow.dismiss();
isShow = false;
}
}
});
return view;
}
}
class ViewHolder {
TextView mTextView;
ImageButton mImageButton;
}
}
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="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="300dp"
android:layout_height="60dp"
android:layout_gravity="center">
<EditText
android:id="@+id/et_input"
android:layout_width="match_parent"
android:layout_height="60dp"
android:hint="点击输入"/>
<ImageButton
android:id="@+id/ib_show_down"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:background="@null"
android:src="@mipmap/arrow_down"/>
</RelativeLayout>
</LinearLayout>
ListView中的item布局:
<?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="50dp"
android:descendantFocusability="blocksDescendants"
android:layout_margin="20dp"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@mipmap/arrow_constant"/>
<TextView
android:id="@+id/tv_center"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="10"
android:gravity="center"
android:text="1000101"
android:textColor="#a000"/>
<ImageButton
android:id="@+id/ib_delete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:src="@mipmap/arrow_delete"/>
</LinearLayout>
遇到的问题
PopupWindow中的ListView的item获取不到点击事件,原因是,item中的ImageButton抢占了item的焦点。我们可以在item的根布局中设置如下属性:
android:descendantFocusability=”blocksDescendants”
将其布局的焦点分块进行区分。就可以获取到焦点了。如果还是不能获取焦点的话,那么就在代码中再添加一行代码;
//让Popupwindown可以获取焦点
mPopupWindow.setFocusable(true);