效果:
点击展开后:
代码:
先自定义一个ReceivingLinearLayout文件继承LinearLayout布局
public class ReceivingLinearLayout extends LinearLayout {
/**
* 折叠按钮图标
*/
private ImageView mExpandBtn;
private TextView ExpandBtn;
/**
* 是否折叠的临界高度
*/
private int mLimitHeight = 200;
/**
* 是否展开了
*/
private boolean mIsExpand = false;
/**
* 是否支持展开折叠功能
*/
private boolean mSupportExpand = true;
public ReceivingLinearLayout(Context context) {
this(context, null);
}
public ReceivingLinearLayout(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public ReceivingLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setOrientation(VERTICAL);
}
/**
* 初始化
*/
public void init() {
setOrientation(VERTICAL);
}
/**
* 重写onMeasure动态控制控件的高度
*
* @param widthMeasureSpec
* @param heightMeasureSpec
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//获取自身的测量高度
int measureHeight = getMeasuredHeight();
//设置折叠按钮是否显示
setExpandBtnVisiable(measureHeight <= mLimitHeight ? View.GONE : View.VISIBLE);
//如果不支持折叠或者需要展开或者高度不到折叠高度,就保持默认测量
if (!mSupportExpand || mIsExpand || measureHeight <= mLimitHeight) {
return;
}
//重新设置高度测量参数
heightMeasureSpec = MeasureSpec.makeMeasureSpec(mLimitHeight, MeasureSpec.EXACTLY);
//重新测量
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
/**
* 绑定展开折叠按钮图标
*
* @param view
* @param expandRes
* @param foldRes
*/
public void bindExpandButton(ImageView view, TextView textView ,final int expandRes, final int foldRes) {
mExpandBtn = view;
ExpandBtn = textView;
if (textView == null) {
return;
}
textView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
setIsExpand(!mIsExpand);
mExpandBtn.setImageResource(mIsExpand ? foldRes : expandRes);
ExpandBtn.setText(mIsExpand ? "收起":"展开");
}
});
setExpandBtnVisiable(GONE);
}
/**
* 获取临界折叠高度
*
* @return
*/
public int getLimitHeight() {
return mLimitHeight;
}
/**
* 设置临界折叠高度
*
* @param limitHeight
*/
public void setLimitHeight(int limitHeight) {
this.mLimitHeight = limitHeight;
}
/**
* 获取是否展开了
*
* @return
*/
public boolean isIsExpand() {
return mIsExpand;
}
/**
* 设置是否展开
*
* @param isExpand
*/
public void setIsExpand(boolean isExpand) {
this.mIsExpand = isExpand;
requestLayout();
}
/**
* 获取当前是否支持折叠展开功能
*
* @return
*/
public boolean isSupportExpand() {
return mSupportExpand;
}
/**
* 设置折叠还是展开
*
* @param supportExpand
*/
public void setSupportExpand(boolean supportExpand) {
this.mSupportExpand = supportExpand;
setExpandBtnVisiable(GONE);
}
/**
* 设置折叠图标是否显示
*
* @param visiable
*/
private void setExpandBtnVisiable(int visiable) {
if (mExpandBtn != null) {
if (!mSupportExpand) {
mExpandBtn.setVisibility(GONE);
} else {
mExpandBtn.setVisibility(visiable);
}
}
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<zzx.com.opensource.a.ReceivingLinearLayout
android:id="@+id/linearLayout"
android:layout_width="355dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="36dp"
android:orientation="vertical"
android:background="@drawable/bt_shape3"
android:layout_marginTop="10dp"
android:padding="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="XXX产品名 "
android:layout_marginTop="10dp"
android:layout_marginLeft="8dp"
android:textColor="#ff2f2f2f"
android:textSize="16sp"/>
<ImageView
android:id="@+id/iv_delete"
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_marginTop="10dp"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:src="@drawable/icon_delete"/>
RelativeLayout>
<View
android:layout_width="320dp"
android:layout_height="4px"
android:layout_alignParentRight="true"
android:layout_marginTop="7dp"
android:background="#ffeeeeee">View>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/logistics_code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="物流码:00000000000001 "
android:textColor="#ff767676"
android:layout_marginTop="11dp"
android:textSize="14sp"/>
<TextView
android:id="@+id/tv_expand_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="展开"
android:textColor="#ff699eff"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:layout_marginTop="7dp"
android:textSize="15sp"/>
<ImageView
android:id="@+id/iv_expand_icon"
android:layout_width="13dp"
android:layout_height="8dp"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:layout_marginTop="13dp"
android:src="@drawable/icon_open"/>
RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/order_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="订单编号:10000000001"
android:textColor="#ff767676"
android:layout_marginTop="11dp"
android:textSize="14sp"/>
RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="时间:2019-04-19 15:20:32"
android:textColor="#ff767676"
android:layout_marginTop="11dp"
android:textSize="14sp"/>
RelativeLayout>
zzx.com.opensource.a.ReceivingLinearLayout>
RelativeLayout>
MainActivity.xml页面:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/shouhuo_return"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="#ffffff">
<ImageView
android:layout_width="12dp"
android:layout_height="18dp"
android:layout_marginTop="13dp"
android:layout_marginLeft="15dp"
android:src="@drawable/icon_return"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="订单记录"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:textColor="#ff2f2f2f"
android:textSize="18sp"/>
RelativeLayout>
<LinearLayout
android:id="@+id/ell_product"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="45dp"
android:orientation="vertical"
android:layout_centerHorizontal="true">
LinearLayout>
RelativeLayout>
LinearLayout>
ScrollView>
LinearLayout>
MainActivity类:
public class MainActivity extends AppCompatActivity {
private LinearLayout linearLayout;//大布局
private ReceivingLinearLayout receivingLinearLayout;//小布局
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout = (LinearLayout) findViewById(R.id.ell_product);
linearLayout.removeAllViews();//清除所有的子View(避免重新刷新数据时重复添加)
for (int i = 1; i <= 10; i++){
ReceivingEntity receivingEntity = new ReceivingEntity("XXX产品名称"+i,"0000000000000"+i,"1000000000"+i,"2019-04-19 15:20:32");
View view = View.inflate( MainActivity.this, R.layout.receiving, null);
receivingLinearLayout = (ReceivingLinearLayout)view.findViewById(R.id.linearLayout);
ViewHolder viewHolder = new ViewHolder(view, receivingEntity);
viewHolder.refreshUI();
receivingLinearLayout.bindExpandButton((ImageView) view.findViewById(R.id.iv_expand_icon),(TextView) view.findViewById(R.id.tv_expand_icon), R.drawable.icon_open, R.drawable.icon_retract);
receivingLinearLayout.setLimitHeight(280);//设置折叠的临界高度
linearLayout.addView(view);//添加子条目
}
}
class ViewHolder {
TextView name;
TextView logistics_code;
TextView order_number;
TextView date;
ReceivingEntity receivingEntity;
//删除按钮
ImageView iv_delete;
public ViewHolder(final View view, final ReceivingEntity receivingEntity) {
ButterKnife.bind(MainActivity.this, view);
this.receivingEntity = receivingEntity;
name = (TextView) view.findViewById(R.id.name);
logistics_code = (TextView) view.findViewById(R.id.logistics_code);
order_number = (TextView) view.findViewById(R.id.order_number);
date = (TextView) view.findViewById(R.id.date);
iv_delete = (ImageView) view.findViewById(R.id.iv_delete);
iv_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
private void refreshUI() {
name.setText(receivingEntity.getName());
logistics_code.setText("物流码:" + receivingEntity.getLogistics_code());
order_number.setText("订单编号:" + receivingEntity.getOrder_number());
date.setText("时间:" + receivingEntity.getDate());
}
}
}
ReceivingEntity类:
public class ReceivingEntity {
private Long id;
private String name;
private String logistics_code;
private String order_number;
private String date;
public ReceivingEntity() {
super();
}
public ReceivingEntity(Long id, String name, String logistics_code, String order_number, String date) {
this.id = id;
this.name = name;
this.logistics_code = logistics_code;
this.order_number = order_number;
this.date = date;
}
public ReceivingEntity(String name, String logistics_code, String order_number, String date) {
this.name = name;
this.logistics_code = logistics_code;
this.order_number = order_number;
this.date = date;
}
@Override
public String toString() {
return "Receiving{" +
"id=" + id +
", name='" + name + '\'' +
", logistics_code='" + logistics_code + '\'' +
", order_number='" + order_number + '\'' +
", date='" + date + '\'' +
'}';
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLogistics_code() {
return logistics_code;
}
public void setLogistics_code(String logistics_code) {
this.logistics_code = logistics_code;
}
public String getOrder_number() {
return order_number;
}
public void setOrder_number(String order_number) {
this.order_number = order_number;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}