Android自定义下拉框

时间:2021-03-19 23:50:07

Android自定义下拉框

      Android 原生的Spinner下拉框无法满足项目的需要,所以自已用PopupWindow写了一自定下拉框,效果如下:

Android自定义下拉框


java代码:CommonSpinner

package guojunustb.commonspinner;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;


import java.util.List;

/**
* Created by guojun on 2015/10/29.
*/
public class CommonSpinner extends TextView implements View.OnClickListener {
private TextView mTvTopView;
private Context mContext;
private SpinnerPopupWindow mPopupWindow;
private List<String> mDatas;
private OnSpinnerItemSelectListener mOnSpinnerItemSelectListener;

public CommonSpinner(Context context) {
this(context, null);
}

public CommonSpinner(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

public CommonSpinner(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mTvTopView = this;
mContext = context;
mPopupWindow = new SpinnerPopupWindow(mContext);
setOnClickListener(this);
}

@Override
public void onClick(View v) {
mPopupWindow.showPopupWindow(mTvTopView);
}

public void setData(List<String> datas) {
this.mDatas = datas;
}

class SpinnerPopupWindow extends PopupWindow {
private Context mContext;
private ListView mListView;
private MyAdapter mMyAdapter;

public SpinnerPopupWindow(Context context) {
init(context);
}

private void init(Context context) {
mContext = context;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.layout_spinner, null);
mListView = (ListView) view.findViewById(R.id.listview);
mListView.setItemsCanFocus(true);
mMyAdapter = new MyAdapter();
mListView.setAdapter(mMyAdapter);
this.setContentView(view);
//设置宽高
this.setWidth(mListView.getLayoutParams().width);
this.setHeight(mListView.getLayoutParams().height);
//让popupwindow获取焦点
this.setFocusable(true);
this.setOutsideTouchable(true);
this.update();

// 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框
ColorDrawable dw = new ColorDrawable(00000000);
this.setBackgroundDrawable(dw);
//设置动画
this.setAnimationStyle(R.style.SpinnerPopupWindowAnim);

}

class MyAdapter extends BaseAdapter {

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

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

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

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = View.inflate(mContext, R.layout.item_spinner, null);
}
Button bt = ViewHolder.get(convertView,R.id.bt_item);
bt.setText(mDatas.get(position));
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showPopupWindow(mTvTopView);
mTvTopView.setText(mDatas.get(position));
if (null != mOnSpinnerItemSelectListener) {
mOnSpinnerItemSelectListener.onItemSelectListener(position);
}
}
});
return convertView;
}
}

public void showPopupWindow(View parent) {
if (!this.isShowing()) {
this.showAsDropDown(parent, -(this.getWidth() -parent.getWidth())/2, 0);
} else {
this.dismiss();
}
}

}

public void setOnSpinnerItemSelectListener(OnSpinnerItemSelectListener listener) {
this.mOnSpinnerItemSelectListener = listener;
}


public interface OnSpinnerItemSelectListener {
void onItemSelectListener(int pos);
}

}
布局文件:layout_spinner.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="111dp"
android:layout_height="wrap_content"
android:orientation="vertical">

<ListView
android:id="@+id/listview"
android:layout_width="111dp"
android:layout_height="190dp"
android:paddingTop="10dp"
android:descendantFocusability="blocksDescendants"
android:background="@mipmap/class_tab_putup"
android:divider="@null"
android:scrollbars="none" />

</LinearLayout>

Activity文件

package guojunustb.commonspinner;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

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

public class MainActivity extends Activity {
private CommonSpinner mCommonSpinner;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
assignViews();
}

private void assignViews() {
mCommonSpinner = (CommonSpinner) findViewById(R.id.common_spinner);
mCommonSpinner.setData(getDatas());
mCommonSpinner.setOnSpinnerItemSelectListener(new CommonSpinner.OnSpinnerItemSelectListener() {
@Override
public void onItemSelectListener(int pos) {
Toast.makeText(MainActivity.this, "pos="+pos, Toast.LENGTH_SHORT).show();
}
});
}

private List<String> getDatas() {
ArrayList<String> list=new ArrayList<>();
for (int i=1;i<15;i++){
list.add("第"+i+"周");
}
return list;
}

}

源码下载地址