自定义popwindow的spinner下拉列表

自定义popwindow:

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.PopupWindow;

import cn.firstleap.parent.R;
import cn.firstleap.parent.listener.IFLItemOnClickListener;

public class SpinerPopWindow extends PopupWindow {

    private Context mContext;
    private ListView mListView;
    private IFLItemOnClickListener mItemSelectListener;

    public ListView getListView() {
        return mListView;
    }

    public SpinerPopWindow(Context context) {
        super(context);
        mContext = context;
        init();
    }


    public void setItemListener(IFLItemOnClickListener listener) {
        mItemSelectListener = listener;
    }


    private void init() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.popupwindow_spinner, null);
        setContentView(view);
        setWidth(LayoutParams.WRAP_CONTENT);
        setHeight(LayoutParams.WRAP_CONTENT);

        setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0x00);
        setBackgroundDrawable(dw);


        mListView = (ListView) view.findViewById(R.id.mListView);
        mListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView adapterView, View view, int position, long l) {
                dismiss();
                if (mItemSelectListener != null) {
                    mItemSelectListener.onClickListener(position);
                }
            }
        });
    }
}
布局:popupwindow_spinner


    
    
样式:shape_roundpopwindowspiner


    
    
适配器: SpinnerClassAdapter
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import cn.firstleap.parent.R;
import cn.firstleap.parent.beanteacher.OrganizationlBean;

/**
 * Author:George on 2016/6/21 0021 16:00
 * Email:[email protected]
 */
public class SpinnerClassAdapter extends BaseAdapter {
    private List mData;
    private LayoutInflater mInflater;

    public SpinnerClassAdapter(Context context, List mData) {
        mInflater = LayoutInflater.from(context);
        this.mData = mData;
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_spinnerclass, null);
            holder = new ViewHolder();
            holder.textClass = (TextView) convertView.findViewById(R.id.textClass);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.textClass.setText(mData.get(position).getName());
        return convertView;
    }

    static class ViewHolder {
        TextView textClass;
    }
}
item布局:item_spinnerclass



    

实体类: OrganizationlBean就省略了,换成需要的实体类就行了
 
  
每个item点击回调接口:IFLItemOnClickListener 
public interface IFLItemOnClickListener {
    void onClickListener(int position);
}
在activity 中使用:
activity中布局:


            

            
        

 
  
声明:
private SpinnerClassAdapter mSpinnerClassAdapter;
    private List spinnerData = new ArrayList<>();
    private SpinerPopWindow mSpinerPopWindow;
初始化:
mSpinerPopWindow = new SpinerPopWindow(this);
        mSpinerPopWindow.getListView().setAdapter(mSpinnerClassAdapter);
mSpinerPopWindow.setItemListener(new IFLItemOnClickListener() {
            @Override
            public void onClickListener(int position) {
                loadStudent(spinnerData.get(position).getDep_id());
                textTitle.setText(spinnerData.get(position).getName());
            }
        });
展示下拉事件:
 case R.id.pullView://下拉
                LogUtils.i(TAG, "显示下拉popWindow");
                mSpinerPopWindow.setWidth(pullView.getWidth());
                mSpinerPopWindow.showAsDropDown(pullView);
                break;
加载本地数据或者异步加载网络数据:记得notify, textTitle显示选中的标题显示出来,这里是默认显示第一个数据
 spinnerData.addAll(JsonUtils.parseDataToList(data.toString(), OrganizationlBean.class));
                    mSpinnerClassAdapter.notifyDataSetChanged();
                    textTitle.setText(spinnerData.get(0).getName());


 
  

 
  

 
  

 
  

 
  
 
  



你可能感兴趣的:(android,listview,popupWindow,spinner)