2

Referring to Jacob Tabok's post here, I added an OnItemTouchListener to my Fragment's onCreateView here:

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_recyclerview, container, false);
    mRecyclerView = (RecyclerView) view.findViewById(R.id.list_recylclerview);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(_context));
    mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    mRecyclerView.addOnItemTouchListener(
            new RecyclerItemClickListener(_context, new RecyclerItemClickListener.OnItemClickListener() {
                @Override public void onItemClick(View view, int position) {
                    logger.i("onitemClick");
                }

            })
    );

I have absolutely no idea how to determine which button within my cardview was clicked using the suggested practice. The problem is the whole cardview is returned instead of the item I click as shown in here.

How can I determine which button was clicked? I'd like to avoid the viewholder suggestion in this post as I'm am doing lots of fragment transactions

Community
  • 1
  • 1
ViciDroid
  • 3,355
  • 4
  • 12
  • 14

1 Answers1

0

You have to implement Activity - Fragment - RecyclerView.Adapter communication. (fragment communication over interface pattern)

You should use one interface for fragment-recyclerview.Adapter communication and another for activity-fragment communication.

There is a good example(github) how to implement recyclerview but without fragments

antoniolg - RecyclerViewExtensions


There are my screenshots of code with blue background comments (todo) so you can follow code flow, I use LongClick instead of Click and my ViewModel class is Friend/User, I have two disjunctional listviews so when somebody clicks on user it will migrate to friends list and vice versa.

RecylclerAdapter


Fragment


Activity


Please correct me if something is wrong with my explanation beacuse I am learning Android too.

fpopic
  • 1,673
  • 3
  • 25
  • 37
  • I ended up doing this through the Adapter since it provides a getPosition method within the viewholder class. I am sure an interface solution would work fine but it is much more code to write. – ViciDroid Nov 28 '14 at 15:24
  • Yes, it is, but after you learn the concept of interface communication between objects you will be most of the time use copy-paste of old code and change things what you need for particular project. – fpopic Nov 28 '14 at 16:11
  • 1
    Completely agree, interface communication between objects (especially fragments) is extremely useful. Thanks for answering! – ViciDroid Nov 28 '14 at 17:54
  • Try with Square Otto (event-bus) my example: I have two fragments with recyclerviews and when I press on first list item, the item will move to second fragment's recyclerview ( you should register and unregister bus everywhere where you are working with bus) http://www.deviantpics.com/images/2015/01/15/otto.png – fpopic Jan 15 '15 at 08:48