简体   繁体   中英

where and how to put a onclick in recyclerview to get the result in the activity that contain the fragment?

In my app, I have an activity that uses fragment(s). In one such fragment, I am using a recyclerview. I just want to add (one or more) onClick(s) whose action I can process in the fragment or in the activity as appropriate.

I searched a lot. I found many solutions, but none that seems to me to really respond to my case, which is however not so exceptional. I found a lot of solutions with the onClick in OnBindviewHolder. However, I also read very often that this solution was not acceptable because it consumed unnecessary resources. I found a solution ( https://openclassrooms.com/en/courses/4568576-recover-and-display-distant-data/4893791-interact-with-the-recyclerview ) using a itemClickSupport class that works, but I don't know how to fully exploit it. So, I'm going around in circles, and I don't know which track I should go to, without wasting my time unnecessarily realizing at the end that I didn't go in the right direction.

Edit: Another solution that seems perfect to me ( https://openclassrooms.com/fr/courses/4568596-construisez-une-interface-utilisateur-flexible-et-adaptative/4789616-creez-votre-premiere-application-avec-des-fragments ), automatically creates it in the fragment by a callback, BUT, I don't know how to get it in the parent activity.

What I really want is:

on smartphone:

  • an activity that open a fragment 1 with a recyclerview
  • another activity with another fragment (with another recyclerview) opened by a click on a button in the recyclerview from the fragment 1

on tablet (with another layout):

  • an activity that open both fragments, and the button in the fragment/recyclerview 1 populate the fragment/recyclerview 2.

I realize that I have a lot of trouble explaining what I want, and that my problem to find the solution probably comes from there.

I think I finally found a solution that can be useful:

activity:

public class MainActivity extends FragmentActivity implements MainFragment.SampleOnClickedListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void sampleOnClicked(int position) {
        Log.e("TAG", " from activity / position:" + position);
    }
}

fragment:

public class MainFragment extends Fragment implements SampleAdapter.SampleRecyclerViewHolder.SampleOnListener {
    private SampleOnClickedListener mCallback;

    public interface SampleOnClickedListener {
        void sampleOnClicked(int position);
    }

    public MainFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);
        SampleAdapter sampleAdapter = new SampleAdapter(this);
        RecyclerView recyclerView = view.findViewById(R.id.sampleRecyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));
        recyclerView.setAdapter(sampleAdapter);
        return view;
    }

    @Override
    public void sampleOnClick(int position) {
        Log.e("TAG", " from fragment / position:" + position);
        mCallback.sampleOnClicked(position);
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        this.createCallbackToParentActivity();
    }

    private void createCallbackToParentActivity() {
        try {
            mCallback = (SampleOnClickedListener) getActivity();
        } catch (ClassCastException e) {
            throw new ClassCastException(e.toString() + " must implement OnButtonClickedListener");
        }
    }
}

Adapter:

public class SampleAdapter extends RecyclerView.Adapter<SampleAdapter.SampleRecyclerViewHolder> {
    private final SampleRecyclerViewHolder.SampleOnListener mListener;

    public SampleAdapter(SampleRecyclerViewHolder.SampleOnListener listener) {
        this.mListener = listener;
    }

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

    @Override
    public void onBindViewHolder(@NonNull SampleRecyclerViewHolder holder, int position) {
        holder.itemView.setOnClickListener(v -> mListener.sampleOnClick(holder.getBindingAdapterPosition()));
    }

    @Override
    public int getItemCount() {
        // modify with real size
        return 0;
    }

    public static class SampleRecyclerViewHolder extends RecyclerView.ViewHolder {
        public SampleRecyclerViewHolder(@NonNull View itemView) {
            super(itemView);
        }

        public interface SampleOnListener {
            void sampleOnClick(int position);
        }
    }
}

If I'm not mistaken, the result of the click can be retrieved in the activity as well as in the fragment and that's what I needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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