类似网购电影票的简易GridView选择器

转载请注明出处: 源码下载: (csdn) (github)https://github.com/noyo/GridViewSelectPopWin

实现效果图

废话不多说,先上图片。

实现代码

MainActivity.java

package com.practice.noyet.gridviewselectpopwin;import android.app.Activity;import android.os.Bundle;import android.view.Gravity;import android.view.View;import android.widget.TextView;import java.util.ArrayList;import java.util.List;.GridViewSelectPopWin mPopWin;//自定义的GridView适配器private GridViewSelectPopWinAdapter mAdapter;//自定义填充数据private List<Integer> mList;private TextView mTextView;(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mTextView = (TextView) findViewById(R.id.content);findViewById(R.id.text).setOnClickListener(this);mList = new ArrayList<>();for (int i = 0; i < 12; i++) {//每个网格模拟显示的数据mList.add(1000 + i);}mPopWin = new GridViewSelectPopWin(this);mAdapter = new GridViewSelectPopWinAdapter(this,Util.getMetrics(this).widthPixels / 6,//网格宽度(暂定每行显示6个网格)mList);}(View view) {switch (view.getId()) {case R.id.text:mPopWin.showWin(mPopWin.getContentView(), Gravity.BOTTOM, 0, 0, new GridViewSelectPopWin.onGridViewClickListener() {() {mTextView.setText(“选中位置:” + mAdapter.getSelectIndex() + ” 选中位置内容:” + mList.get(mAdapter.getSelectIndex()));mPopWin.dismiss();}}, mAdapter);break;}}}

GridViewSelectPopWin.java

package com.practice.noyet.gridviewselectpopwin;import android.app.Activity;import android.graphics.drawable.ColorDrawable;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup.LayoutParams;import android.view.WindowManager;import android.widget.GridView;import android.widget.LinearLayout;import android.widget.PopupWindow;{private Activity mContext;private GridView mGridView;private onGridViewClickListener mConfirm;public GridViewSelectPopWin(Activity context) {super(context);this.mContext = context;View view = LayoutInflater.from(context).inflate(R.layout.include_select_gridview, null);view.findViewById(R.id.pop_cancel).setOnClickListener(this);view.findViewById(R.id.pop_confirm).setOnClickListener(this);setContentView(view);setWidth(LayoutParams.MATCH_PARENT);setHeight(LayoutParams.WRAP_CONTENT);setOutsideTouchable(true);setBackgroundDrawable(new ColorDrawable(0xe0000000));setFocusable(true);setAnimationStyle(R.style.popwin_anim_style);//弹出软键盘时,编辑框变位置时会有一个alpha动画setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);setOnDismissListener(new OnDismissListener() {() {// TODO Auto-generated method stubbackgroundAlpha(1f);}});mGridView = (GridView) view.findViewById(R.id.pop_gendi_gridview);init();}() {mGridView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));}(View parent, int gravity, int x, int y) {// TODO Auto-generated method stubbackgroundAlpha(0.5f);super.showAtLocation(parent, gravity, x, y);}(View parent, int gravity, int x, int y,onGridViewClickListener confirm,GridViewSelectPopWinAdapter mAdapter) {this.showAtLocation(parent, gravity, x, y);this.mConfirm = confirm;mGridView.setAdapter(mAdapter);}/*** 设置添加屏幕的背景透明度** @param bgAlpha 0为不可见,1为透明*/(float bgAlpha) {WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();lp.alpha = bgAlpha; //0.0-1.0mContext.getWindow().setAttributes(lp);}(View v) {// TODO Auto-generated method stubswitch (v.getId()) {case R.id.pop_cancel:dismiss();break;case R.id.pop_confirm:mConfirm.onClick();break;default:break;}}{void onClick();}}世上并没有用来鼓励工作努力的赏赐,所有的赏赐都只是被用来奖励工作成果的。

类似网购电影票的简易GridView选择器

相关文章:

你感兴趣的文章:

标签云: