我正在使用recylerview设置单选按钮。但是在选择时,selected会在滚动时更改其位置。请帮助我。
Thanx in advance!!!
我的RecylerView布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="@+id/dummy_recycle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
我的单选按钮布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/radio_dummy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</RelativeLayout>
我的ReCyrVIEW Java文件
public class Dummy extends AppCompatActivity {
ArrayList<String> arr_qty;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dummy);
arr_qty=new ArrayList<>();
for(int i =0;i<50;i++){
arr_qty.add(String.valueOf(i));
}
RecyclerView dummy = (RecyclerView)findViewById(R.id.dummy_recycle);
DummyAdapter adapter = new DummyAdapter(getApplicationContext(), arr_qty);
dummy.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));
dummy.setAdapter(adapter);
}
}
我的RecylerView适配器
public class DummyAdapter extends RecyclerView.Adapter<DummyAdapter.MyViewHolder>{
private Context c;
ArrayList<String> arr=new ArrayList<>();
public DummyAdapter(Context context, ArrayList<String> arr_qty) {
this.arr = arr_qty;
this.c = context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.dummy_value, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return arr.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public RadioButton radio;
public MyViewHolder(View itemView) {
super(itemView);
radio = (RadioButton) itemView.findViewById(R.id.radio_dummy);
}
}
}
最佳答案
在RecycleView适配器中,可以在下面的行中添加。它已经在为我的代码工作了。此代码设置了列表位置修复。它对我的代码有效。
public int getItemViewType(int position) {
return position;
}