您的位置:首页 > 运维架构

自定义PopWindow,实现简单的回调函数,自己实现简单的CallBack

2016-03-17 19:26 435 查看
首先定义一个接口类:

 interface CallBack {

     void onItemClick(int pos);

}

下面的是Bean类;

public class City {

    public String name;

    public String number;

    public City(String name,String number){

        this.name = name;

        this.number = number;

    }

    @Override

    public String toString() {

        return "City [name=" + name + ", number=" + number + "]";

    }

}

下面是三个简单的xml文件:

1、    activity_main.xml

<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"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerInParent="true"

        android:text="点击弹出PopWindow" />

</RelativeLayout>

2、PopWindow中的xml文件里是一个ListView

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:background="@android:color/background_light"

    android:orientation="vertical" >

    <ListView

        android:id="@+id/lv"

        android:layout_gravity="center"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" >

    </ListView>

</LinearLayout>

3、ListView的每个Item中是一个TextView

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >

    <TextView

        android:id="@+id/tv_item"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" />

</LinearLayout>

最主要的是自定义的PopWindow

/** @TODO

 * @author miaotingxu

 * @date 2016-3-17 上午9:55:12 */

public class MyPopWindow extends PopupWindow {

    

    View conentView;

    List<City> list;

    

    public MyPopWindow(final Activity context,List<City> list){

        this.list = list;

        LayoutInflater inflater = (LayoutInflater) context

                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        conentView = inflater.inflate(R.layout.pop, null);

        int h = context.getWindowManager().getDefaultDisplay().getHeight();

        int w = context.getWindowManager().getDefaultDisplay().getWidth();

        // 设置SelectPicPopupWindow的View

        this.setContentView(conentView);

        // 设置SelectPicPopupWindow弹出窗体的宽

        this.setWidth(100);

        // 设置SelectPicPopupWindow弹出窗体的高

        this.setHeight(LayoutParams.WRAP_CONTENT);

        // 设置SelectPicPopupWindow弹出窗体可点击

        this.setFocusable(true);

        this.setOutsideTouchable(true);

        // 刷新状态

        this.update();

        // 实例化一个ColorDrawable颜色为半透明

        ColorDrawable dw = new ColorDrawable(0000000000);

        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ,设置其他控件变化等操作

        this.setBackgroundDrawable(dw);

        // mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);

        ListView lv = (ListView) conentView.findViewById(R.id.lv);

        

        Myadapter adapter=new Myadapter(context);

        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new OnItemClickListener(){

            @Override

            public void onItemClick(AdapterView<?> parent, View view,

                    int position, long id) {

                cBack.onItemClick(position);

                dismiss();

            }

        });

    }

    

    CallBack cBack;

    public void setOnItem(CallBack cBack) {

        this.cBack = cBack;

    }

    

    class Myadapter extends BaseAdapter {

        LayoutInflater inflater;

        public Myadapter(Context ctx){

            inflater = LayoutInflater.from(ctx);

        }

        @Override

        public int getCount() {

            return list.size();

        }

        

        @Override

        public Object getItem(int position) {

            return position;

        }

        /** TODO */

        @Override

        public long getItemId(int position) {

            return position;

        }

        /** TODO */

        @Override

        public View getView(int position, View convertView, ViewGroup parent) {

            if (convertView == null) {

                convertView = inflater.inflate(R.layout.item, null);

            }

            TextView tv = (TextView) convertView.findViewById(R.id.tv_item);

            tv.setText("" + list.get(position).name);

            return convertView;

        }

    }

    

}

下面是MainActivity

 public class MainActivity extends Activity {

    

    private TextView tv;

    ArrayList<City> list = new ArrayList<City>();

    private MyPopWindow popupWindow;

    

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        tv = (TextView) findViewById(R.id.tv);

        list.add(new City("name1","123"));

        list.add(new City("name2","223"));

        list.add(new City("name3","333"));

        popupWindow = new MyPopWindow(this,list );

        popupWindow.setOnItem(new CallBack(){

            @Override

            public void onItemClick(int pos) {

                Toast.makeText(MainActivity.this, list.get(pos).number, 1).show();

            }

        });

        tv.setOnClickListener(new View.OnClickListener(){

            @Override

            public void onClick(View v) {

                popupWindow.setWidth(v.getWidth());

                popupWindow.showAsDropDown(v);

            }

        });

    }

    

    class Myadapter extends BaseAdapter {

        LayoutInflater inflater;

        public Myadapter(Context ctx){

            inflater = LayoutInflater.from(ctx);

        }

        /** TODO */

        @Override

        public int getCount() {

            return list.size();

        }

        /** TODO */

        @Override

        public Object getItem(int position) {

            

            return position;

            

        }

        @Override

        public long getItemId(int position) {

            return position;

        }

        @Override

        public View getView(int position, View convertView, ViewGroup parent) {

            if (convertView == null) {

                convertView = inflater.inflate(R.layout.item, null);

            }

            TextView tv = (TextView) convertView.findViewById(R.id.tv_item);

            tv.setText("" + list.get(position));

            return convertView;

        }

    }

}

Demo到此结束,这个Demo中用到了自定义PopWindw,回调函数,希望能帮助比我还菜的菜鸟,

也希望大牛能指教一些有技术含量的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: