登录 立即注册
安币:

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

[复制链接]
来自: MrlLee 分类: Android精品源码 上传时间: 2016-9-1 11:10:14
立即下载
收藏

项目介绍:

实现代码
MainActivity.java
[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;

public class MainActivity extends Activity implements View.OnClickListener {

    //选择器弹出框
    private GridViewSelectPopWin mPopWin;
    //自定义的GridView适配器
    private GridViewSelectPopWinAdapter mAdapter;
    //自定义填充数据
    private List mList;
    private TextView mTextView;

    @Override
    protected void onCreate(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);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.text:
                mPopWin.showWin(mPopWin.getContentView(), Gravity.BOTTOM, 0, 0, new GridViewSelectPopWin.onGridViewClickListener() {
                    @Override
                    public void onClick() {
                        mTextView.setText("选中位置:" + mAdapter.getSelectIndex() +  "   选中位置内容:" + mList.get(mAdapter.getSelectIndex()));
                        mPopWin.dismiss();
                    }
                }, mAdapter);
                break;
        }
    }
}

GridViewSelectPopWin.java
[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;

public class GridViewSelectPopWin extends PopupWindow implements OnClickListener {
    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() {

            @Override
            public void onDismiss() {
                // TODO Auto-generated method stub
                backgroundAlpha(1f);
            }

        });

        mGridView = (GridView) view.findViewById(R.id.pop_gendi_gridview);
        init();
    }

    private void init() {


        mGridView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        // TODO Auto-generated method stub
        backgroundAlpha(0.5f);
        super.showAtLocation(parent, gravity, x, y);

    }

    public void showWin(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为透明
     */
    private void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        mContext.getWindow().setAttributes(lp);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.pop_cancel:
                dismiss();
                break;
            case R.id.pop_confirm:
                mConfirm.onClick();
                break;
            default:
                break;
        }
    }

    public interface onGridViewClickListener {
        void onClick();
    }

}

GridViewSelectPopWinAdapter.java
[Java] 查看源文件 复制代码
package com.practice.noyet.gridviewselectpopwin;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class GridViewSelectPopWinAdapter extends BaseAdapter {

    private Context mContext;
    private int mWidth;
    //存储每个item的选中状态
    private List index;
    //当前被选中的item位置
    private int selectIndex;
    private List mList;

    public int getSelectIndex() {
        return selectIndex;
    }

    public List getIndex() {
        return index;
    }


    public GridViewSelectPopWinAdapter(Context context, int width, List list) {
        mContext = context;
        mWidth = width;
        this.mList = list;
        index = new ArrayList<>();
        selectIndex = -1;
        for (int i = 0; i < mList.size(); i++) {
            index.add(false);
        }
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int i) {
        return mList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return (long) i;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        Holder holder;
        if (view != null) {
            holder = (Holder) view.getTag(R.id.tag_data);
        } else {
            view = LayoutInflater.from(mContext).inflate(R.layout.pop_win_gridview_item, null);
            holder = new Holder();
            holder.mItem = (RelativeLayout) view.findViewById(R.id.pop_win_gridview_item);
            float scale = mContext.getResources().getDisplayMetrics().density;
            holder.mItem.setLayoutParams(new LinearLayout.LayoutParams(mWidth, (int) (50 * scale + 0.5F)));
            holder.mImageView = (TextView) view.findViewById(R.id.pop_gridview_item_iv);
            holder.mTextView = (TextView) view.findViewById(R.id.pop_gridview_item_tv);
            view.setTag(R.id.tag_data, holder);
            view.setTag(R.id.tag_id, position);
        }
        holder.mTextView.setText(mList.get(position) + "");
        view.setOnClickListener(listener);
        if (index.get(position)) {
            setSelectIndex(holder, true);
        } else {
            setSelectIndex(holder, false);
        }
        return view;
    }

    private void setSelectIndex(Holder holder, boolean isSelect) {
        if (isSelect) {
            holder.mItem.setSelected(true);
            holder.mImageView.setVisibility(View.VISIBLE);
            holder.mTextView.setTextColor(mContext.getResources().getColor(android.R.color.black));
        } else {
            holder.mItem.setSelected(false);
            holder.mImageView.setVisibility(View.GONE);
            holder.mTextView.setTextColor(mContext.getResources().getColor(android.R.color.darker_gray));
        }
    }

    private View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Holder holder = (Holder) view.getTag(R.id.tag_data);
            int position = (Integer) view.getTag(R.id.tag_id);
            if (!index.get(position)) {
                index.set(position, true);
                if (selectIndex == -1) {
                    setSelectIndex(holder, true);
                } else {
                    index.set(selectIndex, false);
                    notifyDataSetChanged();
                }
                selectIndex = position;
            } else {
                index.set(position, false);
                notifyDataSetChanged();
                selectIndex = -1;
            }
        }
    };

    static class Holder {
        RelativeLayout mItem;
        TextView mImageView;
        TextView mTextView;
    }
}

xml
[XML] 查看源文件 复制代码



    

        

还有些自定义的图片和style这里就不赘述了,想看的欢迎下载源码:

相关源码推荐:

我来说两句
所有评论(13)
春城计算机 2016-9-1 11:21:59
好好 学习了 确实不错
回复
suzhouliuyan 2016-9-1 11:32:01
apkbus好的内容真的很多~赞
回复
aliouswang 2016-9-1 11:44:21
apkbus好的内容真的很多~赞
回复
myself2015 2016-9-1 11:58:41
支持,感谢,祝巴士越来越好~
回复
九酒 2016-9-1 12:34:00
膜拜大神~
回复
孙林 2016-9-1 13:02:53
精华内容,楼主V5!
回复
fancy 2016-9-1 13:06:14
支持,感谢,祝巴士越来越好~
回复
12下一页
上传代码
查看数:2920 收藏数:11 下载数:12 点赞数:1
状态:已购或VIP 售价:10(原价:10)金钱 下载权限:初级码农 
代码贡献英雄榜
用户名 下载数
联系我们
首页/微信公众账号投稿

帖子代码编辑/版权问题

QQ:435399051,769657487

如何获得代码达人称号

如何成为签约作者

联系我们
关闭
合作电话:
15618560077
Email:
805941275@qq.com
商务市场合作/投稿
问题反馈及帮助
联系我们

广告投放| 下载客户端|申请友链|手机版|站点统计|安卓巴士 ( 粤ICP备15117877号 )

快速回复 返回顶部 返回列表