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重新渲染 }});
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持编程小技巧!
总结以上是内存溢出为你收集整理的Android利用GridView实现单选效果全部内容,希望文章能够帮你解决Android利用GridView实现单选效果所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)