![](/img/trans.png)
[英]One Select Radio button using multiple radio buttons on RecyclerView
[英]Multiple radio-buttons in listview gets selected even if I select one button after scrolling
我有一个我正在渲染的卡片视图列表,在每张卡片中都有关于该特定路线和radio button
,点击后
我面临的问题
当我选择第三个单选按钮时,它会在列表呈现下一个视图时选择第三个项目,这就是 arrayadapter 的工作方式。 即使在我的列表视图中我使用了android:choiceMode="singleChoice"
仍然没有选择单个单选按钮。
我的 RouteAdapter 类 whee 我试图检查按下了哪个单选按钮
class RouteAdapter extends ArrayAdapter<RouteItem> {
private Context mContext;
RouteAdapter(Activity context, List<RouteItem> routes) {
super(context, 0, routes);
this.mContext = context;
}
@NonNull
@Override
public View getView(final int position, final View convertView, @NonNull ViewGroup parent) {
View listItemView = convertView;
if(listItemView == null) {
// we are passing false as we don't want to attach it to view just yet,
// first set the proper name and details then set it.
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.route_view, parent, false);
}
// getting the route at that position
RouteItem route = getItem(position);
......
final RadioButton rb = (RadioButton) listItemView.findViewById(R.id.radio_button);
//todo: fix radio button to save prefs
rb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RouteItem route = getItem(position);
Log.d("RouteAdapter.class", "getView: "+ route.getRoute_id());
rb.toggle();
}
});
return listItemView;
}
}
我的 activity_view_route.xml 在这里我渲染了所有的 routeItems
<?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="match_parent">
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/add_route"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="true"
android:choiceMode="singleChoice"
tools:context="com.sjain.routeplanner.ViewRouteActivity" />
<TextView
android:id="@+id/empty_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textAppearance="?android:textAppearanceMedium" />
<ProgressBar
android:id="@+id/loading_indicator"
style="@style/Widget.AppCompat.ProgressBar"
android:indeterminateTint="@color/teal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
这是我的 route_view.xml
<?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="match_parent">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_centerHorizontal="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/route_name"
android:id="@+id/view_name"
android:textSize="20sp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:text="@string/endpoints"
android:id="@+id/view_start"
android:textSize="18sp"
android:layout_below="@id/view_name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:text="@string/view_waypoints"
android:id="@+id/waypoints"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_below="@id/view_start"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:text="@string/endpoints"
android:id="@+id/view_end"
android:textSize="18sp"
android:layout_below="@id/waypoints"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radio_button"
android:layout_alignParentEnd="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/view_map"
android:padding="8dp"
android:background="@color/com_facebook_blue"
android:layout_alignBottom="@id/view_end"
android:layout_alignParentEnd="true"
android:layout_marginEnd="7dp"
android:text="@string/view_map"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/view_id"
android:visibility="gone"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
我的 routeItem 类
class RouteItem {
private String route_name;
private String start_point;
private String end_point;
private String route_id;
private ArrayList<String> waypoints;
private ArrayList<String> waypoint_name;
RouteItem(String route_name, String route_id, String start_point, String end_point, ArrayList<String> waypoints, ArrayList<String> waypoint_name) {
this.route_name = route_name;
this.route_id = route_id;
this.start_point = start_point;
this.end_point = end_point;
this.waypoints = waypoints;
this.waypoint_name = waypoint_name;
}
String getRoute_name() {
return route_name;
}
String getStart_point() {
return start_point.split(";")[0];
}
String getEnd_point() {
return end_point.split(";")[0];
}
String getEnd_point_coordinate() {
return end_point.split(";")[1];
}
String getStart_point_coordinate() {
return start_point.split(";")[1];
}
String getRoute_id() {
return route_id;
}
ArrayList<String> getWaypoints() {
return waypoints;
}
ArrayList<String> getWaypoint_name() {
return waypoint_name;
}
}
我知道也有类似 radio-group 的东西,但这里似乎没有选择,因为列表是根据从服务器获得的路由数量动态生成的。 我的问题与此问题类似, 当我们滚动列表时,自定义列表视图中的 Android 单选按钮会更改其状态,但这也没有得到正确回答。
如果有人可以请让我知道如何解决此问题,并获取所选卡的位置。
好的。 首先将您的 RouteItem 类更改为:
class RouteItem {
private String route_name;
private String start_point;
private String end_point;
private String route_id;
private ArrayList<String> waypoints;
private ArrayList<String> waypoint_name;
private String radioButtonState = "unSelected";
RouteItem(String route_name, String route_id, String start_point, String end_point, ArrayList<String> waypoints, ArrayList<String> waypoint_name) {
this.route_name = route_name;
this.route_id = route_id;
this.start_point = start_point;
this.end_point = end_point;
this.waypoints = waypoints;
this.waypoint_name = waypoint_name;
}
String getRoute_name() {
return route_name;
}
String getStart_point() {
return start_point.split(";")[0];
}
String getEnd_point() {
return end_point.split(";")[0];
}
String getEnd_point_coordinate() {
return end_point.split(";")[1];
}
String getStart_point_coordinate() {
return start_point.split(";")[1];
}
String getRoute_id() {
return route_id;
}
ArrayList<String> getWaypoints() {
return waypoints;
}
ArrayList<String> getWaypoint_name() {
return waypoint_name;
}
public String getRadioButtonState() {
return radioButtonState;
}
public void setRadioButtonState(String radioButtonState) {
this.radioButtonState = radioButtonState;
}
}
现在更新您的适配器。
class RouteAdapter extends ArrayAdapter<RouteItem> {
private Context mContext;
List<RouteItem> routes;
RouteAdapter(Activity context, List<RouteItem> routes) {
super(context, 0, routes);
this.mContext = context;
this.routes = routes;
}
@NonNull
@Override
public View getView(final int position, final View convertView, @NonNull ViewGroup parent) {
View listItemView = convertView;
if(listItemView == null) {
// we are passing false as we don't want to attach it to view just yet,
// first set the proper name and details then set it.
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.route_view, parent, false);
}
// getting the route at that position
RouteItem route = getItem(position);
......
final RadioButton rb = (RadioButton) listItemView.findViewById(R.id.radio_button);
//todo: fix radio button to save prefs
if(route.getRadioButtonState().equals("unSelected")){
rb.setSelected(false);
}
else{
rb.setSelected(true);
}
rb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RouteItem route = getItem(position);
Log.d("RouteAdapter.class", "getView: "+ route.getRoute_id());
rb.toggle();
if(rb.isSelected()){
route.setRadioButtonState("selected");
}
else {
route.setRadioButtonState("unSelected");
}
for(int i=0; i<routes.size(); i++){
if(i != position){
routes.get(i).setRadioButtonState("unSelected");
}
}
notifyDataSetChanged();
}
});
return listItemView;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.