当前位置:   article > 正文

97.android 简单的下拉选择框实现(ListPopupWindow)_android 下拉框框架

android 下拉框框架

//使用系统自带布局的ListPopupWindow: 

//第一步 Activity布局: 

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. tools:context="com.gang.app.myceshi.Main2Activity">
  9. <Button
  10. android:background="@drawable/spinner"
  11. android:layout_gravity="center"
  12. android:id="@+id/mButton"
  13. android:text="数据一"
  14. android:layout_width="180dp"
  15. android:layout_height="30dp" />
  16. </LinearLayout>

//第二步 选择框背景,在drawable下新建select_box_bg.xml文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
  3. <item>
  4. <shape>
  5. <!--边框线宽度 边框线颜色-->
  6. <stroke
  7. android:width="1dp"
  8. android:color="#D3D3D3" />
  9. <!--圆角度数-->
  10. <corners android:radius="3dp" />
  11. <!--背景颜色-->
  12. <solid android:color="#ffffff" />
  13. <!--边距-->
  14. <padding android:right="5dp" />
  15. </shape>
  16. </item>
  17. <!--//第二组item 插入图片(替换默认箭头)-->
  18. <item>
  19. <bitmap
  20. android:gravity="end"
  21. android:src="@drawable/triangle" />
  22. </item>
  23. </layer-list>

//箭头图片 triangle.png:

//第三步 我的Activity代码实现: 

  1. public class Main2Activity extends AppCompatActivity implements View.OnClickListener {
  2. private Button mButton;
  3. private ListPopupWindow listPopupWindow;
  4. private List<String> mList = new ArrayList<>();
  5. ;
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_main2);
  10. initView();
  11. selectBox();//添加数据,初始化ListPopupWindow
  12. listPopupWindowListener();//ListPopupWindow监听
  13. }
  14. private void initView() {
  15. mButton = (Button) findViewById(R.id.mButton);
  16. mButton.setOnClickListener(this);
  17. }
  18. /*
  19. * 方法名:selectBox()
  20. * 功 能:添加数据,初始化ListPopupWindow
  21. * 参 数:无
  22. * 返回值:无
  23. */
  24. private void selectBox() {
  25. mList.add("数据一");
  26. mList.add("数据二");
  27. mList.add("数据三");
  28. //初始化ListPopupWindow,适配
  29. listPopupWindow = new ListPopupWindow(this);
  30. //系统布局
  31. ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mList);
  32. //自定义适配器,布局
  33. // MyApader adapter = new MyApader(this,mList);
  34. listPopupWindow.setAdapter(adapter);
  35. listPopupWindow.setAnchorView(mButton);//设置ListPopupWindow的锚点,关联mButton位置
  36. listPopupWindow.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
  37. listPopupWindow.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
  38. listPopupWindow.setModal(true);
  39. }
  40. /*
  41. * 方法名:listPopupWindowListener()
  42. * 功 能:ListPopupWindow监听
  43. * 参 数:无
  44. * 返回值:无
  45. */
  46. private void listPopupWindowListener() {
  47. listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  48. @Override
  49. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  50. mButton.setText(mList.get(position));
  51. mList.add(0, mList.remove(position)); //list集合中的某个值放到第一的位置
  52. // Collections.swap(mList,0,position);//互换位置
  53. listPopupWindow.dismiss(); //关闭listPopupWindow
  54. }
  55. });
  56. }
  57. @Override
  58. public void onClick(View v) {
  59. switch (v.getId()) {
  60. case R.id.mButton:
  61. listPopupWindow.show();//显示listPopupWindow
  62. break;
  63. }
  64. }
  65. }

//以下使用自定义布局 ListPopupWindow:

//只有适配器变了,使用自定义适配器,自定义布局,

第一步 我的Activity代码实现:

  1. public class Main2Activity extends AppCompatActivity implements View.OnClickListener {
  2. private Button mButton;
  3. private ListPopupWindow listPopupWindow;
  4. private List<String> mList = new ArrayList<>();
  5. ;
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_main2);
  10. initView();
  11. selectBox();//添加数据,初始化ListPopupWindow
  12. listPopupWindowListener();//ListPopupWindow监听
  13. }
  14. private void initView() {
  15. mButton = (Button) findViewById(R.id.mButton);
  16. mButton.setOnClickListener(this);
  17. }
  18. /*
  19. * 方法名:selectBox()
  20. * 功 能:添加数据,初始化ListPopupWindow
  21. * 参 数:无
  22. * 返回值:无
  23. */
  24. private void selectBox() {
  25. mList.add("数据一");
  26. mList.add("数据二");
  27. mList.add("数据三");
  28. //初始化ListPopupWindow,适配
  29. listPopupWindow = new ListPopupWindow(this);
  30. //系统布局
  31. // ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mList);
  32. //自定义适配器,布局
  33. MyApader adapter = new MyApader(this,mList);
  34. listPopupWindow.setAdapter(adapter);
  35. listPopupWindow.setAnchorView(mButton);//设置ListPopupWindow的锚点,关联mButton位置
  36. listPopupWindow.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
  37. listPopupWindow.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
  38. listPopupWindow.setModal(true);
  39. }
  40. /*
  41. * 方法名:listPopupWindowListener()
  42. * 功 能:ListPopupWindow监听
  43. * 参 数:无
  44. * 返回值:无
  45. */
  46. private void listPopupWindowListener() {
  47. listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  48. @Override
  49. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  50. mButton.setText(mList.get(position));
  51. mList.add(0, mList.remove(position)); //list集合中的某个值放到第一的位置
  52. // Collections.swap(mList,0,position);//互换位置
  53. listPopupWindow.dismiss(); //关闭listPopupWindow
  54. }
  55. });
  56. }
  57. @Override
  58. public void onClick(View v) {
  59. switch (v.getId()) {
  60. case R.id.mButton:
  61. listPopupWindow.show();//显示listPopupWindow
  62. break;
  63. }
  64. }
  65. }

//第二步 自定义适配器 MyApader :

  1. class MyApader extends BaseAdapter{
  2. private Context context;
  3. private List<String>mList;
  4. public MyApader(Context context, List<String> mList) {
  5. this.context = context;
  6. this.mList = mList;
  7. }
  8. @Override
  9. public int getCount() {
  10. return mList.size();
  11. }
  12. @Override
  13. public Object getItem(int position) {
  14. return mList.get(position);
  15. }
  16. @Override
  17. public long getItemId(int position) {
  18. return position;
  19. }
  20. @Override
  21. public View getView(int position, View convertView, ViewGroup parent) {
  22. if (null == convertView) {
  23. convertView = View.inflate(context, R.layout.litem_layout, null);
  24. }
  25. TextView textView = (TextView) convertView.findViewById(R.id.mText);
  26. textView.setText(mList.get(position));
  27. // if (position==0){
  28. // textView.setBackgroundColor(Color.BLUE);
  29. // }
  30. return convertView;
  31. }
  32. }

//第三步 适配器Item布局  litem_layout.xml :

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. >
  7. <TextView
  8. android:id="@+id/mText"
  9. android:textStyle="bold"
  10. android:padding="5dp"
  11. android:textSize="15sp"
  12. android:textColor="@color/colorAccent"
  13. android:gravity="center"
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content" />
  16. <View
  17. android:background="#bebebe"
  18. android:layout_width="match_parent"
  19. android:layout_height="1dp"/>
  20. </LinearLayout>

//解决使用ListPopupWindow时发现宽度不适应,显示不全问题,使用View.inflate获取到spinner_item.xml中的TextView对象,赋值后获取宽度,比较出最大值maxWidth即可
,调用ListPopupWindow的setWidth(maxWidth),注意:TextView赋值后,获取宽度前,要调用measure方法,否则获取宽度为0.

  1. popup = new ListPopupWindow(getContext());
  2.             int maxWidth = 0;
  3.             for (String car : cars) {
  4.                 TextView textView = (TextView) View.inflate(getContext(), R.layout.spinner_item, null);
  5.                 textView.setText(car);
  6.                 textView.measure(0, 0);
  7.                 maxWidth = maxWidth > textView.getMeasuredWidth() ? maxWidth : textView.getMeasuredWidth();
  8.             }
  9.             popup.setAdapter(adapter);
  10.             popup.setWidth(maxWidth);


           
 

 

//-------------------------------------------------------------------------完-----------------------------------------------------------------------------------

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/111924
推荐阅读
相关标签