package com.cnblogs.hibraincol;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.os.Bundle;
import android.widget.GridView;
import android.widget.PopupWindow;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.view.View;
import android.widget.SimpleAdapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.graphics.drawable.BitmapDrawable;
import android.view.View.OnTouchListener;
import android.view.Gravity;
public class GridRadioGroup extends Activity {
private PopupWindow mPopGridRadioGroup=null;
private ViewGroup mViewGroup=null;
private GridView mGrid=null;
private SimpleAdapter mWishesAdapter;
private int mLastItme=-1;
private String[] mWishes = {"睡到自然醒", "钱多手抽筋", "加薪又升职", "妞多任我选"};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnPop= (Button)findViewById(R.id.btn_pop);
btnPop.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
popGridRadioGroup();
}
});
}
private void popGridRadioGroup(){
if(mPopGridRadioGroup==null){
LayoutInflater mLayoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
mWishesAdapter = getRadioButtonAdapter(R.drawable.btn_radio_off, mWishes);
mViewGroup = (ViewGroup) mLayoutInflater.inflate(R.layout.radiogroup_gridview, null, true);
mGrid = (GridView)mViewGroup.findViewById(R.id.gridview);
mGrid.setAdapter(mWishesAdapter);
mGrid.requestFocus();
mGrid.setOnItemClickListener( new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
if(mLastItme != arg2){
//更新图标、实现单选
if(mLastItme>=0){
ChangeRadioImg(mWishesAdapter, mLastItme,false);
}
mLastItme = arg2;
ChangeRadioImg(mWishesAdapter,arg2,true);
}
}
});
//注意,PopupWindow的第一个参数必须是没有父亲的view对象即顶层View。
mPopGridRadioGroup = new PopupWindow(mViewGroup, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT,true);
mPopGridRadioGroup.setBackgroundDrawable(new BitmapDrawable());
mPopGridRadioGroup.setTouchInterceptor(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
mPopGridRadioGroup.dismiss();
return true;
}
return false;
}
});
}
mPopGridRadioGroup.showAtLocation(findViewById(R.id.mainView), Gravity.CENTER, 0, 0);
mPopGridRadioGroup.update();
}
/*
* 创建包含多个radiobutton的Adapter。
* RadioButton的图片有redioImg指定,RadioButton的文字由radioNameArray指定
* RadioButton的图片和文字的相对位置在item_radiobutton.xml布局文件中指定。
*/
private SimpleAdapter getRadioButtonAdapter(int redioImage, String[] radioNameArray) {
ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
for (int i = 0; i < radioNameArray.length; i++) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("itemRadioImg", redioImage);
map.put("itemRadioText", radioNameArray[i]);
data.add(map);
}
SimpleAdapter simperAdapter = new SimpleAdapter(this, data,
R.layout.item_radiobutton, new String[] { "itemRadioImg", "itemRadioText" },
new int[] { R.id.item_radioimg, R.id.item_radiotext });
return simperAdapter;
}
/*
* 根据选中的状态来更新图标。也就是实现我们自定义RadioGroup的单选功能
*/
private void ChangeRadioImg(SimpleAdapter adapter,int selectedItem, boolean on) {
HashMap<String, Object> map = (HashMap<String, Object>)adapter.getItem(selectedItem);
if(on)
map.put("itemRadioImg", R.drawable.btn_radio_on);
else
map.put("itemRadioImg", R.drawable.btn_radio_off);
adapter.notifyDataSetChanged();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Andriod: 网格状RadioGroup的实现
共33个文件
class:10个
png:8个
xml:6个
5星 · 超过95%的资源 需积分: 36 391 下载量 2 浏览量
2011-05-28
14:11:17
上传
评论 3
收藏 74KB ZIP 举报
温馨提示
Andriod: 网格状RadioGroup的实现。 简单示例,源码短小清晰.
资源推荐
资源详情
资源评论
收起资源包目录
GridRadioGroup.zip (33个子文件)
GridRadioGroup
.project 850B
default.properties 362B
bin
com
cnblogs
hibraincol
R$string.class 545B
R$layout.class 489B
GridRadioGroup$1.class 833B
R$drawable.class 520B
GridRadioGroup$3.class 1KB
GridRadioGroup$2.class 2KB
GridRadioGroup.class 5KB
R.class 559B
R$attr.class 352B
R$id.class 650B
GridRadioGroup.apk 30KB
resources.ap_ 24KB
classes.dex 7KB
proguard.cfg 1KB
AndroidManifest.xml 690B
src
com
cnblogs
hibraincol
GridRadioGroup.java 4KB
res
values
strings.xml 337B
layout
item_radiobutton.xml 730B
radiogroup_gridview.xml 1KB
main.xml 727B
drawable-ldpi
icon.png 2KB
drawable-hdpi
btn_radio_off.png 2KB
btn_radio_on.png 2KB
icon.png 4KB
drawable
btn_radio_off.png 2KB
grid_corner.xml 545B
btn_radio_on.png 2KB
icon.png 4KB
drawable-mdpi
icon.png 3KB
.classpath 280B
gen
com
cnblogs
hibraincol
R.java 2KB
assets
共 33 条
- 1
Niosm
- 粉丝: 12
- 资源: 47
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页