本文介绍了安卓:单选按钮不断改变其状态时,列表视图滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个列表项三个单选按钮列表视图,问题是,当我滚动列表视图,选定的单选按钮的位置得到改变。所以,请让我知道如何保持单选按钮的选择不变,即使我滚动列表视图。我的code是,
RadioGroupAdapter.java
公共RadioGroupAdapter(上下文的背景下,INT layoutResourceId,
选项[]数据){
超级(上下文,layoutResourceId,数据);
this.layoutResourceId = layoutResourceId;
this.context =背景;
this.data =数据;
} @覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
查看排= convertView;
MatrixHolder支架=无效; 如果(行== NULL){
LayoutInflater充气=((活动)上下文).getLayoutInflater();
行= inflater.inflate(layoutResourceId,父母,假); 持有人=新MatrixHolder();
holder.txtTitle =(TextView中)row.findViewById(R.id.heading);
holder.group =(RadioGroup中)row.findViewById(R.id.radio_group1);
最后的单选按钮[] = RB新单选[2];
对(INT I = 0; I&2;我++){ RB [I] =新的单选按钮(背景);
// RB [I] .setButtonDrawable(R.drawable.single_radio_chice);
RB [I] .setId(ⅰ);
RadioGroup.LayoutParams PARAMS =新RadioGroup.LayoutParams(
0,LayoutParams.WRAP_CONTENT);
params.weight = 1.0F;
params.setMargins(5,0,5,10);
holder.group.addView(RB [I],则params); //的单选按钮被添加到radioGroup中代替的布局
}
//((MatrixHolder)持有者).group.clearCheck();
row.setTag(保持器);
}其他{
支架=(MatrixHolder)row.getTag();
} 选项option =数据[位置]
holder.txtTitle.setText(option.title);
返回行;
}
解决方案
通过添加两种不同功能的解决我的问题:
我知道它几乎是一个黑客,但它的工作原理。所以永远不要介意;)
@覆盖
公众诠释getViewTypeCount(){
//计数= ArrayList的大小。
返回data.length;
} @覆盖
公众诠释getItemViewType(INT位置){ 返回的位置;
}
I have a listview with three Radio Buttons in a list item, Issue is that when I scroll list view, the radio buttons selected position gets changed. So please let me know how to keep radio button's selection intact even if I scroll list view. My code is,
RadioGroupAdapter.java
public RadioGroupAdapter(Context context, int layoutResourceId,
Option[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
MatrixHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new MatrixHolder();
holder.txtTitle = (TextView) row.findViewById(R.id.heading);
holder.group = (RadioGroup) row.findViewById(R.id.radio_group1);
final RadioButton[] rb = new RadioButton[2];
for(int i=0; i<2; i++){
rb[i] = new RadioButton(context);
// rb[i].setButtonDrawable(R.drawable.single_radio_chice);
rb[i].setId(i);
RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(
0, LayoutParams.WRAP_CONTENT);
params.weight=1.0f;
params.setMargins(5, 0, 5, 10);
holder.group.addView(rb[i],params); //the RadioButtons are added to the radioGroup instead of the layout
}
// ((MatrixHolder)holder).group.clearCheck();
row.setTag(holder);
} else {
holder = (MatrixHolder) row.getTag();
}
Option option = data[position];
holder.txtTitle.setText(option.title);
return row;
}
解决方案
Solved my problem by adding two different functions:I know its almost a hack but it works. So never mind ;)
@Override
public int getViewTypeCount() {
//Count=Size of ArrayList.
return data.length;
}
@Override
public int getItemViewType(int position) {
return position;
}
这篇关于安卓:单选按钮不断改变其状态时,列表视图滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!