2018-01-23 09:31:25 bxllove 阅读数 134
  • winform基础

    主讲内容 第一讲 winform课程介绍(完结) 第二讲 Windows编程基础-hello world(完结) 第三讲Windows编程基础-常用控件(完结) 第四讲 多文档界面MDI处理和菜单组件(完结) 第五讲 Windows控件-单选、复选、图片控件(完结) 等

    4011 人正在学习 去看看 胡延亮

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


2015-12-29 09:50:41 lzq520210 阅读数 341
  • winform基础

    主讲内容 第一讲 winform课程介绍(完结) 第二讲 Windows编程基础-hello world(完结) 第三讲Windows编程基础-常用控件(完结) 第四讲 多文档界面MDI处理和菜单组件(完结) 第五讲 Windows控件-单选、复选、图片控件(完结) 等

    4011 人正在学习 去看看 胡延亮
设置ListView为单选模式
<ol class="linenums"><li value="1" class="L0"><span class="pln">        </span><span class="tag"><ListView</span></li><li class="L1"><span class="pln">            </span><span class="atn">android:id</span><span class="pun">=</span><span class="atv">"@+id/lv_category"</span></li><li class="L2"><span class="pln">            </span><span class="atn">android:choiceMode</span><span class="pun">=</span><span class="atv">"singleChoice"</span></li><li class="L3"><span class="pln">            </span><span class="atn">android:layout_width</span><span class="pun">=</span><span class="atv">"0dp"</span></li><li class="L4"><span class="pln">            </span><span class="atn">android:layout_height</span><span class="pun">=</span><span class="atv">"match_parent"</span></li><li class="L5"><span class="pln">            </span><span class="atn">android:layout_weight</span><span class="pun">=</span><span class="atv">"2"</span></li><li class="L6"><span class="pln">            </span><span class="atn">android:divider</span><span class="pun">=</span><span class="atv">"@drawable/iv_left_item_separate_line"</span><span class="pln"> </span><span class="tag">></span></li><li class="L7"><span class="pln">        </span><span class="tag"></ListView></span></li></ol>

设置Item的Layout的背景为一个Selector:
bg_listview_item.xml

<ol class="linenums"><li value="1" class="L0"><span class="tag"><selector</span><span class="pln"> </span><span class="atn">xmlns:android</span><span class="pun">=</span><span class="atv">"http://schemas.android.com/apk/res/android"</span><span class="tag">></span></li><li class="L1"><span class="pln">    </span><span class="tag"><item</span><span class="pln"> </span><span class="atn">android:state_activated</span><span class="pun">=</span><span class="atv">"true"</span><span class="pln"> </span><span class="atn">android:drawable</span><span class="pun">=</span><span class="atv">"@android:drawable/list_selector_background_selected"</span><span class="pln"> </span><span class="tag">/></span></li><li class="L2"><span class="pln">    </span><span class="tag"><item</span><span class="pln"> </span><span class="atn">android:drawable</span><span class="pun">=</span><span class="atv">"@color/transparent"</span><span class="pln"> </span><span class="tag">/></span></li><li class="L3"><span class="tag"></selector></span></li></ol>
<ol class="linenums"><li value="1" class="L0"><span class="pun"><?</span><span class="pln">xml version</span><span class="pun">=</span><span class="str">"1.0"</span><span class="pln"> encoding</span><span class="pun">=</span><span class="str">"utf-8"</span><span class="pun">?></span></li><li class="L1"><span class="tag"><TextView</span><span class="pln"> </span><span class="atn">xmlns:android</span><span class="pun">=</span><span class="atv">"http://schemas.android.com/apk/res/android"</span></li><li class="L2"><span class="pln">    </span><span class="atn">android:layout_width</span><span class="pun">=</span><span class="atv">"match_parent"</span></li><li class="L3"><span class="pln">    </span><span class="atn">android:layout_height</span><span class="pun">=</span><span class="atv">"match_parent"</span></li><li class="L4"><span class="pln">    </span><span class="atn">android:background</span><span class="pun">=</span><span class="atv">"@drawable/bg_listview_item"</span></li><li class="L5"><span class="pln">    </span><span class="atn">android:gravity</span><span class="pun">=</span><span class="atv">"center"</span></li><li class="L6"><span class="pln">    </span><span class="atn">android:minHeight</span><span class="pun">=</span><span class="atv">"80dp"</span></li><li class="L7"><span class="pln">    </span><span class="atn">android:textColor</span><span class="pun">=</span><span class="atv">"@color/black"</span></li><li class="L8"><span class="pln">    </span><span class="atn">android:textSize</span><span class="pun">=</span><span class="atv">"26sp"</span><span class="pln"> </span><span class="tag">/></span></li></ol>
转载:http://www.pocketdigi.com/20120714/890.html
2015-08-05 19:52:21 houxuehan 阅读数 2226
  • winform基础

    主讲内容 第一讲 winform课程介绍(完结) 第二讲 Windows编程基础-hello world(完结) 第三讲Windows编程基础-常用控件(完结) 第四讲 多文档界面MDI处理和菜单组件(完结) 第五讲 Windows控件-单选、复选、图片控件(完结) 等

    4011 人正在学习 去看看 胡延亮

Android ListView中点击单行实现RadioButton的单选功能,自定义Item布局文件

  需求描述:Android的ListView中,在自定义的Item中添加RadioButton来指示选中的某个单行,要求点击单行而不是RadioButton就触发选择功能。
①在包含ListView的layout文件中,将ListView元素属性设置为单选模式android:choiceMode="singleChoice"
 <ListView
            android:id="@+id/lvRegular"
            android:dividerHeight="1px"
            android:layout_width="match_parent"
            android:choiceMode="singleChoice"
            android:layout_height="wrap_content" >
        </ListView>

②在ListView对应的Item的布局文件中,RadioButton元素设置不可点击属性android:clickable="false"这样可以屏蔽掉RadioButton的点击就会(取消)勾选的默认动作,
同时该布局的根元素设置android:descendantFocusability="blocksDescendants"以确保焦点只在根元素上。
这样就可以点击行实现(取消)勾选功能。
        <RadioButton
            android:id="@+id/regularRadio"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:clickable="false"
            android:layout_gravity="right" />

③最为关键的一步,即实现单选操作功能,在Adapter中实现
自定义的RegularAdapte继承BaseAdapter,其他方法不写,这里只贴出关键代码getView()
@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v;
		final int mPosition = position;
		if (convertView == null) {
			v = mInflater.inflate(R.layout.item_load_fragment_list_car, parent,
					false);
		} else {
			v = convertView;
		}
		//Log.v("Regular", position + " " + v.hashCode() + "  " + checkedPosition);
		Regular r = (Regular) getItem(position);
		((TextView) v.findViewById(R.id.regularNo)).setText(r.get_ccbh());
		((TextView) v.findViewById(R.id.regularCph)).setText(r.get_cph());
		((TextView) v.findViewById(R.id.regularFcsj)).setText(r.get_fcsj());
		((TextView) v.findViewById(R.id.regularLc)).setText(r.get_lc() + "km");
		((TextView) v.findViewById(R.id.regularXlmc)).setText(r.get_xlmc());
		final RadioButton rb = (RadioButton) v.findViewById(R.id.regularRadio);
		rb.setChecked(mPosition == checkedPosition);
		//请在xml文件中在根元素设置屏蔽焦点属性,以及在radioButton中设置不可点击事件
		v.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (checkedPosition == mPosition)
					checkedPosition = -1;
				else
					checkedPosition = mPosition;
				// rb.setChecked(curPosi==checkedPosition);//此行代码可以略去,见最后一行代码
				// 回传数据给fragment
				mFragment.onItemClick(checkedPosition);
				// 下面一行代码非常重要,重新调用getView()刷新可见区域的view,达到动态刷新的目的,不用担心性能问题,
				// 因为仅仅只是重新填充数据而不会重新绘制view(有这个作判断convertView == null)
				RegularAdapter.this.notifyDataSetChanged();
			}
		});
		return v;
	}<pre name="code" class="java">private int checkedPosition = -1;// 用于标识选中的radiobutton 的位置
//自定义内部接口
public interface OnItemClickListener{
      void onItemClick(int position);
}
实现思路是用一个字段checkedPosition来标识已经选择的单项,position是唯一的,因此记录它即可。
RegularAdapter.this.notifyDataSetChanged();//这行代码太关键了,有了它,完美解决了RadioButton错乱的问题
               另外要明白一点的是getView()的执行时机,一般而言当超出屏幕显示范围的项,重新回到显示范围内时,getView()会被执行,这样通过
rb.setChecked(mPosition == checkedPosition); 来确定RadioButton的状态,由于convertView有了null的判断,达到了复用的目的,getView()方法的执行不会
        带来太大的性能问题,只是重新填充了数据而已,并且convertView还会缓存一部分数据(好像12项)。
至此,在Adapter中非常简单的实现了单选功能,那么在Activity或者fragment中如何取得选中的行的对象呢?
               mFragment.onItemClick(checkedPosition);//这是个回调函数,通过它,将当前选中的行的唯一表示position回传给Fragment或Activity,此处mFragment是fragment
这需要在Adapter中定义一个内部接口,名字随便取,这里为OnItemClickListener接口。
接下来在Fragment或者Activity中实现该接口,并同样定义属性private int checkedPosition = -1;// 用于标识选中的radiobutton 的位置
在其实现接口的方法中同步赋值即可。这样就完成了回传功能。
@Override
public void onItemClick(int currentPosition) {
checkedPosition = currentPosition;
}
然后就可以Fragment或Activity中这样取得选中行的对象
            Object obj = listview.getItemAtPosition(checkedPosition);//obj需要强制转化为你自定义的对象
至此,该需求就实现了。
2018-01-16 15:16:04 Zhang_YingJie 阅读数 113
  • winform基础

    主讲内容 第一讲 winform课程介绍(完结) 第二讲 Windows编程基础-hello world(完结) 第三讲Windows编程基础-常用控件(完结) 第四讲 多文档界面MDI处理和菜单组件(完结) 第五讲 Windows控件-单选、复选、图片控件(完结) 等

    4011 人正在学习 去看看 胡延亮

这是一个关于listview多选与单选的demo.
先放demo的下载链接demo传送门
过后补具体内容….

2015-12-17 16:09:09 lmy0111ly 阅读数 303
  • winform基础

    主讲内容 第一讲 winform课程介绍(完结) 第二讲 Windows编程基础-hello world(完结) 第三讲Windows编程基础-常用控件(完结) 第四讲 多文档界面MDI处理和菜单组件(完结) 第五讲 Windows控件-单选、复选、图片控件(完结) 等

    4011 人正在学习 去看看 胡延亮
设置ListView为单选模式
  1. <ListView
  2. android:id="@+id/lv_category"
  3. android:choiceMode="singleChoice"
  4. android:layout_width="0dp"
  5. android:layout_height="match_parent"
  6. android:layout_weight="2"
  7. android:divider="@drawable/iv_left_item_separate_line" >
  8. </ListView>

设置Item的Layout的背景为一个Selector:
bg_listview_item.xml

  1. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  2. <item android:state_activated="true" android:drawable="@android:drawable/list_selector_background_selected" />
  3. <item android:drawable="@color/transparent" />
  4. </selector>
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TextView xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="@drawable/bg_listview_item"
  6. android:gravity="center"
  7. android:minHeight="80dp"
  8. android:textColor="@color/black"
  9. android:textSize="26sp" />
转载:http://www.pocketdigi.com/20120714/890.html
没有更多推荐了,返回首页