使用PopupWindow实现Spinner的下拉列表

系统自带的Spinner实在不好用,所以我们用PopupWindow来实现同样的效果

1、布局

    "match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:padding="20dp">

        "0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/time_interval"
            android:textColor="@color/colorBlackText"
            android:textSize="18sp" />

        "@+id/choose_time"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:background="@drawable/shape_task_down_et"
            android:drawableEnd="@mipmap/arrow_down"
            android:text="@string/time_interval"
            android:textColor="@color/colorBlackText"
            android:textSize="18sp" />

    </LinearLayout>

这里写图片描述


2、弹出实现

点击choose_time时弹出popupwindow

private void showPopupWindow() {
        tvSetImg(chooseTime , R.mipmap.arrow_top);
        View view = LayoutInflater.from(TaskDownActivity.this).inflate(R.layout.choose_pop, null);
        final PopupWindow popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setTouchable(true);
        //一定要在代码中setBackgroundDrawable,不然点击外面popupWindow是不会dismiss的
        popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_popup_view));
        popupWindow.showAsDropDown(chooseTime);
        popupWindow.setOnDismissListener(new PopupDismissListener());
        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.rv_choose_pop);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        ChooseTimeAdapter adapter = new ChooseTimeAdapter(TaskDownActivity.this, new ChooseTimeAdapter.MyItemClickListener() {
            @Override
            public void onClick(View view) {
                int tag = (int) view.getTag();
                chooseTime.setText(mData.get(tag));
                popupWindow.dismiss();
            }
        }, mData);
        recyclerView.setAdapter(adapter);
    }

    /**
     * 弹窗消失的时候让箭头换回来
     */
    class PopupDismissListener implements PopupWindow.OnDismissListener {
        @Override
        public void onDismiss() {
            tvSetImg(chooseTime , R.mipmap.arrow_down);
        }

    }

    /**
     * 设置textView右侧的图像
     * @param textView
     * @param img
     */
    private void tvSetImg(TextView textView ,int img) {
        Drawable nav_up = getResources().getDrawable(img);
        nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
        textView.setCompoundDrawables(null, null, nav_up, null);
    }

使用PopupWindow实现Spinner的下拉列表_第1张图片


3、辅助代码

choose_pop.xml


<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rv_choose_pop"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorWhite"/>

shape_popup_view.xml


<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/colorWhite" />
    <corners android:radius="2dp" />
    <padding android:bottom="5dp" android:left="10dp" android:right="10dp" android:top="5dp"/>
    <stroke android:color="@color/colorLineGray" android:width="1dp"/>
shape>

ChooseTimeAdapter.java

public class ChooseTimeAdapter extends RecyclerView.Adapter {
    private Context context;
    private MyItemClickListener itemClickListener;
    private ArrayList list;
    private String id, name, patientId, cardNo;

    public interface MyItemClickListener{
        void onClick(View view);
    }

    public ChooseTimeAdapter(Context context, MyItemClickListener itemClickListener, ArrayList list) {
        this.context = context;
        this.itemClickListener = itemClickListener;
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_choose_time, null);
        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                itemClickListener.onClick(view);
            }
        });
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemViewHolder) {
            ((ItemViewHolder) holder).itemView.setTag(position);
            ((ItemViewHolder) holder).textView.setText(list.get(position));
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {
        TextView textView;

        public ItemViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.choose_item);
        }
    }
}

item_choose_time.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:id="@+id/choose_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textColor="@color/colorBlackText"
        android:textSize="16sp" />

    <include layout="@layout/full_line" />

LinearLayout>

full_line.xml


<View xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@color/colorLineGray"
    android:layout_width="match_parent"
    android:layout_height="0.5dp" />

4、整理成工具代码

这里写图片描述

整理成工具代码,方便复用

代码中调用new SpinnerUtils(TaskDownActivity.this, chooseTime, mData).showPopupWindow();

SpinnerUtils.java

package com.cnbs.cableinspection.utils.spinner;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.cnbs.cableinspection.R;
import com.cnbs.cableinspection.utils.DividerItemDecoration;

import java.util.ArrayList;

/**
 * ArrayList ,其中的string可以换成一个通用的实体,比如一个(int)id ,一个(string) value
 * author: zuo
 * date: 2017/11/29 15:05
 */

public class SpinnerUtils {
    private Context mContext;
    private TextView mTextView;
    private ArrayList mData;

    public SpinnerUtils(Context context , TextView textView , ArrayList data) {
        this.mContext = context;
        this.mTextView = textView;
        this.mData = data;
    }

    public void showPopupWindow() {
        tvSetImg(mTextView, R.mipmap.arrow_top);
        View view = LayoutInflater.from(mContext).inflate(R.layout.choose_pop, null);
        final PopupWindow popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setTouchable(true);
        popupWindow.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.shape_popup_view));
        popupWindow.showAsDropDown(mTextView);
        popupWindow.setOnDismissListener(new PopupDismissListener());
        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.rv_choose_pop);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        SpinnerChooseAdapter adapter = new SpinnerChooseAdapter(mContext, new SpinnerChooseAdapter.MyItemClickListener() {
            @Override
            public void onClick(View view) {
                int tag = (int) view.getTag();
                mTextView.setText(mData.get(tag));
                popupWindow.dismiss();
            }
        }, mData);
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL_LIST));
    }

    /**
     * 弹窗消失的时候让箭头换回来
     */
    class PopupDismissListener implements PopupWindow.OnDismissListener {
        @Override
        public void onDismiss() {
            tvSetImg(mTextView, R.mipmap.arrow_down);
        }

    }

    /**
     * 设置textView右侧的图像
     *
     * @param textView
     * @param img
     */
    private void tvSetImg(TextView textView, int img) {
        Drawable nav_up = mContext.getResources().getDrawable(img);
        nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
        textView.setCompoundDrawables(null, null, nav_up, null);
    }
}

你可能感兴趣的:(android,布局控件)