ImageAdapter和PopupWindow的使用

public class SamplePopup extends Activity {
	 PopupWindow popup;
	 //GridView gView;
	 GridView gView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        final LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final LinearLayout musicGrid = (LinearLayout) inflater.inflate(R.layout.gridviewpopup, null, false);
          gView = (GridView) musicGrid.findViewById(R.id.gridview);
        
        gView.setAdapter(new ImageAdapter(this));
        
         popup = new PopupWindow(this);
        popup.setContentView(musicGrid);
        popup.setTouchable(true);
        popup.setFocusable(true);
        
       
     
        gView.setOnItemClickListener(new Gallery.OnItemClickListener() 
        {
        	
            public void onItemClick(AdapterView parent, View v, int position, long id) 
            {   Log.i("huhudhufhud","");
               // popup.showAsDropDown(findViewById(R.id.main));
            	popup.dismiss();
                 
            }
           
        }); 
      
        final Button popupButton = (Button) findViewById(R.id.popup);
        popupButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	popup.setWidth(300);
            	popup.setHeight(200);
            	popup.showAtLocation(findViewById(R.id.main), Gravity.CENTER, 0, 0);
            }
        });
        
    }
    
 }

 粉红色部分就是vpopupwindow要使用的,红色的部分一定要添加 不然点击事件不执行。

layout.gridviewpopup 如下:



	

 

main



    

 

attrs如下



    
        
    

 

这个呢只是图片的一个外边框背景。

ImageAdapter的扩展

public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    private int itemBackground;

    public ImageAdapter(Context c) {
        mContext = c;
    
      //---setting the style---
        TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery1);
        itemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
        a.recycle();
    }

    public int getCount() {
        return images.length;
    }

    public Object getItem(int position) {
    //return images[position];
    	return position;
    }

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

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(mContext);
        } else {
            imageView = (ImageView) convertView;
        }
        imageView.setImageResource(images[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        imageView.setBackgroundResource(itemBackground);
        return imageView;
        
    }
    public Integer[] images = {
    		R.drawable.android,
    		R.drawable.icon,
    		R.drawable.android
    };
}

 

 

 

你可能感兴趣的:(android)