Single selection in RecyclerView

后端 未结 15 1096
深忆病人
深忆病人 2020-11-22 15:15

I know there are no default selection methods in recyclerview class, But I have tried in following way,

public void onBindViewHolder(ViewHolder holder, final         


        
15条回答
  •  日久生厌
    2020-11-22 16:13

    This is how the Adapter class looks like :

    public class MyRecyclerViewAdapter extends RecyclerView.Adapter{
    
    Context context;
    ArrayList routeDetailsFromFirestoreArrayList_;
    public int  lastSelectedPosition=-1;
    
    
    public MyRecyclerViewAdapter(Context context, ArrayList routeDetailsFromFirestoreArrayList)
    {
        this.context = context;
        this.routeDetailsFromFirestoreArrayList_ = routeDetailsFromFirestoreArrayList;
    }
    
    @NonNull
    @Override
    public MyRecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i)
    {
        // LayoutInflater layoutInflater = LayoutInflater.from(mainActivity_.getBaseContext());
        LayoutInflater layoutInflater = LayoutInflater.from(viewGroup.getContext());
        View view = layoutInflater.inflate(R.layout.route_details, viewGroup, false);
        return new MyRecyclerViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(@NonNull final MyRecyclerViewHolder myRecyclerViewHolder, final int i) {
    
        /* This is the part where the appropriate checking and unchecking of radio button happens appropriately */
        myRecyclerViewHolder.mRadioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b) {
                    if (lastSelectedPosition != -1) {
         /* Getting the reference to the previously checked radio button and then unchecking it.lastSelectedPosition has the index of the previously selected radioButton */  
                        //RadioButton rb = (RadioButton) ((MainActivity) context).linearLayoutManager.getChildAt(lastSelectedPosition).findViewById(R.id.rbRadioButton);
                        RadioButton rb = (RadioButton) ((MainActivity) myRecyclerViewHolder.mRadioButton.getContext()).linearLayoutManager.getChildAt(lastSelectedPosition).findViewById(R.id.rbRadioButton);
                        rb.setChecked(false);
                    }
                        lastSelectedPosition = i;
                        /* Checking the currently selected radio button */
                        myRecyclerViewHolder.mRadioButton.setChecked(true);
                    }
            }
        });
    }
    
    @Override
    public int getItemCount() {
        return routeDetailsFromFirestoreArrayList_.size();
    }
    } // End of Adapter Class
    

    Inside MainActivity.java we call the ctor of Adapter class like this. The context passed is of MainActivity to the Adapter ctor :

    myRecyclerViewAdapter = new MyRecyclerViewAdapter(MainActivity.this, routeDetailsList);
    

提交回复
热议问题