android自定义picker组件,动画从底部弹出文字选择器

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

更新了一下,弹出的布局改为PopupWindow,动态弹出picker。

想要实现底部出现的文字选择器,andorid没有现成的组件,自己封装了一个。下面上代码: 自定义组件picker:

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.eccl.envimonitor.R;

/**
 * Created by zhangda on 2015/9/4.
 * 自定义组件:数据选择器
 */
public class Picker{
    private Button doneBtn;
    private Button cancelBtn;
    private ListView picker_listview;
    private String[] mDatas = {};
    private Context mContext;
    private int selectedItemPosition = 0;
    private OnSelectDoneListener mListner;
    private Picker me;
    private LinearLayout picker;
    private PopupWindowFromBottom menuWindow;
    private View mParentView;

    /***
     * 构造方法
     * @param context
     * @param parentView 父view
     */
    public Picker(Context context, View parentView, String[] datas) {
        me = this;
        mContext = context;
        mParentView = parentView;
        picker = (LinearLayout)LayoutInflater.from(context).inflate(R.layout.picker_content, null);
        doneBtn = (Button)picker.findViewById(R.id.picker_done);
        cancelBtn = (Button)picker.findViewById(R.id.picker_cancel);
        picker_listview = (ListView)picker.findViewById(R.id.picker_listview);
        menuWindow = new PopupWindowFromBottom(context, picker);
        bindBtnsEvent();
        refreshData(datas);
    }

    private void bindBtnsEvent() {
        doneBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mListner.onSelectDone(selectedItemPosition);
                menuWindow.dismiss();
            }
        });
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                menuWindow.dismiss();
            }
        });
    }

    /***
     * 供外部调用
     * @param listener
     */
    public void setOnSelectDoneListener(OnSelectDoneListener listener){
        mListner = listener;
    }

    private void refreshData(String[] datas){
        mDatas = datas;
        final String[] dataForuse = mDatas;
        BaseAdapter pickerAdapter = new BaseAdapter() {
            @Override
            public int getCount() {
                return dataForuse.length;
            }

            @Override
            public Object getItem(int i) {
                return null;
            }

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

            @Override
            public View getView(int i, View convertView, ViewGroup viewGroup) {
                viewHolder viewHoder = null;
                if(convertView == null){
                    viewHoder = new viewHolder();
                    convertView = LayoutInflater.from(mContext).inflate(R.layout.item_picker_listview, null);
                    viewHoder.textView = (TextView)convertView.findViewById(R.id.company_name);
                    convertView.setTag(viewHoder);
                }else{
                    viewHoder = (viewHolder)convertView.getTag();
                }
                viewHoder.textView.setText(dataForuse[i]);
                viewHoder.textView.setTag(dataForuse[i]);
                if(i == selectedItemPosition){
                    viewHoder.textView.setTextColor(mContext.getResources().getColor(R.color.toolbarbg));
                }else{
                    viewHoder.textView.setTextColor(mContext.getResources().getColor(R.color.black));
                }
                return convertView;
            }
        };

        picker_listview.setAdapter(pickerAdapter);
        picker_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView adapterView, View view, int i, long l) {
                selectedItemPosition = i;
            }
        });
    }

    private class viewHolder{
        private TextView textView;
    }

    public void show(){
        if(!menuWindow.isShowing()){
            refreshData(mDatas);
            menuWindow.showAtLocation(mParentView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); //设置layout在PopupWindow中显示的位置
        }
//        if (menuWindow.isShowing()) {
//            menuWindow.dismiss();
//        } else {
//            selectedItemPosition = 0;
//            refreshData(mDatas);
//            menuWindow.showAtLocation(mParentView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); //设置layout在PopupWindow中显示的位置
//        }
    }

    /***
     *
     * 选择完毕调用的监听器
     * @author zhangda
     */
    public interface OnSelectDoneListener{
        void onSelectDone(int i);
    }
}

picker_content.xml文件



    

        

item_picker_listview.xml




    

自定义的popupWindow,PopupWindowFromBottom类:

import android.app.Activity;
import android.content.Context;  
import android.graphics.drawable.ColorDrawable;  
import android.view.LayoutInflater;  
import android.view.MotionEvent;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.view.View.OnTouchListener;  
import android.view.ViewGroup.LayoutParams;  
import android.widget.Button;  
import android.widget.PopupWindow;

import com.eccl.envimonitor.R;

public class PopupWindowFromBottom extends PopupWindow {
  
  
    public PopupWindowFromBottom(Context context, final View view) {
        super(context);  
        LayoutInflater inflater = (LayoutInflater) context  
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  

        //设置按钮监听
        //设置SelectPicPopupWindow的View
        this.setContentView(view);
        //设置SelectPicPopupWindow弹出窗体的宽  
        this.setWidth(LayoutParams.FILL_PARENT);  
        //设置SelectPicPopupWindow弹出窗体的高  
        this.setHeight(LayoutParams.WRAP_CONTENT);  
        //设置SelectPicPopupWindow弹出窗体可点击  
        this.setFocusable(true);  
        //设置SelectPicPopupWindow弹出窗体动画效果  
        this.setAnimationStyle(R.style.PopupAnimation);
        //实例化一个ColorDrawable颜色为半透明  
        ColorDrawable dw = new ColorDrawable(0xb0000000);  
        //设置SelectPicPopupWindow弹出窗体的背景  
        this.setBackgroundDrawable(dw);  
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框  
        view.setOnTouchListener(new OnTouchListener() {
              
            public boolean onTouch(View v, MotionEvent event) {  
                  
                int height = view.findViewById(R.id.picker_cancel).getTop();
                int y=(int) event.getY();  
                if(event.getAction()==MotionEvent.ACTION_UP){  
                    if(y

push_bottom_in.xml








push_bottom_out.xml





    
    
   

style.xml


以上是自定义的部分,那么如何使用呢?也很简单

在需要使用picker的activity里面:

Picker picker_wuran_Picker;
search_wuran_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                picker_wuran_Picker.show();
            }
        });

        final String []wuran_headers = {"企业名称", "排口", "SO2(mg/Nm3)", "NOX(mg/Nm3)", "烟尘(mg/Nm3)", "烟气流量(Nm3/h)"};
        picker_wuran_Picker = new Picker(WuRanActivity.this, feiqi_wuran_listView, wuran_headers);
        picker_wuran_Picker.setOnSelectDoneListener(new Picker.OnSelectDoneListener() {
            @Override
            public void onSelectDone(int i) {
                Toast.makeText(WuRanActivity.this, wuran_headers[i], Toast.LENGTH_SHORT).show();
            }
        });

有图有真相: android自定义picker组件,动画从底部弹出文字选择器_第1张图片

转载于:https://my.oschina.net/u/1011854/blog/501193

你可能感兴趣的:(android自定义picker组件,动画从底部弹出文字选择器)