简体   繁体   English

通过片段中的单选按钮在回收站视图中显示所选项目

[英]Show selected item in recycler view by radiobuttom in fragment

I have this recycler view adapter where the view has a radiobutton for each item.我有这个回收器视图适配器,其中视图对每个项目都有一个单选按钮。 I want to get the item selected in my fragment.我想在我的片段中选择项目。 I can't figure out hoe to do that.我想不出锄头来做到这一点。 Can someone help me out?有人可以帮我吗?

This is my adapter class:这是我的适配器 class:

public class croptypeadapter extends RecyclerView.Adapter<croptypeadapter.ViewHolder> {
    private List<croptype_list> croptypes;
    private Context context;
    Integer mSelectedItem = 0;

    public croptypeadapter(List<croptype_list> croptypes, Context context) {
        this.croptypes = croptypes;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.vertical_menu,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        croptype_list ac=croptypes.get(position);
        Picasso.get().load(ac.getCropurl()).fit().into(holder.img);
        holder.txtview.setText(ac.getCropname());
        holder.radioButton.setChecked(position == mSelectedItem);
    }

    @Override
    public int getItemCount() {
        return croptypes.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        private ImageView img;
        TextView txtview;
        RadioButton radioButton;

        public ViewHolder(View itemView) {
            super(itemView);
            img= itemView.findViewById(R.id.imageview);
            txtview = itemView.findViewById(R.id.txtview);
            radioButton= itemView.findViewById(R.id.radio);

            View.OnClickListener l = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mSelectedItem = getAdapterPosition();
                    String data = croptypes.get(mSelectedItem).getCropname();
                    notifyItemRangeChanged(0, croptypes.size());
                }
            };
            itemView.setOnClickListener(l);
            radioButton.setOnClickListener(l);
        }
    }
}

And this is my fragment which has the recyclerview.这是我的片段,它有recyclerview。 I want to show the item selected in the recycler view in the Textview crop.我想在 Textview 裁剪中显示在回收站视图中选择的项目。

public class desc extends Fragment{

    private View view;
    RecyclerView vertical_recycler_view;
    private croptypeadapter croptypeadapter;
    private List<croptype_list> cropdata;
    ToggleButton toggleButton2;
    TextView crop;

    public static ApiInterface apiInterface;

    public desc() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_zones, container, false);
        crop = view.findViewById(R.id.crop);

        vertical_recycler_view=view.findViewById(R.id.vertical_recycler_view);
        vertical_recycler_view.setLayoutManager(new LinearLayoutManager(getActivity()));

        apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
        Call<List<croptype_list>>call=apiInterface.getcroptypes();
        call.enqueue(new Callback<List<croptype_list>>() {

            @Override
            public void onResponse(Call<List<croptype_list>> call, Response<List<croptype_list>> response) {

                cropdata=response.body();
                croptypeadapter=new croptypeadapter(cropdata,getActivity());
                vertical_recycler_view.setAdapter(croptypeadapter); }

            @Override
            public void onFailure(Call<List<croptype_list>> call, Throwable t) {

            }
        });

        toggleButton2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

               if(isChecked){
                    toggleButton2.setBackgroundResource(R.drawable.uparrow);
                    vertical_recycler_view.setVisibility(View.VISIBLE);
                }else{
                    toggleButton2.setBackgroundResource(R.drawable.downarrow);
                    vertical_recycler_view.setVisibility(View.GONE);
                }
            }
        });
}

Set an onCheckedChanged Listener to the radio button and make the txtView in your class static so that you can access it in your adapterclass将 onCheckedChanged 侦听器设置为单选按钮并在 class static 中创建 txtView 以便您可以在适配器类中访问它

static TextView crop

In you adapter class in the onBindViewholder在您的 onBindViewholder 适配器 class

holder.radiobutton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked)
          this.mSelectedItem = position;
           desc.crop.setText("yourtext");
         }
    });

The onCheckedChanged Listener will updated the value of mSelectedItem so that even when the Viewholder is recycled the value will stay selected for the selected item only. onCheckedChanged 侦听器将更新 mSelectedItem 的值,这样即使 Viewholder 被回收,该值也将仅为选定项目保持选中状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM