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

导读:本篇文章讲解 97.android 简单的下拉选择框实现(ListPopupWindow),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

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

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

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

//第一步 Activity布局: 

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.gang.app.myceshi.Main2Activity">

    <Button
        android:background="@drawable/spinner"
        android:layout_gravity="center"
        android:id="@+id/mButton"
        android:text="数据一"
        android:layout_width="180dp"
        android:layout_height="30dp" />

</LinearLayout>

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

 

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <shape>
            <!--边框线宽度  边框线颜色-->
            <stroke
                android:width="1dp"
                android:color="#D3D3D3" />

            <!--圆角度数-->
            <corners android:radius="3dp" />

            <!--背景颜色-->
            <solid android:color="#ffffff" />

            <!--边距-->
            <padding android:right="5dp" />


        </shape>


    </item>
    <!--//第二组item 插入图片(替换默认箭头)-->
    <item>
        <bitmap
            android:gravity="end"
            android:src="@drawable/triangle" />
    </item>
</layer-list>

//箭头图片 triangle.png:

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

 

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

public class Main2Activity extends AppCompatActivity implements View.OnClickListener {

    private Button mButton;
    private ListPopupWindow listPopupWindow;
    private List<String> mList = new ArrayList<>();
    ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initView();
        selectBox();//添加数据,初始化ListPopupWindow
        listPopupWindowListener();//ListPopupWindow监听
    }

    private void initView() {
        mButton = (Button) findViewById(R.id.mButton);

        mButton.setOnClickListener(this);
    }

    /*
     * 方法名:selectBox()
     * 功    能:添加数据,初始化ListPopupWindow
     * 参    数:无
     * 返回值:无
     */
    private void selectBox() {

        mList.add("数据一");
        mList.add("数据二");
        mList.add("数据三");

        //初始化ListPopupWindow,适配
        listPopupWindow = new ListPopupWindow(this);
        //系统布局
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mList);
        //自定义适配器,布局
//        MyApader adapter = new MyApader(this,mList);
        listPopupWindow.setAdapter(adapter);
        listPopupWindow.setAnchorView(mButton);//设置ListPopupWindow的锚点,关联mButton位置
        listPopupWindow.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
        listPopupWindow.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        listPopupWindow.setModal(true);

    }

    /*
     * 方法名:listPopupWindowListener()
     * 功    能:ListPopupWindow监听
     * 参    数:无
     * 返回值:无
     */
    private void listPopupWindowListener() {
        listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mButton.setText(mList.get(position));

                mList.add(0, mList.remove(position)); //list集合中的某个值放到第一的位置

//                Collections.swap(mList,0,position);//互换位置

                listPopupWindow.dismiss(); //关闭listPopupWindow
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.mButton:
                listPopupWindow.show();//显示listPopupWindow
                break;
        }
    }
}

 

 

 

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

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

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

第一步 我的Activity代码实现:

 

public class Main2Activity extends AppCompatActivity implements View.OnClickListener {

    private Button mButton;
    private ListPopupWindow listPopupWindow;
    private List<String> mList = new ArrayList<>();
    ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initView();
        selectBox();//添加数据,初始化ListPopupWindow
        listPopupWindowListener();//ListPopupWindow监听
    }

    private void initView() {
        mButton = (Button) findViewById(R.id.mButton);

        mButton.setOnClickListener(this);
    }

    /*
     * 方法名:selectBox()
     * 功    能:添加数据,初始化ListPopupWindow
     * 参    数:无
     * 返回值:无
     */
    private void selectBox() {

        mList.add("数据一");
        mList.add("数据二");
        mList.add("数据三");

        //初始化ListPopupWindow,适配
        listPopupWindow = new ListPopupWindow(this);
        //系统布局
//        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mList);
        //自定义适配器,布局
        MyApader adapter = new MyApader(this,mList);
        listPopupWindow.setAdapter(adapter);
        listPopupWindow.setAnchorView(mButton);//设置ListPopupWindow的锚点,关联mButton位置
        listPopupWindow.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
        listPopupWindow.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        listPopupWindow.setModal(true);

    }

    /*
     * 方法名:listPopupWindowListener()
     * 功    能:ListPopupWindow监听
     * 参    数:无
     * 返回值:无
     */
    private void listPopupWindowListener() {
        listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mButton.setText(mList.get(position));

                mList.add(0, mList.remove(position)); //list集合中的某个值放到第一的位置

//                Collections.swap(mList,0,position);//互换位置

                listPopupWindow.dismiss(); //关闭listPopupWindow
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.mButton:
                listPopupWindow.show();//显示listPopupWindow
                break;
        }
    }
}

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

 

class MyApader extends BaseAdapter{
    private Context context;
    private List<String>mList;

    public MyApader(Context context, List<String> mList) {
        this.context = context;
        this.mList = mList;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (null == convertView) {
            convertView = View.inflate(context, R.layout.litem_layout, null);
        }

        TextView textView = (TextView) convertView.findViewById(R.id.mText);
        textView.setText(mList.get(position));
//        if (position==0){
//            textView.setBackgroundColor(Color.BLUE);
//        }
        return convertView;
    }
}

//第三步 适配器Item布局  litem_layout.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"
    >

    <TextView
        android:id="@+id/mText"
        android:textStyle="bold"
        android:padding="5dp"
        android:textSize="15sp"
        android:textColor="@color/colorAccent"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <View
        android:background="#bebebe"
        android:layout_width="match_parent"
        android:layout_height="1dp"/>

</LinearLayout>

//————————————————————————-完———————————————————————————–

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/118297.html

(0)
seven_的头像seven_bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!