提交 ac784094 编写于 作者: C chaychan

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

上级 93fe4605
...@@ -114,6 +114,15 @@ ...@@ -114,6 +114,15 @@
![](./intro_img/ell_4.gif) ![](./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",声明不使用默认底部。自己定义底部的布局。 布局文件中,ExpandableLinearLayout配置useDefaultBottom="false",声明不使用默认底部。自己定义底部的布局。
......
...@@ -6,6 +6,7 @@ import android.view.View; ...@@ -6,6 +6,7 @@ import android.view.View;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.RelativeLayout; import android.widget.RelativeLayout;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
import com.chaychan.expandablelinearlayout.bean.ProductBean; import com.chaychan.expandablelinearlayout.bean.ProductBean;
...@@ -62,7 +63,7 @@ public class EllCustomBottomDemoActivity extends AppCompatActivity { ...@@ -62,7 +63,7 @@ public class EllCustomBottomDemoActivity extends AppCompatActivity {
ellProduct.removeAllViews();//清除所有的子View(避免重新刷新数据时重复添加) 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); View view = View.inflate(this, R.layout.item_product, null);
ProductBean productBean = new ProductBean(imgUrls[i], names[i], intros[i], "12.00"); ProductBean productBean = new ProductBean(imgUrls[i], names[i], intros[i], "12.00");
ViewHolder viewHolder = new ViewHolder(view, productBean); ViewHolder viewHolder = new ViewHolder(view, productBean);
...@@ -90,6 +91,13 @@ public class EllCustomBottomDemoActivity extends AppCompatActivity { ...@@ -90,6 +91,13 @@ public class EllCustomBottomDemoActivity extends AppCompatActivity {
ellProduct.toggle(); 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 { ...@@ -103,7 +111,6 @@ public class EllCustomBottomDemoActivity extends AppCompatActivity {
} }
} }
class ViewHolder { class ViewHolder {
@Bind(R.id.iv_img) @Bind(R.id.iv_img)
ImageView ivImg; ImageView ivImg;
......
...@@ -5,6 +5,7 @@ import android.support.v7.app.AppCompatActivity; ...@@ -5,6 +5,7 @@ import android.support.v7.app.AppCompatActivity;
import android.view.View; import android.view.View;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
import com.chaychan.expandablelinearlayout.bean.ProductBean; import com.chaychan.expandablelinearlayout.bean.ProductBean;
...@@ -60,6 +61,13 @@ public class EllDefaultBottomDemoActivity extends AppCompatActivity { ...@@ -60,6 +61,13 @@ public class EllDefaultBottomDemoActivity extends AppCompatActivity {
viewHolder.refreshUI(); viewHolder.refreshUI();
ellProduct.addItem(view);//添加子条目 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 { class ViewHolder {
......
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="10dp" android:layout_marginTop="10dp"
android:orientation="vertical" android:orientation="vertical"
app:defaultItemCount="2" app:defaultItemCount="3"
app:useDefaultBottom="false" app:useDefaultBottom="false"
> >
......
...@@ -34,6 +34,7 @@ public class ExpandableLinearLayout extends LinearLayout implements View.OnClick ...@@ -34,6 +34,7 @@ public class ExpandableLinearLayout extends LinearLayout implements View.OnClick
private float fontSize; private float fontSize;
private int textColor; private int textColor;
private int arrowResId; private int arrowResId;
private int mPosition;
public ExpandableLinearLayout(Context context) { public ExpandableLinearLayout(Context context) {
this(context, null); this(context, null);
...@@ -85,10 +86,10 @@ public class ExpandableLinearLayout extends LinearLayout implements View.OnClick ...@@ -85,10 +86,10 @@ public class ExpandableLinearLayout extends LinearLayout implements View.OnClick
public void addItem(View view) { public void addItem(View view) {
int childCount = getChildCount();
if (!useDefaultBottom){ if (!useDefaultBottom){
//如果不使用默认底部 //如果不使用默认底部
addView(view); addView(view);
int childCount = getChildCount();
if (childCount > defaultItemCount){ if (childCount > defaultItemCount){
hide(); hide();
} }
...@@ -100,7 +101,8 @@ public class ExpandableLinearLayout extends LinearLayout implements View.OnClick ...@@ -100,7 +101,8 @@ public class ExpandableLinearLayout extends LinearLayout implements View.OnClick
//如果还没有底部 //如果还没有底部
addView(view); addView(view);
} else { } else {
addView(view, childCount - 2);//插在底部之前 int childCount = getChildCount();
addView(view, childCount - 1);//插在底部之前
} }
refreshUI(view); refreshUI(view);
} }
...@@ -205,13 +207,13 @@ public class ExpandableLinearLayout extends LinearLayout implements View.OnClick ...@@ -205,13 +207,13 @@ public class ExpandableLinearLayout extends LinearLayout implements View.OnClick
isExpand = !isExpand; isExpand = !isExpand;
//回调 //回调
if (mListener != null){ if (mStateListener != null){
mListener.onStateChanged(isExpand); mStateListener.onStateChanged(isExpand);
} }
} }
private OnStateChangeListener mListener; private OnStateChangeListener mStateListener;
/** /**
* 定义状态改变接口 * 定义状态改变接口
...@@ -221,6 +223,25 @@ public class ExpandableLinearLayout extends LinearLayout implements View.OnClick ...@@ -221,6 +223,25 @@ public class ExpandableLinearLayout extends LinearLayout implements View.OnClick
} }
public void setOnStateChangeListener(OnStateChangeListener mListener) { 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);
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册