Anddroid 下拉列表

适配器类,扩展baseadapter:

package com.youche.cango.activity.loansign.spinnerpopwindow;

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 com.youche.cango.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 下拉列表的适配器
 * @param 
 */
public abstract class AbstractSpinerAdapter extends BaseAdapter {


	/**
	 * Item点击接口
	 */
	public  interface IOnItemSelectListener{
		 void onItemClick(int pos);
	};
	
	 private Context mContext;   
	 private List mObjects = new ArrayList<>();
	 private int mSelectItem = 0;
	 private LayoutInflater mInflater;
	
	 public  AbstractSpinerAdapter(Context context){
		 init(context);
	 }
	 
	 public void refreshData(List objects, int selIndex){
		 mObjects = objects;
		 if (selIndex < 0){
			 selIndex = 0;
		 }
		 if (selIndex >= mObjects.size()){
			 selIndex = mObjects.size() - 1;
		 }
		 
		 mSelectItem = selIndex;
	 }
	 
	 private void init(Context context) {
	        mContext = context;
	        mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	 }
	    
	    
	@Override
	public int getCount() {

		return mObjects.size();
	}

	@Override
	public Object getItem(int pos) {
		return mObjects.get(pos).toString();
	}

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

	@Override
	public View getView(int pos, View convertView, ViewGroup arg2) {
		 ViewHolder viewHolder;
	     if (convertView == null) {
	    	 convertView = mInflater.inflate(R.layout.spiner_item_layout, null);
	         viewHolder = new ViewHolder();
	         viewHolder.mTextView = (TextView) convertView.findViewById(R.id.textView);
	         convertView.setTag(viewHolder);
	     } else {
	         viewHolder = (ViewHolder) convertView.getTag();
	     }

	     Object item =  getItem(pos);
		 viewHolder.mTextView.setText(item.toString());

	     return convertView;
	}

	/**
	 * 复用Holder
	 */
	public static class ViewHolder
	{
	    public TextView mTextView;
    }


}


2.扩展PopWinodw

package com.youche.cango.activity.loansign.spinnerpopwindow;

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 com.youche.cango.R;

import java.util.List;

/**
 * 申请人列拉列表是使用popwindow的方式传进去一个ListView的
 */
public class SpinerPopWindow extends PopupWindow implements OnItemClickListener {

    private Context mContext;
    private ListView mListView;
    private AbstractSpinerAdapter mAdapter;
    private AbstractSpinerAdapter.IOnItemSelectListener mItemSelectListener;


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


    public void setItemListener(AbstractSpinerAdapter.IOnItemSelectListener listener) {
        mItemSelectListener = listener;
    }

    public void setAdatper(AbstractSpinerAdapter adapter) {
        mAdapter = adapter;
        mListView.setAdapter(mAdapter);
    }


    private void init() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.spiner_window_layout, 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.listview);
        mListView.setOnItemClickListener(this);
    }

    /**
     * 刷新数据接口,是替换式更新
     * @param list
     * @param selIndex
     * @param 
     */
    public  void refreshData(List list, int selIndex) {
        if (list != null && selIndex != -1) {
            if (mAdapter != null) {
                mAdapter.refreshData(list, selIndex);
            }
        }
    }


    @Override
    public void onItemClick(AdapterView arg0, View view, int pos, long arg3) {
        dismiss();
        if (mItemSelectListener != null) {
            mItemSelectListener.onItemClick(pos);
        }
    }


}

3.popwindow布局文件:




    

    



4.下拉选项布局文件:




    
    



5.使用方式:初始化可放在onCreate中
private void initSpinerListData(){
        String[] names = {"主申请人","配偶"};
        for(int i = 0; i < names.length; i++){
            nameList.add(names[i]);
        }

        mAdapter = new ApplierAdapter(this);
        mAdapter.refreshData(nameList, 0);

        mSpinerPopWindow = new SpinerPopWindow(this);
        mSpinerPopWindow.setAdatper(mAdapter);
        mSpinerPopWindow.setItemListener(new ItemClickListener());
    }

6.接口回调:

/**
     * 下拉列表选择框
     */
    private class ItemClickListener implements AbstractSpinerAdapter.IOnItemSelectListener{

        @Override
        public void onItemClick(int pos) {
            showSelected(pos);

        }
    }

    /**
     * 显示选中的列表项
     * @param pos
     */
    private void showSelected(int pos){
        if (pos >= 0 && pos <= nameList.size()){
            mMainApplier.setText(nameList.get(pos));
        }

    }




你可能感兴趣的:(Anddroid 下拉列表)