Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClickListeners support generic types #3590

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.chad.baserecyclerviewadapterhelper.utils.Tips;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.jaredrummler.materialspinner.MaterialSpinner;
import com.kyleduo.switchbutton.SwitchButton;

Expand Down Expand Up @@ -58,11 +59,11 @@ private void initAdapter() {
int mFirstPageItemCount = 3;
// mAnimationAdapter.setNotDoAnimationCount(mFirstPageItemCount);
mAnimationAdapter.addChildClickViewIds(R.id.img, R.id.tweetName, R.id.tweetText);
mAnimationAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
mAnimationAdapter.setOnItemChildClickListener(new OnItemChildClickListener<Status, BaseViewHolder>() {
@Override
public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
public void onItemChildClick(@NonNull BaseQuickAdapter<Status, BaseViewHolder> adapter, @NonNull View view, int position) {
String content = null;
Status status = (Status) adapter.getItem(position);
Status status = adapter.getItem(position);
switch (view.getId()) {
case R.id.img:
content = "img:" + status.getUserAvatar();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import com.chad.baserecyclerviewadapterhelper.R;
import com.chad.baserecyclerviewadapterhelper.adapter.DataBindingAdapter;
import com.chad.baserecyclerviewadapterhelper.base.BaseActivity;
import com.chad.baserecyclerviewadapterhelper.databinding.ItemMovieBinding;
import com.chad.baserecyclerviewadapterhelper.entity.Movie;
import com.chad.baserecyclerviewadapterhelper.utils.Tips;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -48,9 +50,9 @@ protected void onCreate(Bundle savedInstanceState) {
adapter.addHeaderView(view);

//item 点击事件
adapter.setOnItemClickListener(new OnItemClickListener() {
adapter.setOnItemClickListener(new OnItemClickListener<Movie, BaseDataBindingHolder<ItemMovieBinding>>() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
public void onItemClick(BaseQuickAdapter<Movie, BaseDataBindingHolder<ItemMovieBinding>> adapter, View view, int position) {
Tips.show("onItemClick: " + position);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ public void onItemSwipeMoving(Canvas canvas, RecyclerView.ViewHolder viewHolder,
mRecyclerView.setAdapter(mAdapter);


mAdapter.setOnItemClickListener(new OnItemClickListener() {
mAdapter.setOnItemClickListener(new OnItemClickListener<String, BaseViewHolder>() {
@Override
public void onItemClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
public void onItemClick(@NonNull BaseQuickAdapter<String, BaseViewHolder> adapter, @NonNull View view, int position) {
Tips.show("点击了:" + position);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
import com.chad.baserecyclerviewadapterhelper.R;
import com.chad.baserecyclerviewadapterhelper.adapter.HeaderAndFooterAdapter;
import com.chad.baserecyclerviewadapterhelper.base.BaseActivity;
import com.chad.baserecyclerviewadapterhelper.entity.Status;
import com.chad.baserecyclerviewadapterhelper.utils.Tips;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

/**
* https://github.com/CymChad/BaseRecyclerViewAdapterHelper
Expand Down Expand Up @@ -100,9 +102,9 @@ private void initAdapter() {
adapter = new HeaderAndFooterAdapter(PAGE_SIZE);
adapter.setAnimationEnable(true);

adapter.setOnItemClickListener(new OnItemClickListener() {
adapter.setOnItemClickListener(new OnItemClickListener<Status, BaseViewHolder>() {
@Override
public void onItemClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
public void onItemClick(@NonNull BaseQuickAdapter<Status, BaseViewHolder> adapter, @NonNull View view, int position) {
Tips.show(String.valueOf(position));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import com.chad.baserecyclerviewadapterhelper.databinding.ActivityHomeBinding
import com.chad.baserecyclerviewadapterhelper.entity.HomeEntity
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.chad.library.adapter.base.viewholder.BaseViewHolder

class HomeActivity : AppCompatActivity(), OnItemClickListener {
class HomeActivity : AppCompatActivity(), OnItemClickListener<HomeEntity, BaseViewHolder> {

private lateinit var binding: ActivityHomeBinding

Expand All @@ -30,8 +31,8 @@ class HomeActivity : AppCompatActivity(), OnItemClickListener {
}
}

override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
val item = adapter.data[position] as HomeEntity
override fun onItemClick(adapter: BaseQuickAdapter<HomeEntity, BaseViewHolder>, view: View, position: Int) {
val item = adapter.data[position]
if (!item.isHeader) {
startActivity(Intent(this@HomeActivity, item.activity))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.chad.library.adapter.base.listener.OnItemChildLongClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.listener.OnItemLongClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -41,28 +42,28 @@ protected void onCreate(Bundle savedInstanceState) {
mRecyclerView = findViewById(R.id.rv);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
initAdapter();
adapter.setOnItemClickListener(new OnItemClickListener() {
adapter.setOnItemClickListener(new OnItemClickListener<ClickEntity, BaseViewHolder>() {
@Override
public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
public void onItemClick(@NonNull BaseQuickAdapter<ClickEntity, BaseViewHolder> adapter, @NonNull View view, int position) {
Tips.show("onItemClick " + position);
}
});
adapter.setOnItemLongClickListener(new OnItemLongClickListener() {
adapter.setOnItemLongClickListener(new OnItemLongClickListener<ClickEntity, BaseViewHolder>() {
@Override
public boolean onItemLongClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
public boolean onItemLongClick(@NonNull BaseQuickAdapter<ClickEntity, BaseViewHolder> adapter, @NonNull View view, int position) {
Tips.show("onItemLongClick " + position);
return true;
}
});
adapter.setOnItemChildClickListener(new OnItemChildClickListener() {
adapter.setOnItemChildClickListener(new OnItemChildClickListener<ClickEntity, BaseViewHolder>() {
@Override
public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
public void onItemChildClick(@NonNull BaseQuickAdapter<ClickEntity, BaseViewHolder> adapter, @NonNull View view, int position) {
Tips.show("onItemChildClick " + position);
}
});
adapter.setOnItemChildLongClickListener(new OnItemChildLongClickListener() {
adapter.setOnItemChildLongClickListener(new OnItemChildLongClickListener<ClickEntity, BaseViewHolder>() {
@Override
public boolean onItemChildLongClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
public boolean onItemChildLongClick(@NonNull BaseQuickAdapter<ClickEntity, BaseViewHolder> adapter, @NonNull View view, int position) {
Tips.show("onItemChildLongClick " + position);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import java.util.List;

Expand All @@ -43,9 +44,9 @@ protected void onCreate(Bundle savedInstanceState) {
mData = DataServer.getSectionData();
SectionQuickAdapter adapter = new SectionQuickAdapter(R.layout.item_section_content, R.layout.def_section_head, mData);

adapter.setOnItemClickListener(new OnItemClickListener() {
adapter.setOnItemClickListener(new OnItemClickListener<MySection,BaseViewHolder>() {
@Override
public void onItemClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
public void onItemClick(@NonNull BaseQuickAdapter<MySection,BaseViewHolder> adapter, @NonNull View view, int position) {
MySection mySection = mData.get(position);
if (mySection.isHeader()) {
Tips.show((String) mySection.getObject());
Expand All @@ -55,9 +56,9 @@ public void onItemClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, i
}
}
});
adapter.setOnItemChildClickListener(new OnItemChildClickListener() {
adapter.setOnItemChildClickListener(new OnItemChildClickListener<MySection,BaseViewHolder>() {
@Override
public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
public void onItemChildClick(@NonNull BaseQuickAdapter<MySection,BaseViewHolder> adapter, @NonNull View view, int position) {
Tips.show("onItemChildClick: " + position);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import com.chad.baserecyclerviewadapterhelper.R;
import com.chad.baserecyclerviewadapterhelper.entity.ClickEntity;
import com.chad.baserecyclerviewadapterhelper.entity.Status;
import com.chad.baserecyclerviewadapterhelper.utils.Tips;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
Expand All @@ -20,7 +21,7 @@
/**
*
*/
public class ItemClickAdapter extends BaseMultiItemQuickAdapter<ClickEntity, BaseViewHolder> implements OnItemClickListener, OnItemChildClickListener {
public class ItemClickAdapter extends BaseMultiItemQuickAdapter<ClickEntity, BaseViewHolder> implements OnItemClickListener<Status, BaseViewHolder>, OnItemChildClickListener<Status, BaseViewHolder> {

public ItemClickAdapter(List<ClickEntity> data) {
super(data);
Expand Down Expand Up @@ -73,12 +74,12 @@ protected void convert(@NonNull final BaseViewHolder helper, final ClickEntity i
}

@Override
public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
public void onItemChildClick(BaseQuickAdapter<Status, BaseViewHolder> adapter, View view, int position) {
Tips.show("childView click");
}

@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
public void onItemClick(BaseQuickAdapter<Status, BaseViewHolder> adapter, View view, int position) {
Tips.show("嵌套RecycleView item 收到: " + "点击了第 " + position + " 一次");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ abstract class BaseQuickAdapter<T, VH : BaseViewHolder>
private var mLastPosition = -1

private var mSpanSizeLookup: GridSpanSizeLookup? = null
private var mOnItemClickListener: OnItemClickListener? = null
private var mOnItemLongClickListener: OnItemLongClickListener? = null
private var mOnItemChildClickListener: OnItemChildClickListener? = null
private var mOnItemChildLongClickListener: OnItemChildLongClickListener? = null
private var mOnItemClickListener: OnItemClickListener<T,VH>? = null
private var mOnItemLongClickListener: OnItemLongClickListener<T, VH>? = null
private var mOnItemChildClickListener: OnItemChildClickListener<T,VH>? = null
private var mOnItemChildLongClickListener: OnItemChildLongClickListener<T,VH>? = null
private var mUpFetchModule: BaseUpFetchModule? = null
private var mDraggableModule: BaseDraggableModule? = null
internal var mLoadMoreModule: BaseLoadMoreModule? = null
Expand Down Expand Up @@ -1335,27 +1335,27 @@ abstract class BaseQuickAdapter<T, VH : BaseViewHolder>
this.mSpanSizeLookup = spanSizeLookup
}

fun setOnItemClickListener(listener: OnItemClickListener?) {
fun setOnItemClickListener(listener: OnItemClickListener<T,VH>?) {
this.mOnItemClickListener = listener
}

fun setOnItemLongClickListener(listener: OnItemLongClickListener?) {
fun setOnItemLongClickListener(listener: OnItemLongClickListener<T,VH>?) {
this.mOnItemLongClickListener = listener
}

fun setOnItemChildClickListener(listener: OnItemChildClickListener?) {
fun setOnItemChildClickListener(listener: OnItemChildClickListener<T,VH>?) {
this.mOnItemChildClickListener = listener
}

fun setOnItemChildLongClickListener(listener: OnItemChildLongClickListener?) {
fun setOnItemChildLongClickListener(listener: OnItemChildLongClickListener<T,VH>?) {
this.mOnItemChildLongClickListener = listener
}

fun getOnItemClickListener(): OnItemClickListener? = mOnItemClickListener
fun getOnItemClickListener(): OnItemClickListener<T,VH>? = mOnItemClickListener

fun getOnItemLongClickListener(): OnItemLongClickListener? = mOnItemLongClickListener
fun getOnItemLongClickListener(): OnItemLongClickListener<T,VH>? = mOnItemLongClickListener

fun getOnItemChildClickListener(): OnItemChildClickListener? = mOnItemChildClickListener
fun getOnItemChildClickListener(): OnItemChildClickListener<T,VH>? = mOnItemChildClickListener

fun getOnItemChildLongClickListener(): OnItemChildLongClickListener? = mOnItemChildLongClickListener
fun getOnItemChildLongClickListener(): OnItemChildLongClickListener<T,VH>? = mOnItemChildLongClickListener
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
package com.chad.library.adapter.base.listener;


import android.view.View;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

/**
* @author: limuyang
* @date: 2019-12-03
* @Description:
*/
public interface OnItemChildClickListener {
public interface OnItemChildClickListener<T, VH extends BaseViewHolder> {
/**
* callback method to be invoked when an item child in this view has been click
*
* @param adapter BaseQuickAdapter
* @param view The view whihin the ItemView that was clicked
* @param position The position of the view int the adapter
*/
void onItemChildClick(@NonNull BaseQuickAdapter<?,?> adapter, @NonNull View view, int position);
void onItemChildClick(@NonNull BaseQuickAdapter<T, VH> adapter, @NonNull View view, int position);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

/**
* @author: limuyang
* @date: 2019-12-03
* @Description:
*/
public interface OnItemChildLongClickListener {
public interface OnItemChildLongClickListener<T,VH extends BaseViewHolder> {
/**
* callback method to be invoked when an item in this view has been
* click and held
Expand All @@ -21,5 +22,5 @@ public interface OnItemChildLongClickListener {
* @param position The position of the view int the adapter
* @return true if the callback consumed the long click ,false otherwise
*/
boolean onItemChildLongClick(@NonNull BaseQuickAdapter<?,?> adapter, @NonNull View view, int position);
boolean onItemChildLongClick(@NonNull BaseQuickAdapter<T,VH> adapter, @NonNull View view, int position);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

/**
* @author: limuyang
* @date: 2019-12-03
* @Description: Interface definition for a callback to be invoked when an item in this
* RecyclerView itemView has been clicked.
*/
public interface OnItemClickListener {
public interface OnItemClickListener<T, VH extends BaseViewHolder> {
/**
* Callback method to be invoked when an item in this RecyclerView has
* been clicked.
Expand All @@ -22,5 +23,5 @@ public interface OnItemClickListener {
* will be a view provided by the adapter)
* @param position The position of the view in the adapter.
*/
void onItemClick(@NonNull BaseQuickAdapter<?,?> adapter, @NonNull View view, int position);
void onItemClick(@NonNull BaseQuickAdapter<T, VH> adapter, @NonNull View view, int position);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

/**
* @author: limuyang
* @date: 2019-12-03
* @Description:
*/
public interface OnItemLongClickListener {
public interface OnItemLongClickListener<T, VH extends BaseViewHolder> {
/**
* callback method to be invoked when an item in this view has been
* click and held
Expand All @@ -21,5 +22,5 @@ public interface OnItemLongClickListener {
* @param position The position of the view int the adapter
* @return true if the callback consumed the long click ,false otherwise
*/
boolean onItemLongClick(@NonNull BaseQuickAdapter<?,?> adapter, @NonNull View view, int position);
boolean onItemLongClick(@NonNull BaseQuickAdapter<T, VH> adapter, @NonNull View view, int position);
}