Android利用GridView实现单选效果
程序员文章站
2023-11-05 11:22:52
1.实现如图所示的单选效果
由于android提供的单选按钮radiobutton只能单行或单列显示,且样式并不美观,故可用gridview进行改造,实现单选效果,而...
1.实现如图所示的单选效果
由于android提供的单选按钮radiobutton只能单行或单列显示,且样式并不美观,故可用gridview进行改造,实现单选效果,而要实现这样的效果重点就在gridview的适配器这块了。
首先是gridview的item的xml:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <relativelayout android:id="@+id/options" android:layout_width="match_parent" android:layout_height="143dp"> <imageview android:id="@+id/imageview" android:layout_width="65dp" android:layout_height="65dp"/> <textview android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerinparent="true" android:text="通讯录"/> </relativelayout> </linearlayout>
里边很简单,就只用来放显示选中效果的imageview和文字的textview
接下来是适配器:
public class gridviewradioadapter extends baseadapter { private context mcontext; private int lastposition = -1; //记录上一次选中的图片位置,默认不选中 private string[] str = null; //放问题内容文字的数组 public gridviewradioadapter(context mcontext) { this.mcontext = mcontext; } public void setstr(string[] str){ //在activity中调用此方法传入问题的数组 this.str = str; } public void setselection(int position) { //在activity中gridview的onitemclicklistener中调用此方法,来设置选中位置 lastposition = position; } @override public int getcount() { return str.length; } @override public object getitem(int position) { return position; } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { viewholder viewholder = null; if (convertview == null){ viewholder = new viewholder(); convertview = layoutinflater.from(mcontext).inflate(r.layout.adapter_gridview_radio_item, null); viewholder.textview = (textview) convertview.findviewbyid(r.id.textview); viewholder.imageview = (imageview) convertview.findviewbyid(r.id.imageview); viewholder.relativelayout = (relativelayout) convertview.findviewbyid(r.id.options); convertview.settag(viewholder); }else { viewholder = (viewholder) convertview.gettag(); } viewholder.textview.settext(str[position]); if (lastposition == position){ //判断是否为选中项,选中项与非选中项设置不同的样式 switch (position){ //选中状态下设置样式 case 0: viewholder.imageview.setbackgroundresource(r.drawable.option_a_checked); break; case 1: viewholder.imageview.setbackgroundresource(r.drawable.option_b_checked); break; case 2: viewholder.imageview.setbackgroundresource(r.drawable.option_c_checked); break; case 3: viewholder.imageview.setbackgroundresource(r.drawable.option_d_checked); break; } viewholder.relativelayout.setbackgroundresource(r.drawable.shape_rect_orange); }else { //非选中状态下设置样式 switch (position){ case 0: viewholder.imageview.setbackgroundresource(r.drawable.option_a); break; case 1: viewholder.imageview.setbackgroundresource(r.drawable.option_b); break; case 2: viewholder.imageview.setbackgroundresource(r.drawable.option_c); break; case 3: viewholder.imageview.setbackgroundresource(r.drawable.option_d); break; } viewholder.relativelayout.setbackgroundresource(r.drawable.shape_rect_gray); } return convertview; } class viewholder{ private textview textview; private imageview imageview; private relativelayout relativelayout; } }
最后在activity中设置适配器:
gridviewradioadapter adapter = new gridviewradioadapter(mcontext); adapter.setstr(options); //传入问题的选项 mgridview.setadapter(adapter); mgridview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> arg0, view arg1, int position, long arg3) { adapter.setselection(position); //传值更新 adapter.notifydatasetchanged(); //每一次点击通知adapter重新渲染 } });
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!
上一篇: 华人荣耀!5天前,她搞定了人类史上最大的半导体买卖
下一篇: Photoshop 打造一杯鲜美的橙汁