精华内容
下载资源
问答
  • Android三级目录、ListView单选/GridView单选、ListView多选/GridView多选。选中后背景变色,字体变色。
  • 下面小编就为大家带来一篇详谈自定义View之GridView单选 金额选择Layout-ChooseMoneyLayout。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
  • Android ListView And GridView 单选模式 Android ListView And GridView 单选模式: 自定义TextView 使 ClassItemTextView 具有Checkable 功能 相当于 CheckBox public class ClassItemTextView extends ...

    Android ListView And GridView 单选模式

    Android ListView And GridView 单选模式:


    自定义TextView 使 ClassItemTextView 具有Checkable 功能 相当于 CheckBox

    public class ClassItemTextView extends TextView implements Checkable {
        private boolean mChecked;
        public ClassItemTextView(Context context) {
            super(context);
        }
    
        public ClassItemTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public ClassItemTextView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @Override
        public void setChecked(boolean checked) {
            mChecked = checked;
            setBackgroundDrawable(checked ? getResources().getDrawable(R.drawable.select_item): getResources().getDrawable(R.drawable.default_item));//当选中时呈现绿色
            setTextColor(checked? Color.WHITE:Color.parseColor("#7D7D7D"));
        }
    
        @Override
        public boolean isChecked() {
            return mChecked;
        }
    
        @Override
        public void toggle() {
            setChecked(!mChecked);
        }
    }
    

    Adapter 的写法

    
        /**
         * chooseAdapter
         */
        class ChooseClassAdapter extends BaseAdapter {
            private List<Boolean> booleanList;
            private String[] array;
    
            public ChooseClassAdapter(String[] array) {
                booleanList = new ArrayList<>();
                this.array = array;
            }
    
            @Override
            public int getCount() {
                return array.length;
            }
    
            @Override
            public Object getItem(int position) {
                return array[position];
            }
    
            @Override
            public long getItemId(int position) {
                return position;
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder viewHolder;
                if (convertView == null) {
                    convertView = View.inflate(SingleActivity.this, R.layout.choose_class_imte, null);
                    viewHolder = new ViewHolder();
                    viewHolder.classText = (ClassItemTextView) convertView.findViewById(R.id.class_item_choose);
                    convertView.setTag(viewHolder);
                } else {
                    viewHolder = (ViewHolder) convertView.getTag();
                }
                for (int i = 0; i < getCount(); i++) {
                    if (i == 0) {
                        booleanList.add(true);
                    } else {
                        booleanList.add(false);
                    }
    
                }
                viewHolder.classText.setChecked(booleanList.get(position));
                viewHolder.classText.setText(array[position].toString());
    
                return convertView;
            }
    
            /**
             * 匹配选择false
             *
             * @param position
             */
            public void selectPosition(int position) {
                for (int i = 0; i < getCount(); i++) {
                    if (position == i) {// 设置已选位置 (cast)
                        booleanList.set(i, true);
                    } else {
                        booleanList.set(i, false);
                    }
                }
            }
    
            class ViewHolder {
                private ClassItemTextView classText;
            }
        }

    调用方式:

    
    public class SingleActivity extends AppCompatActivity {
        private GridView mListData;
        private String chooseSelect;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            String[] array = new String[]{"高一", "高二", "高三", "一年级", "二年级", "三年级", "四年级", "五年级", "六年级", "七年级", "八年级", "九年级"};
            chooseSelect = "高一";
            mListData = (GridView)findViewById(R.id.nianji_list);
            final ChooseClassAdapter chooseClassAdapter = new ChooseClassAdapter(array);
            mListData.setAdapter(chooseClassAdapter);
            mListData.setChoiceMode(mListData.CHOICE_MODE_SINGLE);
            mListData.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    
                    if (ListView.CHOICE_MODE_SINGLE == mListData.getChoiceMode()) {
                        String str = (String) mListData.getItemAtPosition(position);
                        chooseClassAdapter.selectPosition(mListData.getCheckedItemPosition());
                /*        ToastUtils.showMessage(mListData.getCheckedItemPosition() + "----" + str);*/
                        chooseClassAdapter.notifyDataSetChanged();
                        chooseSelect = str;
                        Toast.makeText(SingleActivity.this,chooseSelect+ "", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }

    效果如下:
    这里写图片描述

    链接http://download.csdn.net/download/bxllove/10219004


    展开全文
  • 使用js实现GridView单选效果自动设置交替行、选中行、鼠标移动行背景色
  • GridView单选按钮

    2017-02-08 15:02:13
    仿APP充值界面,启动默认选中,背景 ,边框,字体,颜色 ,都发生改变。还有单击监听事件
  • android ListView GridView 单选和复选列子1.看到论坛有 同学不会写 listview item 的单选和 复选 所以写了一个发出来 单选类(SignChooseActivity)package com.zhx.listdemo;import java.util.ArrayList; import ...

    android ListView GridView 单选和复选列子

    1.看到论坛有 同学不会写 listview item 的单选和 复选 所以写了一个发出来 单选类(SignChooseActivity)

    package com.zhx.listdemo;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.ListView;
    
    public class SignChooseActivity extends Activity {
        private ListView mListView;
        private TextAdapter mAdapter;
        private List<String> mList = new ArrayList<String>();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_sign);
            mListView = (ListView) findViewById(R.id.text_listview);
            for (int i = 0; i < 20; i++) {
                mList.add("去约车" + i);
            }
            mAdapter = new TextAdapter(mList, this);
            mListView.setAdapter(mAdapter);
    
        }
    }
    

    2.单选适配器

    package com.zhx.listdemo;
    
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import android.app.Activity;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.CheckBox;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    public class TextAdapter extends BaseAdapter {
        private List<String> list;
        private Activity activity;
        private Map<String, Boolean> isSelectedMap = new HashMap<String, Boolean>();
    
        public TextAdapter(List<String> list, Activity activity) {
            super();
            this.list = list;
            this.activity = activity;
            for (int i = 0; i < list.size(); i++) {
                isSelectedMap.put(i + "", false);
            }
        }
    
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return list == null ? 0 : list.size();
        }
    
        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }
    
        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }
    
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = activity.getLayoutInflater().inflate(
                        R.layout.test_item, null);
                holder.tv = (TextView) convertView.findViewById(R.id.test_tv);
                holder.box = (ImageView) convertView
                        .findViewById(R.id.test_check_box);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
    
            convertView.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
    
                    for (int i = 0; i < list.size(); i++) {
                        if (i == position) {
                            Log.i("点击项目", position + "");
                            isSelectedMap.put(i + "", true);
                        } else {
                            Log.i("未点击项目", position + "");
                            isSelectedMap.put(i + "", false);
                        }
                    }
                    notifyDataSetChanged();
                }
            });
            Log.i("选中项", position + "@" + isSelectedMap.get(position + "") + "");
            if (isSelectedMap.get(position + "")) {
                holder.box.setImageResource(R.drawable.multi_select_flag);
            } else {
                holder.box.setImageResource(R.drawable.ic_launcher);
            }
            holder.tv.setText(list.get(position));
            return convertView;
        }
    
        class ViewHolder {
            TextView tv;
            ImageView box;
        }
    
    }
    
    package com.zhx.listdemo;
    
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import android.app.Activity;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.CheckBox;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    public class TextAdapter extends BaseAdapter {
        private List<String> list;
        private Activity activity;
        private Map<String, Boolean> isSelectedMap = new HashMap<String, Boolean>();
    
        public TextAdapter(List<String> list, Activity activity) {
            super();
            this.list = list;
            this.activity = activity;
            for (int i = 0; i < list.size(); i++) {
                isSelectedMap.put(i + "", false);
            }
        }
    
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return list == null ? 0 : list.size();
        }
    
        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }
    
        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }
    
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = activity.getLayoutInflater().inflate(
                        R.layout.test_item, null);
                holder.tv = (TextView) convertView.findViewById(R.id.test_tv);
                holder.box = (ImageView) convertView
                        .findViewById(R.id.test_check_box);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
    
            convertView.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
    
                    for (int i = 0; i < list.size(); i++) {
                        if (i == position) {
                            Log.i("点击项目", position + "");
                            isSelectedMap.put(i + "", true);
                        } else {
                            Log.i("未点击项目", position + "");
                            isSelectedMap.put(i + "", false);
                        }
                    }
                    notifyDataSetChanged();
                }
            });
            Log.i("选中项", position + "@" + isSelectedMap.get(position + "") + "");
            if (isSelectedMap.get(position + "")) {
                holder.box.setImageResource(R.drawable.multi_select_flag);
            } else {
                holder.box.setImageResource(R.drawable.ic_launcher);
            }
            holder.tv.setText(list.get(position));
            return convertView;
        }
    
        class ViewHolder {
            TextView tv;
            ImageView box;
        }
    
    }

    3.单选用的是listview 复选用gridview 做例子 其实一样的换个名字就好了 (MultChooseActivity)

    package com.zhx.listdemo;
    
    
    import java.util.ArrayList;
    import java.util.List;
    
    import android.app.Activity;
    import android.graphics.BitmapFactory;
    import android.os.Bundle;
    import android.util.TypedValue;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.BaseAdapter;
    import android.widget.Button;
    import android.widget.GridView;
    import android.widget.ImageView;
    import android.widget.RelativeLayout.LayoutParams;
    import android.widget.TextView;
    
    public class MultChooseActivity extends Activity {
        private Button mBack;
        private Button mAll;
        private GridView mDisplay;
        private TextView mCount;
        private AlbumAdapter mAdapter;
    
        private List<String> mSelect = new ArrayList<String>();// 存放当前用户选择的图片的编号
        private int imagesId[] = { R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher,
                R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher,
                R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher,
                R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher,
                R.drawable.ic_launcher, R.drawable.ic_launcher,R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher,
                R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher,
                R.drawable.ic_launcher, R.drawable.ic_launcher,R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher,
                R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher,
                R.drawable.ic_launcher, R.drawable.ic_launcher,R.drawable.ic_launcher, R.drawable.ic_launcher };
    
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.album_activity);
            findViewById();
            setListener();
            init();
    
        }
    
        private void findViewById() {
            mBack = (Button) findViewById(R.id.album_back);
            mAll = (Button) findViewById(R.id.album_all);
            mDisplay = (GridView) findViewById(R.id.album_display);
            mCount = (TextView) findViewById(R.id.album_count);
        }
    
        private void setListener() {
            mBack.setOnClickListener(new OnClickListener() {
    
                public void onClick(View v) {
                    // 关闭当前界面
                    finish();
                }
            });
            mAll.setOnClickListener(new OnClickListener() {
    
                public void onClick(View v) {
                    if (mSelect.size() == imagesId.length) {
                        // 全部选中时,点击为反选,所以清空
                        mSelect.clear();
                    } else if (mSelect.size() > 0) {
                        // 当已选中时,点击为全选,添加没选择中的
                        for (int i = 0; i < imagesId.length; i++) {
                            if (mSelect.contains(String.valueOf(i))) {
                                continue;
                            }
                            mSelect.add(String.valueOf(i));
                        }
                    } else {
                        // 没选中,点击为全选,添加所有
                        for (int i = 0; i < imagesId.length; i++) {
                            mSelect.add(String.valueOf(i));
                        }
                    }
                    // 如果全部选中则显示反选,否则显示全选
                    if (mSelect.size() == imagesId.length) {
                        mAll.setText("反选");
                    } else {
                        mAll.setText("全选");
                    }
                    // 更新界面显示数量内容
                    initCount(mSelect.size());
                    // 刷新界面
                    mAdapter.notifyDataSetChanged();
                }
            });
            mDisplay.setOnItemClickListener(new OnItemClickListener() {
    
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                    // 查看当前点击的条目是否已经选中,选中则移出,没选中则删除
                    if (mSelect.contains(String.valueOf(arg2))) {
                        mSelect.remove(String.valueOf(arg2));
                    } else {
                        mSelect.add(String.valueOf(arg2));
                    }
                    // 如果全部选中则显示反选,否则显示全选
                    if (mSelect.size() == imagesId.length) {
                        mAll.setText("反选");
                    } else {
                        mAll.setText("全选");
                    }
                    // 更新界面显示数量内容
                    initCount(mSelect.size());
                    // 刷新界面
                    mAdapter.notifyDataSetChanged();
                }
            });
        }
    
        private void init() {
            // 更新界面显示数量内容
            initCount(mSelect.size());
            // 初始化适配器
            mAdapter = new AlbumAdapter();
            // 添加适配器
            mDisplay.setAdapter(mAdapter);
        }
    
        /**
         * 更新界面显示数量内容
         * 
         * @param count
         *            选中的图片数量
         */
        private void initCount(int count) {
            // 更新选择的图片数量,如果数量大于0,设置确定按钮为可用,反之不可用并修改字体颜色
            if (count > 0) {
                mCount.setText("已选择" + count + "张");
                mCount.setTextColor(0xFFFFFFFF);
            } else {
                mCount.setText("已选择0张");
                mCount.setTextColor(0xFF999999);
            }
        }
    
        private class AlbumAdapter extends BaseAdapter {
    
            public int getCount() {
                return imagesId.length;
            }
    
            public Object getItem(int position) {
                return null;
            }
    
            public long getItemId(int position) {
                return position;
            }
    
            public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder holder = null;
                if (convertView == null) {
                    convertView = LayoutInflater.from(MultChooseActivity.this).inflate(R.layout.album_activity_item, null);
                    holder = new ViewHolder();
                    holder.photo = (ImageView) convertView.findViewById(R.id.album_item_photo);
                    holder.select = (ImageView) convertView.findViewById(R.id.album_item_select);
                    // padding大小为40dip,计算px值
                    int padding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, MultChooseActivity.this
                            .getResources().getDisplayMetrics());
                    // 设置显示的图片大小为屏幕宽度1/4大小
                    LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                    params.width = (480 - padding) / 4;
                    params.height = (480 - padding) / 4;
                    holder.photo.setLayoutParams(params);
                    holder.select.setLayoutParams(params);
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();
                }
                // 添加图片
                holder.photo.setImageBitmap(BitmapFactory.decodeResource(getResources(), imagesId[position]));
                // 查看是否选中,选中则显示选中效果
                if (mSelect.contains(String.valueOf(position))) {
                    holder.select.setVisibility(View.VISIBLE);
                } else {
                    holder.select.setVisibility(View.GONE);
                }
                return convertView;
            }
    
            class ViewHolder {
                ImageView photo;
                ImageView select;
            }
        }
    }
    
    

    4 .下载地址https://github.com/zhoulinxue/Listdemo.git

    展开全文
  • 一个自定义Web控件,用于将单选按钮选择添加到GridView控件。
  • 一个自定义Web控件,用于将单选按钮选择添加到GridView控件。
  • GridView 单选变色

    千次阅读 2015-06-09 15:58:39
    1、首先在gridview适配器实现如下代码 // 点击的位置,初始化默认选择第一个 private int clickStatus = 0; // 定义一个公有方法,在activity中调用 public void setSeclection(int position) {  clickStatus = ...

    实现淘宝充话费时,选择话费的功能

    1、首先在gridview适配器实现如下代码

    // 点击的位置,初始化默认选择第一个

    private int clickStatus = 0;

    //  定义一个公有方法,在activity中调用

    public void setSeclection(int position) {
                clickStatus = position;
            }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                、、、、、、

                if (clickStatus==position) {
                    gridview_item_ll_id.setBackgroundResource(R.drawable.rounded_bg);
                    textView.setTextColor(Color.WHITE);
                }else {
                    gridview_item_ll_id.setBackgroundResource(R.drawable.rounded);
                    textView.setTextColor(Color.GRAY);
                }

                return convertView;
            }

    2、然后再你的activity中实现如下代码:

            gridview.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) {
                    // TODO Auto-generated method stub
                    adapter.setSeclection(arg2);
                    adapter.notifyDataSetChanged();

                }
            });

    3、gridview 一些特殊的属性

    1.android:numColumns=”auto_fit”   //GridView的列数设置为自动

    2.android:columnWidth=”90dp "       //每列的宽度,也就是Item的宽度

    3.android:stretchMode=”columnWidth"//缩放与列宽大小同步

    4.android:verticalSpacing=”10dp”          //两行之间的边距

    5.android:horizontalSpacing=”10dp”      //两列之间的边距 

    6.android:cacheColorHint="#00000000" //去除拖动时默认的黑色背景

    7.android:listSelector="#00000000"        //去除选中时的黄色底色

    8.android:scrollbars="none"                   //隐藏GridView的滚动条

    9.android:fadeScrollbars="true"             //设置为true就可以实现滚动条的自动隐藏和显示

    10.android:fastScrollEnabled="true"      //GridView出现快速滚动的按钮(至少滚动4页才会显示)

    11.android:fadingEdge="none"                //GridView衰落(褪去)边缘颜色为空,缺省值是vertical。(可以理解为上下边缘的提示色)

    12.android:fadingEdgeLength="10dip"   //定义的衰落(褪去)边缘的长度

    13.android:stackFromBottom="true"       //设置为true时,你做好的列表就会显示你列表的最下面

    14.android:transcriptMode="alwaysScroll" //当你动态添加数据时,列表将自动往下滚动最新的条目可以自动滚动到可视范围内

    15.android:drawSelectorOnTop="false"  //点击某条记录不放,颜色会在记录的后面成为背景色,内容的文字可见(缺省为false)


    4、搞定,以上是主要代码,来个图看看效果


    展开全文
  • Gridview 单选效果,在androidapp中实现非常频繁,我这里提供我的一种思路,这是我的思维导图:    怎么样将这样的思维导图翻译成java代码了,请看下面的代码了: // 电子产品的图片列表信息 static class ...

     Gridview 单选效果,在androidapp中实现非常频繁,我这里提供我的一种思路,这是我的思维导图:

      

      怎么样将这样的思维导图翻译成java代码了,请看下面的代码了:


      // 电子产品的图片列表信息
        static class ConfigInfo {
            static final int[] img = { R.drawable.electron_dvdplay,
                    R.drawable.electron_toudaishishexianji,
                    R.drawable.electron_miniyinxiang,
                    R.drawable.electron_weidan_camera };
        }
        static class ConfigInfos{
            static final int[] imgs = { R.drawable.electron_dedplat_select,
                R.drawable.electron_toudaishishexiangji_select,
                R.drawable.electron_miniyinxiang,
                R.drawable.electron_weidan_select };        
        }
        // GridView 设配数据
    
        class Gridadapter extends BaseAdapter {
    
            List<Electronic> listbean;
            Context context;
            private int lastPosition = -1; // 记录上一次选中的图片位置,-1表示未选中任何图片
            private boolean multiChoose; // 表示当前适配器是否允许多选
            private List<Boolean> mImage_bs = new ArrayList<Boolean>(); // 定义一个向量作为选中与否容器
            private List<Integer> mSelectItems = new ArrayList<Integer>();
    
            public Gridadapter(Context context, List<Electronic> listbean,
                    Boolean isMulti) {
                this.context = context;
                this.listbean = listbean;
                multiChoose = isMulti;
    
                for (int i = 0; i < listbean.size(); i++)
                    mImage_bs.add(false);
            }
    
            //
            // private LayerDrawable makeBmp(Electronic id, boolean isChosen) {
            // Bitmap mainBmp = ((BitmapDrawable) context.getResources()
            // .getDrawable(id.getIcon())).getBitmap();
            //
            // // 根据isChosen来选取对勾的图片
            // Bitmap seletedBmp;
            // if (isChosen == true)
            // seletedBmp = BitmapFactory.decodeResource(
            // context.getResources(), R.drawable.gridview_chech_bg1);
            // else
            // seletedBmp = BitmapFactory.decodeResource(
            // context.getResources(),
            // R.drawable.electron_shuma_camera);
            //
            // // 产生叠加图
            // Drawable[] array = new Drawable[2];
            // array[0] = new BitmapDrawable(mainBmp);
            // array[1] = new BitmapDrawable(seletedBmp);
            // LayerDrawable la = new LayerDrawable(array);
            // la.setLayerInset(50, 50, 50, 50, 50);
            // la.setLayerInset(1, 10, -5, 60, 45);
            //
            // return la; // 返回叠加后的图
            // }
    
            // public void changeState(int position) {
            // // 多选时
            // if (multiChoose == true) {
            // mImage_bs
            // .setElementAt(!mImage_bs.elementAt(position), position); // 直接取反即可
            // }
            // // 单选时
            // else {
            // if (lastPosition != -1)
            // mImage_bs.setElementAt(false, lastPosition); // 取消上一次的选中状态
            // mImage_bs
            // .setElementAt(!mImage_bs.elementAt(position), position); // 直接取反即可
            // lastPosition = position; // 记录本次选中的位置
            // }
            // notifyDataSetChanged(); // 通知适配器进行更新
            // }
    
            @Override
            public int getCount() {
                return listbean.size();
    
            }
    
            @Override
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return listbean.get(position);
            }
    
            @Override
            public long getItemId(int position) {
                return position;
            }
    
            @Override
            public View getView(final int position, View convertView,
                    ViewGroup parent) {
                final Handle handle;
                if (convertView == null) {
                    convertView = LayoutInflater.from(context).inflate(
                            R.layout.grivview_textview_img, null);
                    handle = new Handle();
                    handle.imageView = (ImageView) convertView
                            .findViewById(R.id.Grid_image);
                    handle.tv = (TextView) convertView.findViewById(R.id.grid_tv);
                    convertView.setTag(handle);
                } else {
                    handle = (Handle) convertView.getTag();
                }
                // 设置数据
                // handle.imageView.setBackgroundResource(listbean.get(position)
                // .getIcon());
                int resId = mImage_bs.get(position) ? ConfigInfos.imgs[position]
                        : ConfigInfo.img[position];
                handle.imageView.setImageResource(resId);
                // handle.imageView.setBackgroundDrawable(makeBmp(
                // listbean.get(position), mImage_bs.get(position)));
                handle.imageView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.v("图片中的项目", "你点击了按钮" + listbean.get(position)
                                + position);
                        boolean selected=mImage_bs.get(position);
                        mImage_bs.set(position, !selected); 
                        notifyDataSetChanged();
                    }
                });
    
                return convertView;
            }

     通过这么篇幅代码,我们可以得出来这样的结论:

      Ⅰ用了两个实体类存储图片的实体的信息,一个布尔数组来存储是否选中的结果。

      Ⅱ点击了相应的某项以后,使其该布尔数组所对应的布尔变量变成相应的非值。

      这样的gridview的运行结果如下:

      


    展开全文
  • .net基础框架gridwiew单选变色 最适用的Gridview模板
  • Android关于一个页面双列表内嵌gridview单选问题最近公司移动应用开发的时候,公司提出一个需求:一个Activity中左右两边各一个列表,每个列表的item中要现实两列数据也就是内嵌的GridView,然后实现整个页面的单选...
  • 改进gridView单选和多选功能 实现全选和取消全选功能
  • 思路:外层控件用的是GridView,里面每个item放一个FrameLayout,FrameLayout里面有Checkbox和ImageView,chechBox添加background实现选中效果,选中背景为透明,显示item的勾勾图标,不选中checkbox就有背景,挡住...
  • import android.content.Context; import android.text.TextUtils; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter;...
  • http://blog.csdn.net/fancylovejava/article/details/39966639http://blog.csdn.net/ljfbest/article/details/40685327http://blog.csdn.net/zhouyuanjing/article/details/8372686

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 4,466
精华内容 1,786
关键字:

gridview单选