Skip to content

Commit ac78409

Browse files
committed
1.修复使用默认底部状态下,条目插入顺序混乱的Bug;2.增加子条目点击事件的监听
1 parent 93fe460 commit ac78409

5 files changed

Lines changed: 54 additions & 9 deletions

File tree

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,15 @@
114114

115115
![](./intro_img/ell_4.gif)
116116

117+
#### 5.设置条目点击事件
118+
119+
ellProduct.setOnItemClickListener(new ExpandableLinearLayout.OnItemClickListener() {
120+
@Override
121+
public void onItemClick(View view, int position) {
122+
Toast.makeText(EllCustomBottomDemoActivity.this,names[position] , Toast.LENGTH_SHORT).show();
123+
}
124+
});
125+
117126
### 二、使用自定义底部
118127

119128
布局文件中,ExpandableLinearLayout配置useDefaultBottom="false",声明不使用默认底部。自己定义底部的布局。

demo/src/main/java/com/chaychan/expandablelinearlayout/EllCustomBottomDemoActivity.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.widget.ImageView;
77
import android.widget.RelativeLayout;
88
import android.widget.TextView;
9+
import android.widget.Toast;
910

1011
import com.bumptech.glide.Glide;
1112
import com.chaychan.expandablelinearlayout.bean.ProductBean;
@@ -62,7 +63,7 @@ protected void onCreate(Bundle savedInstanceState) {
6263

6364
ellProduct.removeAllViews();//清除所有的子View(避免重新刷新数据时重复添加)
6465
//添加数据
65-
for (int i = 0; i < 5; i++) {
66+
for (int i = 0; i < 4; i++) {
6667
View view = View.inflate(this, R.layout.item_product, null);
6768
ProductBean productBean = new ProductBean(imgUrls[i], names[i], intros[i], "12.00");
6869
ViewHolder viewHolder = new ViewHolder(view, productBean);
@@ -90,6 +91,13 @@ public void onClick(View v) {
9091
ellProduct.toggle();
9192
}
9293
});
94+
95+
ellProduct.setOnItemClickListener(new ExpandableLinearLayout.OnItemClickListener() {
96+
@Override
97+
public void onItemClick(View view, int position) {
98+
Toast.makeText(EllCustomBottomDemoActivity.this,names[position] , Toast.LENGTH_SHORT).show();
99+
}
100+
});
93101
}
94102

95103
// 箭头的动画
@@ -103,7 +111,6 @@ private void doArrowAnim(boolean isExpand) {
103111
}
104112
}
105113

106-
107114
class ViewHolder {
108115
@Bind(R.id.iv_img)
109116
ImageView ivImg;

demo/src/main/java/com/chaychan/expandablelinearlayout/EllDefaultBottomDemoActivity.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.view.View;
66
import android.widget.ImageView;
77
import android.widget.TextView;
8+
import android.widget.Toast;
89

910
import com.bumptech.glide.Glide;
1011
import com.chaychan.expandablelinearlayout.bean.ProductBean;
@@ -60,6 +61,13 @@ protected void onCreate(Bundle savedInstanceState) {
6061
viewHolder.refreshUI();
6162
ellProduct.addItem(view);//添加子条目
6263
}
64+
65+
ellProduct.setOnItemClickListener(new ExpandableLinearLayout.OnItemClickListener() {
66+
@Override
67+
public void onItemClick(View view, int position) {
68+
Toast.makeText(EllDefaultBottomDemoActivity.this,names[position] , Toast.LENGTH_SHORT).show();
69+
}
70+
});
6371
}
6472

6573
class ViewHolder {

demo/src/main/res/layout/page_ell_custom_bottom_demo.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
android:layout_height="wrap_content"
2020
android:layout_marginTop="10dp"
2121
android:orientation="vertical"
22-
app:defaultItemCount="2"
22+
app:defaultItemCount="3"
2323
app:useDefaultBottom="false"
2424
>
2525

library/src/main/java/com/chaychan/library/ExpandableLinearLayout.java

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class ExpandableLinearLayout extends LinearLayout implements View.OnClick
3434
private float fontSize;
3535
private int textColor;
3636
private int arrowResId;
37+
private int mPosition;
3738

3839
public ExpandableLinearLayout(Context context) {
3940
this(context, null);
@@ -85,10 +86,10 @@ private void findViews() {
8586

8687

8788
public void addItem(View view) {
88-
int childCount = getChildCount();
8989
if (!useDefaultBottom){
9090
//如果不使用默认底部
9191
addView(view);
92+
int childCount = getChildCount();
9293
if (childCount > defaultItemCount){
9394
hide();
9495
}
@@ -100,7 +101,8 @@ public void addItem(View view) {
100101
//如果还没有底部
101102
addView(view);
102103
} else {
103-
addView(view, childCount - 2);//插在底部之前
104+
int childCount = getChildCount();
105+
addView(view, childCount - 1);//插在底部之前
104106
}
105107
refreshUI(view);
106108
}
@@ -205,13 +207,13 @@ public void toggle() {
205207
isExpand = !isExpand;
206208

207209
//回调
208-
if (mListener != null){
209-
mListener.onStateChanged(isExpand);
210+
if (mStateListener != null){
211+
mStateListener.onStateChanged(isExpand);
210212
}
211213
}
212214

213215

214-
private OnStateChangeListener mListener;
216+
private OnStateChangeListener mStateListener;
215217

216218
/**
217219
* 定义状态改变接口
@@ -221,6 +223,25 @@ public interface OnStateChangeListener {
221223
}
222224

223225
public void setOnStateChangeListener(OnStateChangeListener mListener) {
224-
this.mListener = mListener;
226+
this.mStateListener = mListener;
227+
}
228+
229+
230+
public void setOnItemClickListener(final OnItemClickListener listener){
231+
int endIndex = useDefaultBottom ? getChildCount() - 1 : getChildCount();//如果是使用默认底部,则结束的下标是到底部之前
232+
for (int i = 0; i< endIndex;i++){
233+
View view = getChildAt(i);
234+
final int position = i;
235+
view.setOnClickListener(new OnClickListener() {
236+
@Override
237+
public void onClick(View v) {
238+
listener.onItemClick(v,position);
239+
}
240+
});
241+
}
242+
}
243+
244+
public interface OnItemClickListener{
245+
void onItemClick(View view,int position);
225246
}
226247
}

0 commit comments

Comments
 (0)