简体   繁体   中英

How to implement an onClick method in an Activity rather than a RecyclerView Adapter class?

I'm trying to allow a user to click on any of my RecyclerView items and for that item to then be highlighted, added to an arraylist and then for that list to be used in a Retrofit Post request.

I understand how to implement an onClick method in the Adapter class, but I cant use the list I append to, as it's initialised in my adapter class. How to I implement this functionality in the Activity class?

Detailed explanation with code example is needed. Thanks!

If I correctly understood your question, you may want to have a look at this free to use copy paste class. It sets up an OnItemClickListener inside the Activity. Use this code for setup:

RecycerView recyclerView = findViewById(R.id.recyclerView);   
ItemClickSupport.addTo(recyclerView).setOnItemClickListener(new ItemClickSupport.OnItemClickListener( { 
    @Override 
    public void onItemClicked(RecyclerView recyclerView, int position, View { 
        //Code goes here
    } 
}); 

I hope this is what you're looking for.

I have figured out a solution to my own question. The code is below.

I wanted to get the contact (data type for each list view item) that was clicked to be able to manipulate the data in the activity class and not the adapter class.

I created an Interface as below

public interface ItemClickListener {
    void onItemClick(Contact contact);
}

In my RecyclerViewAdapter class I created the following constructor and local variable:

private static ItemClickListener onItemClick;

public RecyclerViewAdapter(List<Contact> contactList, int itemLayout, Context context) {
    this.contactList = contactList;
    this.itemLayout = itemLayout;
    this.context = context;
}

Then in the onBindViewHodler method I assigned a setOnClickListener to the list items root layout (LinearLayout in this case):

@Override
public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int position) {
    final Contact contact = contactList.get(position);
    viewHolder.contactName.setText(contact.getFullName());
    viewHolder.contactImage.setBackgroundResource(R.drawable.human_photo);
    viewHolder.viewLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onItemClick.onItemClick(contactList.get(position));
        }
    });
}

Now, in the Activity class, I create some global variables and then in my onCreate method, I set up my RecyclerView:

private CreateGroupAccountStage2RVAdapter adapter;
private RecyclerView contactsRecyclerView;
private RecyclerView.LayoutManager contactsRecyclerViewLayoutManager;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_select_contacts);

    //Set up recyclerview with dummy data
    ArrayList<Contact> contactList = new ArrayList<>();
    contactList.add(new Contact("FN1", "LN1", "12345"));
    contactList.add(new Contact("FN2", "LN2", "23456"));

    contactsRecyclerView = (RecyclerView) findViewById(R.id.contactsRV);
    contactsRecyclerViewLayoutManager = new LinearLayoutManager(this);
    contactsRecyclerView.setLayoutManager(contactsRecyclerViewLayoutManager);
    adapter = new RecyclerViewAdapter(contactList, R.layout.contact_list_item, this);
    contactsRecyclerView.setAdapter(adapter);
    adapter.setOnClick(ThisActivity.this);
}

I Hope this helps anyone else that is stuck!

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