Android中自定义下拉样式Spinner(1),看懂这些帮你轻松解决就业问题

import java.util.List;

import org.gaochun.R;

import org.gaochun.adapter.SpinerAdapter;

import org.gaochun.adapter.SpinerAdapter.IOnItemSelectListener;

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;

/**

  • 自定义SpinerPopWindow类

  • @author gao_chun

*/

public class SpinerPopWindow extends PopupWindow implements OnItemClickListener{

private Context mContext;

private ListView mListView;

private SpinerAdapter mAdapter;

private IOnItemSelectListener mItemSelectListener;

public SpinerPopWindow(Context context)

{

super(context);

mContext = context;

init();

}

public void setItemListener(IOnItemSelectListener listener){

mItemSelectListener = listener;

}

public void setAdatper(SpinerAdapter 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);

}

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);

}

}

}

三、定义Adapter


package org.gaochun.adapter;

import java.util.List;

import org.gaochun.R;

import android.content.Context;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.TextView;

public class SpinerAdapter extends BaseAdapter {

public static interface IOnItemSelectListener{

public void onItemClick(int pos);

};

private List mObjects;

private LayoutInflater mInflater;

public SpinerAdapter(Context context,List mObjects){

this.mObjects = mObjects;

mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

public void refreshData(List objects, int selIndex){

mObjects = objects;

if (selIndex < 0){

selIndex = 0;

}

if (selIndex >= mObjects.size()){

selIndex = mObjects.size() - 1;

}

}

@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(mObjects.get(pos));

return convertView;

}

public static class ViewHolder

{

public TextView mTextView;

}

}

四、调用示例


public class MainActivity extends Activity implements OnClickListener,SpinerAdapter.IOnItemSelectListener{

private List mListType = new ArrayList(); //类型列表

private TextView mTView;

private SpinerAdapter mAdapter;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_search);

mTView = (TextView) findViewById(R.id.tv_value);

//初始化数据

String[] names = getResources().getStringArray(R.array.array_name);

for(int i = 0; i < names.length; i++){

mListType.add(names[i]);

}

mAdapter = new SpinerAdapter(this,mListType);

mAdapter.refreshData(mListType,0);

//显示第一条数据

//mTView.setText(names[0]);

//初始化PopWindow

mSpinerPopWindow = new SpinerPopWindow(this);

mSpinerPopWindow.setAdatper(mAdapter);

mSpinerPopWindow.setItemListener(this);

}

//设置PopWindow

private SpinerPopWindow mSpinerPopWindow;

private void showSpinWindow(){

Log.e("", “showSpinWindow”);

mSpinerPopWindow.setWidth(mTView.getWidth());

mSpinerPopWindow.showAsDropDown(mTView);

}

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.bt_dropdown:

showSpinWindow();

break;

}

}

/* (non-Javadoc)

  • @see org.gaochun.adapter.SpinerAdapter.IOnItemSelectListener#onItemClick(int)

*/

@Override

public void onItemClick(int pos) {

// TODO Auto-generated method stub

if (pos >= 0 && pos <= mListType.size()){

String value = mListType.get(pos);

mTView.setText(value.toString());

}

}

}

源码下载:[http://downlo

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

ad.csdn.net/download/gao_chun/8653069]( )

上一篇:2021.12.08 P1848 [USACO12OPEN]Bookshelf G(线段树优化DP)


下一篇:JAVA概述(8) 循环语句(流程控制)(理论1)