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)); <SPAN style="COLOR: #ff00ff"> popup = new PopupWindow(this); popup.setContentView(musicGrid); popup.setTouchable(true);</SPAN> <SPAN style="COLOR: #ff0000"> popup.setFocusable(true);</SPAN> 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)); <SPAN style="COLOR: #ff00ff">popup.dismiss();</SPAN> } }); final Button popupButton = (Button) findViewById(R.id.popup); popupButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { <SPAN style="COLOR: #ff00ff"> popup.setWidth(300); popup.setHeight(200); popup.showAtLocation(findViewById(R.id.main), Gravity.CENTER, 0, 0);</SPAN> } }); } }

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

layout.gridviewpopup 如下:
[code="java<?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>

attrsl:
 
 
引用
<?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--- <SPAN style="COLOR: #ffcc00">TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery1); itemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0); a.recycle();</SPAN> } 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); <SPAN style="COLOR: #ffcc00">imageView.setBackgroundResource(itemBackground);</SPAN> return imageView; } public Integer[] images = { R.drawable.android, R.drawable.icon, R.drawable.android }; }

猜你喜欢

转载自chenqiang5206.iteye.com/blog/1627248
今日推荐