PopupWindow弹出框

PopupWindow 弹出框显示
    关键字:PopupWindow 弹出框  setContentView(view) 设定内容显示    参数是view对象
            setTouchable(true) 设定可触摸,保证点击空白区域能够使弹框回收
            setFocusable(true)  设定焦点
    public class PopupWindow_Activity extends AppCompatActivity {
    public TextView mtextview;
    public ListView popupListView;
    public List<Student> list = new ArrayList<>();
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_popup_window_layout);

        mtextview = (TextView) findViewById(R.id.popupwindow_textview);


        LayoutInflater inflater = LayoutInflater.from(this);//解析文件布局使用
        View popupContentView = inflater.inflate(R.layout.activity_my_object_adapter_layout,null);
        popupListView = (ListView) popupContentView.findViewById(R.id.myobjectadapter);
        initData();// 加载数据
        MyStudentAdapter myStudentAdapter = new MyStudentAdapter(this,list);
        popupListView.setAdapter(myStudentAdapter);

        // TODO: 2016/6/12 PopupWindow 是系统准备好的实例化后直接用
        final PopupWindow popupWindow = new PopupWindow(this);
        popupWindow.setContentView(popupContentView);
        popupWindow.setWidth(400);
        popupWindow.setHeight(400);
        // TODO: 2016/6/12 以下属性保证点击其它区域,弹出的内容可以自行回收
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setFocusable(true);
        popupWindow.setTouchable(true);


        mtextview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(PopupWindow_Activity.this,mtextview.getText(),Toast.LENGTH_SHORT).show();
                if(popupWindow.isShowing()){
                    popupWindow.dismiss();
                }else{
                    popupWindow.showAsDropDown(view);
                }
            }
        });

    }
    public void initData(){

        addData("账上1","男",R.drawable.liying6);
        addData("账上2","男",R.drawable.liying6);
        addData("账上3","男",R.drawable.liying6);
        addData("账上4","男",R.drawable.liying6);
        addData("账上5","男",R.drawable.liying6);
        addData("账上6","男",R.drawable.liying6);
        addData("账上7","男",R.drawable.liying6);
        addData("账上8","男",R.drawable.liying6);
        addData("账上9","男",R.drawable.liying6);


    }
    public void addData(String name,String sex,int pic){

        Student studnent = new Student(name,sex,pic);
        list.add(studnent);
    }
    public class Student  {
        String name;
        String sex;
        int pic;
        public Student(String name,String sex,int pic){
            this.name = name;
            this.sex = sex;
            this.pic = pic;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getSex() {
            return sex;
        }

        public void setSex(String sex) {
            this.sex = sex;
        }

        public int getPic() {
            return pic;
        }

        public void setPic(int pic) {
            this.pic = pic;
        }
    }
    //自定义的适配器
    class MyStudentAdapter extends BaseAdapter {

        public List<Student> list = new ArrayList<>();
        public LayoutInflater layoutInflater;

        public MyStudentAdapter(Context context, List<Student> list) {
            this.list = list;
            layoutInflater = LayoutInflater.from(context);
        }

        public int getCount() {
            return list.size();
        }

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

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

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            HoldView holdView;
            if (view == null) {
                // todo 一级优化  优化View不被重复解析
                view = layoutInflater.inflate(R.layout.activity_demo__simple_layout, null);

                ImageView iconimag = (ImageView) view.findViewById(R.id.simple_demo_imageview);
                TextView titletxt = (TextView) view.findViewById(R.id.simple_demo_textviewtitle);
                TextView contenttxt = (TextView) view.findViewById(R.id.simple_demo_textviewcontent);

                // todo 二级优化  优化view控件不被重复加载
                holdView = new HoldView();
                holdView.iconimg = iconimag;
                holdView.titletxt = titletxt;
                holdView.contenttxt = contenttxt;
                view.setTag(holdView);// todo setTag里面放的是一个对象,为了view控件不被重复加载,需要再定义一个类放控件
            }

            holdView = (HoldView) view.getTag();
            // todo 从View对象中获取控件实例
            Student item = (Student) getItem(i);

            int icon = item.getPic();
            String title = item.getName();
            String content = item.getSex();

            holdView.iconimg.setImageResource(icon);
            holdView.titletxt.setText(title);
            holdView.contenttxt.setText(content);
            return view;
        }
    }
    public class HoldView{
        ImageView iconimg;
        TextView titletxt;
        TextView contenttxt;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值