关闭

自定义spinner下拉框

标签: 自定义SpinnerSpinner
923人阅读 评论(0) 收藏 举报
分类:

本文出自:http://blog.csdn.net/dt235201314/article/details/52372609

一丶简介

一直觉得自定义View该加强学习,自定义spinner下拉框就作为一个开始,网上一找一大把例子,前段时间找了一个还用到了工作中

二丶演示图



三丶代码实现

分析spinner组成:1.带边框的TextView   2.TextView右边显示上下箭头  3.TextView下显示装ListView显示item得Popupwindow。

SpinerPopWindow.java  自定义Popupwindow类 初始化PopupWindow显示的布局,以及一些参数,并且给listView设置适配器

public class SpinerPopWindow<T> extends PopupWindow {
   private LayoutInflater inflater;
   private ListView mListView;
   private List<T> list;
   private MyAdapter  mAdapter;
   
   public SpinerPopWindow(Context context,List<T> list,OnItemClickListener clickListener) {
      super(context);
      inflater=LayoutInflater.from(context);
      this.list=list;
      init(clickListener);
   }
   
   private void init(OnItemClickListener clickListener){
      View view = inflater.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.setAdapter(mAdapter=new MyAdapter());
      mListView.setOnItemClickListener(clickListener);
   }
   
   private class MyAdapter extends BaseAdapter{
      @Override
      public int getCount() {
         return list.size();
      }

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

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

      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
         ViewHolder holder=null;
         if(convertView==null){
            holder=new ViewHolder();
            convertView=inflater.inflate(R.layout.spiner_item_layout, null);
            holder.tvName=(TextView) convertView.findViewById(R.id.tv_name);
            convertView.setTag(holder);
         }else{
            holder=(ViewHolder) convertView.getTag();
         }
         holder.tvName.setText(getItem(position).toString());
         return convertView;
      }
   }
   
   private class ViewHolder{
      private TextView tvName;
   }
}

MainActivity.java  程序入口,初始化数据,初始化自定义PopupWIndow,TextView点击之后显示PopupWIndow,处理ListView的点击事件.

public class MainActivity extends Activity {
   private SpinerPopWindow<String> mSpinerPopWindow;
   private List<String> list;
   private TextView tvValue;

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

      initData();

      tvValue = (TextView) findViewById(R.id.tv_value);
      tvValue.setOnClickListener(clickListener);
      mSpinerPopWindow = new SpinerPopWindow<String>(this, list,itemClickListener);
      mSpinerPopWindow.setOnDismissListener(dismissListener);
   }
   

   private OnDismissListener  dismissListener=new OnDismissListener() {
      @Override
      public void onDismiss() {
         setTextImage(R.drawable.icon_down);
      }
   };


   private OnItemClickListener itemClickListener = new OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
         mSpinerPopWindow.dismiss();
         tvValue.setText(list.get(position));
         Toast.makeText(MainActivity.this, "�����:" + list.get(position),Toast.LENGTH_LONG).show();
      }
   };


   private OnClickListener clickListener = new OnClickListener() {
      @Override
      public void onClick(View v) {
         switch (v.getId()) {
         case R.id.tv_value:
            mSpinerPopWindow.setWidth(tvValue.getWidth());
            mSpinerPopWindow.showAsDropDown(tvValue);
            setTextImage(R.drawable.icon_up);
            break;
         }
      }
   };


   private void initData() {
      list = new ArrayList<String>();
      for (int i = 0; i < 5; i++) {
         list.add("test:" + i);
      }
   }


   private void setTextImage(int resId) {
      Drawable drawable = getResources().getDrawable(resId);
      drawable.setBounds(0, 0, drawable.getMinimumWidth(),drawable.getMinimumHeight());
      tvValue.setCompoundDrawables(null, null, drawable, null);
   }
}

spiner_item_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical">

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="fdsfdsfdsf"
        android:textColor="@color/content_color"
        android:textSize="20sp"/>

</LinearLayout>

spiner_window_layout.xml

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

    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="#00000000"
        android:background="@drawable/shape_popupwindow_list_bg"
        android:scrollbars="none" >
    </ListView>
</LinearLayout>
主页面就一个TextView(略)

四丶总结

1.这是上周得事情,压倒现在,很多事情需要规划

2.项目进入写P层,M层,调接口后台取数据得阶段,正是不擅长的地方,要学的很多


1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    我的github简书订阅号和QQ群
    个人资料
    • 访问:142456次
    • 积分:3259
    • 等级:
    • 排名:第11121名
    • 原创:145篇
    • 转载:2篇
    • 译文:0篇
    • 评论:245条
    博客专栏
    打赏 微信扫描 支付宝扫描
    微信 支付宝
    最新评论