Ars*_*war 40 android radio-button android-layout
我想在列表中显示一个选择选项.我正在使用RadioButton我的listView排.我知道这RadioGroup用于单一选择.
但问题是我已经添加了RadioButton我的ListRowView.现在我想将所有列表项添加到一个中RadioButton.我正在使用Custom Adapter和进入getView().我得到RadioButton的getView(),但要添加它时RadioGroup它说
"视图已经有父级,在父级之前调用removeView()"
我知道它是真的,但如果我从视图中删除它.然后它是不可见的.
我还尝试以RadioButton编程方式创建和添加.然后添加它RadioGrop.然后查看列表行.但这次作为父母RadioGroup,所以它再说
"视图已经有父级,在父级之前调用removeView()"
我想要做的是一次只选择列表中的一个项目.我的代码如下.
public class MyAdapter extends ArrayAdapter < MyMenuItem > {
private LayoutInflater mInflater ;
int mResource ;
List < MyMenuItem > mData ;
Context context;
public MyAdapter ( Context context , int resource , int textViewResourceId , List < MyMenuItem > data ) {
super ( context , resource , textViewResourceId , data ) ;
this.context = context;
mData = data ;
mResource = resource ;
mInflater = ( LayoutInflater ) getSystemService ( Context.LAYOUT_INFLATER_SERVICE ) ;
}
@ Override
public View getView ( int position , View convertView , ViewGroup parent ) {
ViewHolder holder = null ;
if ( convertView == null ) {
convertView = mInflater.inflate ( mResource , null ) ;
holder = new ViewHolder ( ) ;
holder.icon = ( ImageView ) convertView.findViewById ( R.id.icon ) ;
holder.text = ( TextView ) convertView.findViewById ( R.id.text ) ;
holder.comment = ( TextView ) convertView.findViewById ( R.id.comment ) ;
LinearLayout lin = ( LinearLayout ) convertView.findViewById ( R.id.linerList ) ;
RadioButton rbtn = new RadioButton ( context );
LayoutParams lparam = new LayoutParams ( LayoutParams.WRAP_CONTENT , LayoutParams.WRAP_CONTENT );
rbtn.setSelected ( false );
holder.check = rbtn;
//radioGroup.addView ( rbtn );
lin.addView ( rbtn , 0 );
convertView.setTag ( holder ) ;
} else {
holder = ( ViewHolder ) convertView.getTag ( ) ;
}
holder.text.setText ( mData.get ( position ).getText ( ) ) ;
holder.comment.setText ( mData.get ( position ).getComment ( ) ) ;
holder.icon.setImageResource ( getApplicationContext ( ).getResources ( ).getIdentifier ( mData.get ( position ).getIcon ( ) ,
"drawable" , getPackageName ( ) )
) ;
return convertView ;
}
}
Run Code Online (Sandbox Code Playgroud)
我的XML行
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<LinearLayout
android:id = "@+id/linerList"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="6dip" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="My Application"
android:textSize="20sp"
android:singleLine="true"
android:ellipsize="marquee"
android:textColor="@color/white" />
<TextView
android:id="@+id/comment"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
android:text="Simple application that shows how to use RelativeLayout"
android:textSize="14sp"
android:textColor="@color/light_gray" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

Ino*_*man 37
这个解决方案很有效,而且很干净,但可能会有一些更好的解决方案.
您应该使用适配器来管理单选按钮状态.
您必须保留对最后检查的单选按钮的引用,然后RadioButton.onClick设置最后选中的单选按钮setChecked(false).
还记得将新选择的单选按钮设置为最后选择的单选按钮.
看例子:
private class MyAdapter extends ArrayAdapter<String>{
private int mResourceId = 0;
private LayoutInflater mLayoutInflater;
private RadioButton mSelectedRB;
private int mSelectedPosition = -1;
public MyAdapter(Context context, int resource, int textViewResourceId, List<String> objects) {
super(context, resource, textViewResourceId, objects);
mResourceId = resource;
mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder;
if(view == null){
view = mLayoutInflater.inflate(mResourceId, parent, false);
holder = new ViewHolder();
holder.name = (TextView)view.findViewById(R.id.text);
holder.radioBtn = (RadioButton)view.findViewById(R.id.radioButton1);
view.setTag(holder);
}else{
holder = (ViewHolder)view.getTag();
}
holder.radioBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(position != mSelectedPosition && mSelectedRB != null){
mSelectedRB.setChecked(false);
}
mSelectedPosition = position;
mSelectedRB = (RadioButton)v;
}
});
if(mSelectedPosition != position){
holder.radioBtn.setChecked(false);
}else{
holder.radioBtn.setChecked(true);
if(mSelectedRB != null && holder.radioBtn != mSelectedRB){
mSelectedRB = holder.radioBtn;
}
}
holder.name.setText(getItem(position));
return view;
}
private class ViewHolder{
TextView name;
RadioButton radioBtn;
}
}
Run Code Online (Sandbox Code Playgroud)
希望它能为你做到.
pet*_*ejl 14
这是我的解决方案.它非常简单.
my_radio_adapter_item.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
... />
<RadioButton
android:id="@+id/radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:focusable="false"
... />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
MyRadioAdapter.java
public class MyRadioAdapter extends BaseAdapter
{
private Context mContext;
private ArrayList<Variation> mVariations;
private int mSelectedVariation;
public MyRadioAdapter(Context context, ArrayList<Variation> variations, int selectedVariation)
{
mContext = context;
mVariations = variations;
mSelectedVariation = selectedVariation;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
View view = convertView;
if(view==null)
{
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.my_radio_adapter_item, null);
}
final Variation variation = mVariations.get(position);
TextView name = (TextView) view.findViewById(R.id.name);
RadioButton radio = (RadioButton) view.findViewById(R.id.radio);
name.setText(variation.getName());
if(position==mSelectedVariation) radio.setChecked(true);
else radio.setChecked(false);
view.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
mSelectedVariation = position;
MyRadioAdapter.this.notifyDataSetChanged();
}
});
return view;
}
...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
60433 次 |
| 最近记录: |