diff --git a/demo/src/main/java/estar/leftsectionrecyclerview/TestActivity.java b/demo/src/main/java/estar/leftsectionrecyclerview/TestActivity.java index 11d4c7c..45dd66c 100644 --- a/demo/src/main/java/estar/leftsectionrecyclerview/TestActivity.java +++ b/demo/src/main/java/estar/leftsectionrecyclerview/TestActivity.java @@ -4,10 +4,14 @@ import android.os.Bundle; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.RecyclerView; +import android.util.Log; import android.view.LayoutInflater; +import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; +import android.widget.AdapterView; import android.widget.ImageView; +import android.widget.Toast; import com.bumptech.glide.Glide; import com.bumptech.glide.request.RequestOptions; @@ -64,7 +68,8 @@ protected void onCreate(Bundle savedInstanceState) { int width = getResources().getDisplayMetrics().widthPixels; final int itemWidth = (int) (width - getResources().getDimension(R.dimen.dp_50)); - sectionRecyclerView.setAdapter(new BaseSectionAdapter(R.layout.item_image, list) { + BaseSectionAdapter adapter = null; + sectionRecyclerView.setAdapter(adapter = new BaseSectionAdapter(R.layout.item_image, list) { @Override @@ -95,5 +100,11 @@ protected void convert(BaseViewHolder helper, ImageInfo item) { }); + adapter.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + Log.i("test","positon="+position); + } + }); } } diff --git a/leftsection/src/main/java/estar/leftsection/BaseSectionAdapter.java b/leftsection/src/main/java/estar/leftsection/BaseSectionAdapter.java index 223cf8e..581d3e7 100644 --- a/leftsection/src/main/java/estar/leftsection/BaseSectionAdapter.java +++ b/leftsection/src/main/java/estar/leftsection/BaseSectionAdapter.java @@ -53,6 +53,7 @@ protected K createBaseViewHolder(View view) { } else { k = createGenericKInstance(z, view); } + return k != null ? k : (K) new BaseViewHolder(view); } @@ -116,6 +117,7 @@ public K onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(itemlayout,null); K k = createBaseViewHolder(view); k.setAdapter(this); + bindViewClickListener(k); return k; } @@ -136,19 +138,45 @@ public SupperSection getItem(int position){ } - public AdapterView.OnItemClickListener getmOnItemClickListener() { + public AdapterView.OnItemClickListener getOnItemClickListener() { return mOnItemClickListener; } - public void setmOnItemClickListener(AdapterView.OnItemClickListener mOnItemClickListener) { + public void setOnItemClickListener(AdapterView.OnItemClickListener mOnItemClickListener) { this.mOnItemClickListener = mOnItemClickListener; } - public AdapterView.OnItemLongClickListener getmOnItemLongClickListener() { + public AdapterView.OnItemLongClickListener getOnItemLongClickListener() { return mOnItemLongClickListener; } - public void setmOnItemLongClickListener(AdapterView.OnItemLongClickListener mOnItemLongClickListener) { + public void setOnItemLongClickListener(AdapterView.OnItemLongClickListener mOnItemLongClickListener) { this.mOnItemLongClickListener = mOnItemLongClickListener; } + + private void bindViewClickListener(final BaseViewHolder baseViewHolder) { + if (baseViewHolder == null) { + return; + } + final View view = baseViewHolder.itemView; + if (view == null) { + return; + } + if (getOnItemClickListener() != null) { + view.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + getOnItemClickListener().onItemClick(null, v, baseViewHolder.getLayoutPosition(),v.getId()); + } + }); + } + if (getOnItemLongClickListener() != null) { + view.setOnLongClickListener(new View.OnLongClickListener() { + @Override + public boolean onLongClick(View v) { + return getOnItemLongClickListener().onItemLongClick(null, v, baseViewHolder.getLayoutPosition(),v.getId()); + } + }); + } + } }