ImageAdapter和PopupWindow的使用

本文详细介绍了在Android应用中如何使用PopupWindow组件,包括其基本构造、属性设置、事件监听以及布局实现。重点阐述了如何通过适配器(ImageAdapter)将GridView与PopupWindow结合使用,展示了一个完整的实例代码,包括布局文件和事件处理逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[quote]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>
}
});

}

} [/quote]

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

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

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

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

ImageAdapter的扩展
[quote]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
};
}
[/quote]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值