listview点击item,在item的上边弹出popwindow

来源:互联网 发布:java报表工具 开源 编辑:程序博客网 时间:2024/05/15 01:03
public class ListActivity extends Activity {


    
    private PopupWindow popupWindow;
    
    private TextView mTextView;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        
        LinearLayout ll = new LinearLayout(this);
        ll.setLayoutParams( new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        ListView listView = new ListView(this);
        ll.addView(listView);
        setContentView(ll);
        
        MyAdapter adapter = new MyAdapter();
        listView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        
        initPopView();
        
        listView.setOnTouchListener(new OnTouchListener() {
            
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (popupWindow.isShowing()) {
                        popupWindow.dismiss();
                    }
                }
                return false;
            }
        });
    }
    
    private void initPopView() {
        View view = LayoutInflater.from(this).inflate(R.layout.pop_view, null);
        mTextView = (TextView) view.findViewById(R.id.textview);
        popupWindow = new PopupWindow(view, 300, 100);
    }
    
    
    class MyAdapter extends BaseAdapter{


        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return 10;
        }


        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }


        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }


        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            
            final TextView textView = new TextView(ListActivity.this);
            textView.setPadding(20, 20, 0, 20);
            textView.setTextSize(25);
            textView.setText("Hello Android");
            
            textView.setOnTouchListener(new OnTouchListener() {
                
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    // TODO Auto-generated method stub
                    if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        int[] location = new int[2];
                        v.getLocationOnScreen(location);
                        
                        int dalta = (300 - v.getWidth()) / 2;
                        Log.e(this.getClass().getName(), ":" + location[0] + "=======" + location[1]);
                        popupWindow.showAtLocation(textView, Gravity.CENTER|Gravity.TOP, location[0] - dalta, location[1] - 100);
                        mTextView.setText("第" + position + "个");
                    }
                    return false;
                }
            });
            
            return textView;
        }
        
    }
0 0
原创粉丝点击