From ac784094b9d5f675a50bd7b59fe23cf26413ba27 Mon Sep 17 00:00:00 2001 From: chaychan <844738237@qq.com> Date: Sat, 22 Jul 2017 15:51:45 +0800 Subject: [PATCH] =?UTF-8?q?1.=E4=BF=AE=E5=A4=8D=E4=BD=BF=E7=94=A8=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E5=BA=95=E9=83=A8=E7=8A=B6=E6=80=81=E4=B8=8B=EF=BC=8C?= =?UTF-8?q?=E6=9D=A1=E7=9B=AE=E6=8F=92=E5=85=A5=E9=A1=BA=E5=BA=8F=E6=B7=B7?= =?UTF-8?q?=E4=B9=B1=E7=9A=84Bug;2.=E5=A2=9E=E5=8A=A0=E5=AD=90=E6=9D=A1?= =?UTF-8?q?=E7=9B=AE=E7=82=B9=E5=87=BB=E4=BA=8B=E4=BB=B6=E7=9A=84=E7=9B=91?= =?UTF-8?q?=E5=90=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 9 +++++ .../EllCustomBottomDemoActivity.java | 11 +++++-- .../EllDefaultBottomDemoActivity.java | 8 +++++ .../layout/page_ell_custom_bottom_demo.xml | 2 +- .../library/ExpandableLinearLayout.java | 33 +++++++++++++++---- 5 files changed, 54 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index c45deec..5f34e2d 100644 --- a/README.md +++ b/README.md @@ -114,6 +114,15 @@ ![](./intro_img/ell_4.gif) +#### 5.设置条目点击事件 + + ellProduct.setOnItemClickListener(new ExpandableLinearLayout.OnItemClickListener() { + @Override + public void onItemClick(View view, int position) { + Toast.makeText(EllCustomBottomDemoActivity.this,names[position] , Toast.LENGTH_SHORT).show(); + } + }); + ### 二、使用自定义底部 布局文件中,ExpandableLinearLayout配置useDefaultBottom="false",声明不使用默认底部。自己定义底部的布局。 diff --git a/demo/src/main/java/com/chaychan/expandablelinearlayout/EllCustomBottomDemoActivity.java b/demo/src/main/java/com/chaychan/expandablelinearlayout/EllCustomBottomDemoActivity.java index 26c3052..a443133 100644 --- a/demo/src/main/java/com/chaychan/expandablelinearlayout/EllCustomBottomDemoActivity.java +++ b/demo/src/main/java/com/chaychan/expandablelinearlayout/EllCustomBottomDemoActivity.java @@ -6,6 +6,7 @@ import android.view.View; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; +import android.widget.Toast; import com.bumptech.glide.Glide; import com.chaychan.expandablelinearlayout.bean.ProductBean; @@ -62,7 +63,7 @@ public class EllCustomBottomDemoActivity extends AppCompatActivity { ellProduct.removeAllViews();//清除所有的子View(避免重新刷新数据时重复添加) //添加数据 - for (int i = 0; i < 5; i++) { + for (int i = 0; i < 4; i++) { View view = View.inflate(this, R.layout.item_product, null); ProductBean productBean = new ProductBean(imgUrls[i], names[i], intros[i], "12.00"); ViewHolder viewHolder = new ViewHolder(view, productBean); @@ -90,6 +91,13 @@ public class EllCustomBottomDemoActivity extends AppCompatActivity { ellProduct.toggle(); } }); + + ellProduct.setOnItemClickListener(new ExpandableLinearLayout.OnItemClickListener() { + @Override + public void onItemClick(View view, int position) { + Toast.makeText(EllCustomBottomDemoActivity.this,names[position] , Toast.LENGTH_SHORT).show(); + } + }); } // 箭头的动画 @@ -103,7 +111,6 @@ public class EllCustomBottomDemoActivity extends AppCompatActivity { } } - class ViewHolder { @Bind(R.id.iv_img) ImageView ivImg; diff --git a/demo/src/main/java/com/chaychan/expandablelinearlayout/EllDefaultBottomDemoActivity.java b/demo/src/main/java/com/chaychan/expandablelinearlayout/EllDefaultBottomDemoActivity.java index 9105747..42fd3bd 100644 --- a/demo/src/main/java/com/chaychan/expandablelinearlayout/EllDefaultBottomDemoActivity.java +++ b/demo/src/main/java/com/chaychan/expandablelinearlayout/EllDefaultBottomDemoActivity.java @@ -5,6 +5,7 @@ import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.ImageView; import android.widget.TextView; +import android.widget.Toast; import com.bumptech.glide.Glide; import com.chaychan.expandablelinearlayout.bean.ProductBean; @@ -60,6 +61,13 @@ public class EllDefaultBottomDemoActivity extends AppCompatActivity { viewHolder.refreshUI(); ellProduct.addItem(view);//添加子条目 } + + ellProduct.setOnItemClickListener(new ExpandableLinearLayout.OnItemClickListener() { + @Override + public void onItemClick(View view, int position) { + Toast.makeText(EllDefaultBottomDemoActivity.this,names[position] , Toast.LENGTH_SHORT).show(); + } + }); } class ViewHolder { diff --git a/demo/src/main/res/layout/page_ell_custom_bottom_demo.xml b/demo/src/main/res/layout/page_ell_custom_bottom_demo.xml index 332ed52..979529a 100644 --- a/demo/src/main/res/layout/page_ell_custom_bottom_demo.xml +++ b/demo/src/main/res/layout/page_ell_custom_bottom_demo.xml @@ -19,7 +19,7 @@ android:layout_height="wrap_content" android:layout_marginTop="10dp" android:orientation="vertical" - app:defaultItemCount="2" + app:defaultItemCount="3" app:useDefaultBottom="false" > diff --git a/library/src/main/java/com/chaychan/library/ExpandableLinearLayout.java b/library/src/main/java/com/chaychan/library/ExpandableLinearLayout.java index a12a44c..996df0e 100644 --- a/library/src/main/java/com/chaychan/library/ExpandableLinearLayout.java +++ b/library/src/main/java/com/chaychan/library/ExpandableLinearLayout.java @@ -34,6 +34,7 @@ public class ExpandableLinearLayout extends LinearLayout implements View.OnClick private float fontSize; private int textColor; private int arrowResId; + private int mPosition; public ExpandableLinearLayout(Context context) { this(context, null); @@ -85,10 +86,10 @@ public class ExpandableLinearLayout extends LinearLayout implements View.OnClick public void addItem(View view) { - int childCount = getChildCount(); if (!useDefaultBottom){ //如果不使用默认底部 addView(view); + int childCount = getChildCount(); if (childCount > defaultItemCount){ hide(); } @@ -100,7 +101,8 @@ public class ExpandableLinearLayout extends LinearLayout implements View.OnClick //如果还没有底部 addView(view); } else { - addView(view, childCount - 2);//插在底部之前 + int childCount = getChildCount(); + addView(view, childCount - 1);//插在底部之前 } refreshUI(view); } @@ -205,13 +207,13 @@ public class ExpandableLinearLayout extends LinearLayout implements View.OnClick isExpand = !isExpand; //回调 - if (mListener != null){ - mListener.onStateChanged(isExpand); + if (mStateListener != null){ + mStateListener.onStateChanged(isExpand); } } - private OnStateChangeListener mListener; + private OnStateChangeListener mStateListener; /** * 定义状态改变接口 @@ -221,6 +223,25 @@ public class ExpandableLinearLayout extends LinearLayout implements View.OnClick } public void setOnStateChangeListener(OnStateChangeListener mListener) { - this.mListener = mListener; + this.mStateListener = mListener; + } + + + public void setOnItemClickListener(final OnItemClickListener listener){ + int endIndex = useDefaultBottom ? getChildCount() - 1 : getChildCount();//如果是使用默认底部,则结束的下标是到底部之前 + for (int i = 0; i< endIndex;i++){ + View view = getChildAt(i); + final int position = i; + view.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + listener.onItemClick(v,position); + } + }); + } + } + + public interface OnItemClickListener{ + void onItemClick(View view,int position); } } -- GitLab