简体   繁体   中英

Where to place onClickListener

I have buttons in the rows of a RecyclerView within a Fragment within an Activity. Is it best to place the onClickListener and logic in the RecyclerView adapter or within the Fragments onActivityCreated method or the in the Activity?

I prefer MyFragment implements onClickListener

In the adapter's onBindViewHolder

button.setTag(position);
button.setOnClickListener(MyFragment.this);

In the onClick function

int position = view.getTag();

And then you can use the position variable to identify which button is clicked.

You put the listeners in the adapter, the logic somewhere else, for example the fragment.

So in your adapter, assuming Kotlin, for example:

...

var yourListener: (() -> Unit)? = null

...

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
holder.itemView.setOnClickListener { yourListener?.let { it() } } 
}

And where you listener lives, and where the adapter is set:

var addCardClicked: (() -> Unit)? = null

...

adapter.yourListener = { yourListener() }

...
private fun yourListener() {
//does all kind of cool stuff
}

This works

public class ServiceListAdapter extends RecyclerView.Adapter<ServiceListAdapter.ViewHolder> {
private final Context mContext;
private List<ServiceListModel> categoryList;
private View.OnClickListener onClickListener;

public ServiceListAdapter(Context mContext, List<ServiceListModel> categoryList, View.OnClickListener onClickListener) {
this.categoryList = categoryList;
this.mContext = mContext;
this.onClickListener = onClickListener;
}

@Override
public ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
final RowServiceListBinding binding = DataBindingUtil.inflate(inflater, R.layout.row_service_list, parent, false);
return new ViewHolder(binding.getRoot(), binding);
}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.binding.rlService.setOnClickListener(onClickListener);
holder.binding.rlService.setTag(position);
}

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

public class ViewHolder extends RecyclerView.ViewHolder {
private final RowServiceListBinding binding;

public ViewHolder(final View view, final RowServiceListBinding binding) {
    super(view);
    this.binding = binding;
}

@UiThread
public void bind(final ServiceListModel mAddressModel) {
    //this.binding.setAddress(mAddressModel);
}
}
}

Use in Activity/Fragment

ServiceListAdapter adapter = new ServiceListAdapter(context, serviceList, new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rlService:
                int pos = (int) v.getTag();
                serviceList.remove(position);
                break;
        }
    }
});

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