diff --git a/app/src/main/java/com/chaychan/multiitemrvadapter/activity/MainActivity.java b/app/src/main/java/com/chaychan/multiitemrvadapter/activity/MainActivity.java index 9e385e6..d222234 100644 --- a/app/src/main/java/com/chaychan/multiitemrvadapter/activity/MainActivity.java +++ b/app/src/main/java/com/chaychan/multiitemrvadapter/activity/MainActivity.java @@ -5,11 +5,11 @@ import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; +import com.chaychan.multiitemrvadapter.R; +import com.chaychan.multiitemrvadapter.adapter.MessageListAdapter; import com.chaychan.multiitemrvadapter.model.ImageMessage; import com.chaychan.multiitemrvadapter.model.Message; -import com.chaychan.multiitemrvadapter.R; import com.chaychan.multiitemrvadapter.model.TextMessage; -import com.chaychan.multiitemrvadapter.adapter.MessageListAdapter; import java.util.ArrayList; import java.util.List; @@ -37,6 +37,7 @@ protected void onCreate(Bundle savedInstanceState) { mMessages.add(new TextMessage("嗯嗯,今天天气真不错!")); mMessages.add(new ImageMessage(imgUrl2)); - mRvMessages.setAdapter(new MessageListAdapter(mMessages)); + MessageListAdapter adapter = new MessageListAdapter(mMessages); + mRvMessages.setAdapter(adapter); } } diff --git a/library/src/main/java/com/chaychan/adapter/MultipleItemRvAdapter.java b/library/src/main/java/com/chaychan/adapter/MultipleItemRvAdapter.java index 50f8295..0f96775 100644 --- a/library/src/main/java/com/chaychan/adapter/MultipleItemRvAdapter.java +++ b/library/src/main/java/com/chaychan/adapter/MultipleItemRvAdapter.java @@ -71,20 +71,34 @@ protected void convert(BaseViewHolder helper, T item) { } private void bindClick(final BaseViewHolder helper, final T item, final int position, final BaseItemProvider provider) { + OnItemClickListener clickListener = getOnItemClickListener(); + OnItemLongClickListener longClickListener = getOnItemLongClickListener(); + + if (clickListener != null && longClickListener != null){ + //如果已经设置了子条目点击监听和子条目长按监听 + return; + } + View itemView = helper.itemView; - itemView.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - provider.onClick(helper, item, position); - } - }); + if (clickListener == null){ + //如果没有设置点击监听,则回调给itemProvider + itemView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + provider.onClick(helper, item, position); + } + }); + } - itemView.setOnLongClickListener(new View.OnLongClickListener() { - @Override - public boolean onLongClick(View v) { - return provider.onLongClick(helper, item, position); - } - }); + if (longClickListener == null){ + //如果没有设置长按监听,则回调给itemProvider + itemView.setOnLongClickListener(new View.OnLongClickListener() { + @Override + public boolean onLongClick(View v) { + return provider.onLongClick(helper, item, position); + } + }); + } } } diff --git a/library/src/main/java/com/chaychan/adapter/ProviderDelegate.java b/library/src/main/java/com/chaychan/adapter/ProviderDelegate.java index 8ba96b8..315d55e 100644 --- a/library/src/main/java/com/chaychan/adapter/ProviderDelegate.java +++ b/library/src/main/java/com/chaychan/adapter/ProviderDelegate.java @@ -15,7 +15,7 @@ public class ProviderDelegate { public void registerProvider(BaseItemProvider provider){ ItemProviderTag tag = provider.getClass().getAnnotation(ItemProviderTag.class); if (tag == null){ - throw new RuntimeException("ItemProviderTag not def layout"); + throw new RuntimeException("Can not find the annotation: 'ItemProviderTag'"); } int viewType = tag.viewType();