我们传统的下拉列表是spinner,但是spinner有时候实现不了我们想要的效果,可能需要自定义,但是自定义也会出现很多问题,这个问题变的非常的繁琐,为此需要用popwindow实现这个下拉列表,下面看demo运行效果图,
这个下拉列表实现范围非常广泛,为此特意做了一个demo哈,下面看我实现的思路,了解popwindow,其实就是一个控件点击之后弹出popwindow,然后在popwindow布局中设置它的list列表,下面就是listview了。其实还是蛮简单喽。
下面看代码:首先是布局,这里没什么好说的就是相对布局里面加TextView和ImageView,其实一个TextView也可以实现,下面看布局文件activity_main
<?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:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:background="#E9E9E9" tools:context="com.jiuxin.popdemomaster.MainActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="60dp" android:layout_marginTop="10dp" android:background="#ffffff" > <LinearLayout android:layout_width="200dp" android:layout_height="50dp" android:layout_centerHorizontal="true" android:layout_centerVertical="true"> <LinearLayout android:id="@+id/xiala_layout" android:layout_width="wrap_content" android:layout_height="40dp" android:layout_gravity="center_vertical" android:layout_marginLeft="10dp" android:layout_toRightOf="@+id/first_text" android:background="@drawable/shape_juxing" android:orientation="horizontal" android:visibility="visible"> <TextView android:id="@+id/tv_left" android:layout_width="60dp" android:layout_height="30dp" android:layout_marginTop="5dp" android:gravity="center" android:paddingLeft="5dp" android:text="1" android:textSize="18sp" android:textColor="#59D4FD" /> <ImageView android:id="@+id/iv_select" android:layout_width="20dp" android:layout_height="15dp" android:layout_alignRight="@+id/editText" android:layout_gravity="center_vertical" android:layout_marginLeft="5dp" android:background="@mipmap/arrowdownicon" /> </LinearLayout> </LinearLayout> </RelativeLayout> </RelativeLayout>
然后进入mainactivity里面
package com.jiuxin.popdemomaster; import android.graphics.drawable.ColorDrawable; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.widget.AdapterView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.PopupWindow; import android.widget.RelativeLayout; import android.widget.TextView; import java.util.ArrayList; public class MainActivity extends AppCompatActivity implements View.OnClickListener { LinearLayout xialaLayout; private ArrayList<String> list = new ArrayList<>(); private AnswerQuestionAdapter myAdapter; private PopupWindow popLeft; private View layoutLeft; // 左中右三个ListView控件(弹出窗口里) private ListView menulistLeft; TextView tvLeft; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); xialaLayout = (LinearLayout) findViewById(R.id.xiala_layout); tvLeft = (TextView) findViewById(R.id.tv_left); xialaLayout.setOnClickListener(this); initData(); } private void initData() { for (int i = 0; i < 15; i++) { list.add(1 + i + ""); } } public void getpullData() { if (popLeft != null && popLeft.isShowing()) { popLeft.dismiss(); } else { layoutLeft = getLayoutInflater().inflate( R.layout.pop_menulist, null); menulistLeft = (ListView) layoutLeft .findViewById(R.id.menulist); myAdapter = new AnswerQuestionAdapter(MainActivity.this); menulistLeft.setAdapter(myAdapter); menulistLeft.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { tvLeft.setText(list.get(position)); // popupWindow.dismiss(); popLeft.dismiss(); } }); popLeft = new PopupWindow(layoutLeft, xialaLayout.getWidth(), RelativeLayout.LayoutParams.WRAP_CONTENT); ColorDrawable cd = new ColorDrawable(-0000); popLeft.setBackgroundDrawable(cd); popLeft.setAnimationStyle(R.style.PopupAnimation); popLeft.update(); popLeft.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED); popLeft.setTouchable(true); // 设置popupwindow可点击 popLeft.setOutsideTouchable(true); // 设置popupwindow外部可点击 popLeft.setFocusable(true); // 获取焦点 // 设置popupwindow的位置(相对tvLeft的位置) // int topBarHeight = rlTopBar.getBottom(); // popLeft.showAsDropDown(tvLeft, 0, // (topBarHeight - tvLeft.getHeight()) / 2); popLeft.showAsDropDown(xialaLayout, 0, 0); popLeft.setTouchInterceptor(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // 如果点击了popupwindow的外部,popupwindow也会消失 if (event.getAction() == MotionEvent.ACTION_OUTSIDE) { popLeft.dismiss(); return true; } return false; } }); } } @Override public void onClick(View view) { switch (view.getId()) { case R.id.xiala_layout: getpullData(); break; } } }
点击上面的控件显示popwindow,在popwindow弹出的时候,给popwindow设置布局
pop_menulist <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/shape_juxing" android:orientation="vertical" > <ListView android:id="@+id/menulist" android:layout_width="fill_parent" android:layout_height="wrap_content" android:divider="#ffffff" android:dividerHeight="8.0dip" android:padding="3dip" android:scrollbars="none" > </ListView> </LinearLayout>这就是popmenulist的布局,将listview加入里面。
最后就是写适配器adapter了。adapter就不用说了。
下面发一下demo地址:https://github.com/yuanchongzhang/PopDemoMaster-master