Android PopupWindow嵌套ListView(模仿分类下拉菜单效果)

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用

1:布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
 >  
  
       <TextView   
                android:id="@+id/tv_text_age"  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:background="@drawable/choolse_bg"  
                android:text="不限"  
                android:paddingLeft="12dp"  
                android:textColor="#9d72d2"  
                android:layout_centerInParent="true"  
                android:gravity="center_vertical"  
                />  
</RelativeLayout>
    package com.jackie.popupwindow;  
    import java.util.ArrayList;  
    import java.util.List;  
      
    import android.app.Activity;  
    import android.graphics.Color;  
    import android.graphics.drawable.ColorDrawable;  
    import android.os.Bundle;  
    import android.view.LayoutInflater;  
    import android.view.View;  
    import android.view.View.OnClickListener;  
    import android.view.ViewGroup;  
    import android.view.ViewGroup.LayoutParams;  
    import android.widget.AdapterView;  
    import android.widget.AdapterView.OnItemClickListener;  
    import android.widget.BaseAdapter;  
    import android.widget.ListView;  
    import android.widget.PopupWindow;  
    import android.widget.TextView;  
      
    public class MainActivity extends Activity implements OnClickListener {  
        private TextView tv_text_age;  
        private PopupWindow pop;  
        ListView listView = null;  
        private List<String> datas;  
        private SelectAgeAdapter mSelectAdapter;  
        private LayoutInflater inflater;  
        @Override  
        protected void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.activity_main);  
            initData();  
            inflater = LayoutInflater.from(this);  
            tv_text_age = (TextView) findViewById(R.id.tv_text_age);  
            tv_text_age.setOnClickListener(this);  
        }  
        private void initData() {  
            datas = new ArrayList<String>();  
            for(int i = 0; i < 50; i++){  
                datas.add("HelloWorld - " + i);  
            }  
        }  
        @Override  
        public void onClick(View v) {  
            if(v.getId() == R.id.tv_text_age){  
                if(pop == null) {  
                    listView = new ListView(this);  
                    listView.setDividerHeight(1);  
                    listView.setBackgroundResource(R.drawable.kge_feek_bg);  
                    listView.setCacheColorHint(0x00000000);  
                    mSelectAdapter = new SelectAgeAdapter();  
                    pop = new PopupWindow(listView, tv_text_age.getWidth(), LayoutParams.WRAP_CONTENT, true);  
                }  
                listView.setAdapter(mSelectAdapter);  
                pop.setBackgroundDrawable(new ColorDrawable(Color.RED));
                pop.showAsDropDown(tv_text_age, 0, 0);  
                  
                listView.setOnItemClickListener(new OnItemClickListener() {  
                    @Override  
                    public void onItemClick(AdapterView<?> parent, View view,  
                            int position, long id) {  
                        tv_text_age.setText(datas.get(position));  
                        dismissPopWindow();  
                    }  
                });  
            }  
        }  
        public void dismissPopWindow() {  
            if(pop != null && pop.isShowing()){  
                pop.dismiss();  
            }  
        }  
          
        class SelectAgeAdapter extends BaseAdapter {  
      
            @Override  
            public int getCount() {  
                return datas != null&&datas.size() > 0 ? datas.size() : 0;  
            }  
      
            @Override  
            public Object getItem(int position) {  
                return datas.get(position);  
            }  
      
            @Override  
            public long getItemId(int position) {  
                return position;  
            }  
      
            @Override  
            public View getView(int position, View convertView, ViewGroup parent) {  
                ViewHolder holder;  
                if (convertView == null) {  
                    holder = new ViewHolder();  
                    convertView = inflater.inflate(R.layout.item, null);  
                    holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);  
                    convertView.setTag(holder);  
                      
                } else {  
                    holder = (ViewHolder) convertView.getTag();  
                }  
                holder.tv_name.setText(datas.get(position));  
                return convertView;  
            }  
        }  
        class ViewHolder {  
            TextView tv_name;  
        }  
    }  


标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:ftp 实现文件的上传下载以及列出文件列表Java代码

下一篇:Java 代码实现七牛云存储图片