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 如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/gridviewparent"
	android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="10dip"
    android:paddingRight="10dip"
    android:paddingTop="10dip"
    android:paddingBottom="10dip"
    android:gravity="center_horizontal" >
	<GridView 
		android:id="@+id/gridview" 
    	android:layout_width="fill_parent" 
   		android:layout_height="fill_parent"
    	android:numColumns="auto_fit"
    	android:verticalSpacing="30dp"
    	android:horizontalSpacing="15dp"
    	android:stretchMode="columnWidth"
    	android:gravity="center"
	/>
</LinearLayout>

 

main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/main"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <Button
        android:id="@+id/popup"
        android:scaleType="centerInside"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="POPUP"
        	/>
 </LinearLayout>

 

attrs如下

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="Gallery1">
        <attr name="android:galleryItemBackground" />
    </declare-styleable>
</resources>

 

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

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
    };
}

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值