diff --git a/README-cn.md b/README-cn.md index 317f6b568..54813f537 100644 --- a/README-cn.md +++ b/README-cn.md @@ -63,7 +63,6 @@ public class QuickAdapter extends BaseQuickAdapter { } } ``` -#设置 item click Adapter ```java mRecyclerView.addOnItemTouchListener(new OnItemClickListener(mRecyclerView,mQuickAdapter){ @@ -76,6 +75,7 @@ mRecyclerView.addOnItemTouchListener(new OnItemClickListener(mRecyclerView,mQuic }); ``` +#设置 item click 新增添加子布局多个控件的点击事件 #设置 it item child click 首先需要添加需要点击触发的 childview id ``` @@ -92,7 +92,7 @@ mRecyclerView.addOnItemTouchListener(new OnItemClickListener(mRecyclerView,mQuic Glide.with(mContext).load(item.getUserAvatar()).crossFade().placeholder(R.mipmap.def_head).transform(new GlideCircleTransform(mContext)).into((ImageView) helper.getView(R.id.tweetAvatar)); } ``` -然后 +Activity ```java mRecyclerView.addOnItemTouchListener(new OnItemChildClickListener(mRecyclerView,mQuickAdapter) { @Override @@ -162,45 +162,7 @@ mRecyclerView.addOnItemTouchListener(new OnItemClickListener(mRecyclerView,mQuic }); ``` -![addOnItemTouchListener](http://7xs9qs.com1.z0.glb.clouddn.com/B001F6D5-E057-4202-9312-8953D9B60E18.png) -#如果需要给 childView 设置点击事件 -``` - helper.addOnClickListener(R.id.tweetAvatar) - .addOnClickListener(R.id.tweetName); -``` -#如何添加item点击、长按事件 -![demo](https://github.com/CymChad/BaseRecyclerViewAdapterHelper/blob/master/demo_res/chlid_click.gif) -```java -mQuickAdapter.setOnRecyclerViewItemClickListener(); -mQuickAdapter.setOnRecyclerViewItemLongClickListener(); -``` -#新增添加子布局多个控件的点击事件 -Adapter -```java - protected void convert(BaseViewHolder helper, Status item) { - helper.setOnClickListener(R.id.tweetAvatar, new OnItemChildClickListener()) - .setOnClickListener(R.id.tweetName, new OnItemChildClickListener()); - } -``` -Activity -```java -mQuickAdapter.setOnRecyclerViewItemChildClickListener(new BaseQuickAdapter.OnRecyclerViewItemChildClickListener() { - @Override - public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) { - String content = null; - Status status = (Status) adapter.getItem(position); - switch (view.getId()) { - case R.id.tweetAvatar: - content = "img:" + status.getUserAvatar(); - break; - case R.id.tweetName: - content = "name:" + status.getUserName(); - break; - } - Toast.makeText(AnimationUseActivity.this, content, Toast.LENGTH_LONG).show(); - } - }); -``` + #如何使用它添加动画? ![demo](https://github.com/CymChad/BaseRecyclerViewAdapterHelper/blob/master/demo_res/animation.gif) ```java diff --git a/README.md b/README.md index bc59fb051..73844699a 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ # BaseRecyclerViewAdapterHelper([中文版文档](https://github.com/CymChad/BaseRecyclerViewAdapterHelper/blob/master/README-cn.md)) ![Paste_Image.png](http://upload-images.jianshu.io/upload_images/972352-1d77e0a75a4a7c0a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) Powerful and flexible RecyclerAdapter -Please feel free to use this.(Love can be a **Star**) +Please feel free to use this.(Welcome to **Star** and **Fork**) ## Google Play Demo [![Get it on Google Play](https://developer.android.com/images/brand/en_generic_rgb_wo_60.png)](https://play.google.com/store/apps/details?id=com.chad.baserecyclerviewadapterhelper) @@ -73,7 +73,7 @@ mRecyclerView.addOnItemTouchListener(new OnItemClickListener(mRecyclerView,mQuic ``` #Use it item child click -first you should add child view id +first you should register child view id ``` @Override protected void convert(BaseViewHolder helper, Status item) { @@ -110,7 +110,7 @@ and then }); ``` #use it item child long click -first you should add child view id +Adapter ``` @Override protected void convert(BaseViewHolder helper, Status item) { @@ -125,7 +125,7 @@ first you should add child view id Glide.with(mContext).load(item.getUserAvatar()).crossFade().placeholder(R.mipmap.def_head).transform(new GlideCircleTransform(mContext)).into((ImageView) helper.getView(R.id.tweetAvatar)); } ``` -and then +Activity ```java mRecyclerView.addOnItemTouchListener(new OnItemChildLongClickListener(mRecyclerView,mQuickAdapter) { @Override @@ -135,6 +135,7 @@ and then }); ``` #if you wish to implement various forms of click +Activity ```java mRecyclerView.addOnItemTouchListener(new SimpleClickListener(mRecyclerView, mQuickAdapter) { @Override @@ -159,33 +160,7 @@ and then }); ``` -#Use it item chlid click -Adapter -```java -protected void convert(BaseViewHolder helper, Status item) { - helper.setOnClickListener(R.id.tweetAvatar, new OnItemChildClickListener()) - .setOnClickListener(R.id.tweetName, new OnItemChildClickListener()); -} -``` -Activity -```java -mQuickAdapter.setOnRecyclerViewItemChildClickListener(new BaseQuickAdapter.OnRecyclerViewItemChildClickListener() { - @Override - public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) { - String content = null; - Status status = (Status) adapter.getItem(position); - switch (view.getId()) { - case R.id.tweetAvatar: - content = "img:" + status.getUserAvatar(); - break; - case R.id.tweetName: - content = "name:" + status.getUserName(); - break; - } - Toast.makeText(AnimationUseActivity.this, content, Toast.LENGTH_LONG).show(); - } - }); -``` + #Use it add adaptar Animation ![demo](https://github.com/CymChad/BaseRecyclerViewAdapterHelper/blob/master/demo_res/animation.gif) diff --git a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/RecyclerClickItemActivity.java b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/RecyclerClickItemActivity.java index 96d05b981..f70ea55de 100644 --- a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/RecyclerClickItemActivity.java +++ b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/RecyclerClickItemActivity.java @@ -21,7 +21,7 @@ public class RecyclerClickItemActivity extends Activity { private RecyclerView mRecyclerView; private QuickClickAdapter mQuickAdapter; - private static final int PAGE_SIZE = 100; + private static final int PAGE_SIZE = 10; private static String TAG = "RecyclerClickItemActivity"; @Override @@ -31,8 +31,8 @@ protected void onCreate(Bundle savedInstanceState) { mRecyclerView = (RecyclerView) findViewById(R.id.rv_list); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); initAdapter(); - mQuickAdapter.addHeaderView(getView()); - mQuickAdapter.addFooterView(getView()); + mQuickAdapter.addHeaderView(getHeadView()); + mQuickAdapter.addFooterView(getFootView()); mRecyclerView.setAdapter(mQuickAdapter); /** * Item clcik @@ -50,10 +50,10 @@ public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) super.onItemChildClick(adapter, view, position); switch (view.getId()) { case R.id.tweetAvatar: - Toast.makeText(RecyclerClickItemActivity.this, "R.id.tweetAvatar click" + Integer.toString(position), Toast.LENGTH_SHORT).show(); + Toast.makeText(RecyclerClickItemActivity.this, "The " + Integer.toString(position)+" tweetAvatar is clicked", Toast.LENGTH_SHORT).show(); break; case R.id.tweetName: - Toast.makeText(RecyclerClickItemActivity.this, "R.id.tweetName click" + Integer.toString(position), Toast.LENGTH_SHORT).show(); + Toast.makeText(RecyclerClickItemActivity.this, "The " + Integer.toString(position)+" tweetName is clicked", Toast.LENGTH_SHORT).show(); break; default: break; @@ -64,14 +64,14 @@ public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) @Override public void onItemLongClick(BaseQuickAdapter adapter, View view, int position) { super.onItemLongClick(adapter, view, position); - Toast.makeText(RecyclerClickItemActivity.this, "ItemLongClick" + Integer.toString(position), Toast.LENGTH_SHORT).show(); + Toast.makeText(RecyclerClickItemActivity.this,"The " + Integer.toString(position)+ " Item is LongClick ", Toast.LENGTH_SHORT).show(); } @Override public void onItemChildLongClick(BaseQuickAdapter adapter, View view, int position) { super.onItemChildLongClick(adapter, view, position); - Toast.makeText(RecyclerClickItemActivity.this, view.getId() + " getLongClick" + Integer.toString(position), Toast.LENGTH_SHORT).show(); + Toast.makeText(RecyclerClickItemActivity.this, "The "+ Integer.toString(position)+" view itemchild " + "is LongClick " + Integer.toString(position), Toast.LENGTH_SHORT).show(); } }); @@ -122,19 +122,30 @@ public void onItemChildLongClick(BaseQuickAdapter adapter, View view, int positi });*/ } - private View getView() { + private View getHeadView() { View view = getLayoutInflater().inflate(R.layout.head_view, null); view.findViewById(R.id.tv).setVisibility(View.GONE); view.setLayoutParams(new DrawerLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - Toast.makeText(RecyclerClickItemActivity.this, "click View", Toast.LENGTH_LONG).show(); + Toast.makeText(RecyclerClickItemActivity.this, "click headView", Toast.LENGTH_LONG).show(); + } + }); + return view; + } + private View getFootView() { + View view = getLayoutInflater().inflate(R.layout.head_view, null); + view.findViewById(R.id.tv).setVisibility(View.GONE); + view.setLayoutParams(new DrawerLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); + view.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Toast.makeText(RecyclerClickItemActivity.this, "click FootView", Toast.LENGTH_LONG).show(); } }); return view; } - private void initAdapter() { mQuickAdapter = new QuickClickAdapter(PAGE_SIZE); mQuickAdapter.openLoadAnimation(); diff --git a/library/src/main/java/com/chad/library/adapter/base/listener/SimpleClickListener.java b/library/src/main/java/com/chad/library/adapter/base/listener/SimpleClickListener.java index 5cdc5fbcf..ee34f835e 100644 --- a/library/src/main/java/com/chad/library/adapter/base/listener/SimpleClickListener.java +++ b/library/src/main/java/com/chad/library/adapter/base/listener/SimpleClickListener.java @@ -103,18 +103,21 @@ public void onLongPress(MotionEvent e) { View child = recyclerView.findChildViewUnder(e.getX(), e.getY()); if (child != null) { BaseViewHolder vh = (BaseViewHolder) recyclerView.getChildViewHolder(child); - - longClickViewIds =vh.getItemChildLongClickViewIds(); - if (longClickViewIds!=null&&longClickViewIds.size()>0){ - for (Iterator it = longClickViewIds.iterator(); it.hasNext(); ) { - View childView = child.findViewById((Integer) it.next()); - if (inRangeOfView(childView, e)) { - onItemChildLongClick(baseQuickAdapter,childView, vh.getLayoutPosition() - baseQuickAdapter.getHeaderLayoutCount()); - return ; + int type= baseQuickAdapter.getItemViewType(vh.getLayoutPosition()); + if (type != EMPTY_VIEW && type != HEADER_VIEW && type != FOOTER_VIEW && type != LOADING_VIEW){ + longClickViewIds =vh.getItemChildLongClickViewIds(); + if (longClickViewIds!=null&&longClickViewIds.size()>0){ + for (Iterator it = longClickViewIds.iterator(); it.hasNext(); ) { + View childView = child.findViewById((Integer) it.next()); + if (inRangeOfView(childView, e)) { + onItemChildLongClick(baseQuickAdapter,childView, vh.getLayoutPosition() - baseQuickAdapter.getHeaderLayoutCount()); + return ; + } } } + onItemLongClick(baseQuickAdapter,child, vh.getLayoutPosition() - baseQuickAdapter.getHeaderLayoutCount()); } - onItemLongClick(baseQuickAdapter,child, vh.getLayoutPosition() - baseQuickAdapter.getHeaderLayoutCount()); + } }