今天項目需要實現一個點擊展開收縮的功能,網上搜索找了個適合自己的,稍微修改了下適合項目本身需求的。
只是做記錄,需要的可以參考。
不廢話直接上代碼。
首先main佈局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/head"
android:orientation="vertical"
tools:context="com.foldtext.MainActivity">
<LinearLayout
android:id="@+id/lin_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/backdrop_top_style"
android:orientation="horizontal">
<TextView
android:id="@+id/hello1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:maxLines="2"
android:padding="10dp"
android:text="標題" />
<TextView
android:id="@+id/textview_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="點擊向下展開" />
<ImageView
android:id="@+id/img_shrink"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:padding="10dp"
android:src="@mipmap/bottom" />
</LinearLayout>
<com.foldtext.Utils.ExpandView
android:id="@+id/ex_expandview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
</LinearLayout>
2.收縮控件佈局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#99ffffff"
android:gravity="center_horizontal"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_weight="1"
android:gravity="center"
android:text="按鈕1"
android:textSize="14sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_weight="1"
android:gravity="center"
android:text="按鈕2"
android:textSize="14sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_weight="1"
android:gravity="center"
android:text="按鈕3"
android:textSize="14sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_weight="1"
android:gravity="center"
android:text="按鈕4"
android:textSize="14sp" />
</LinearLayout>
3.Main代碼:
package com.foldtext;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.foldtext.Utils.ExpandView;
public class MainActivity extends AppCompatActivity {
private LinearLayout lin_tv;
private ImageView img_shrink;
private ExpandView expandView;
private TextView textview_title;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
oncreateview();
ExpandView();
}
/**
* 初始化控件
*/
private void oncreateview() {
lin_tv = (LinearLayout) findViewById(R.id.lin_tv);
img_shrink = (ImageView) findViewById(R.id.img_shrink);
expandView = (ExpandView) findViewById(R.id.ex_expandview);
textview_title = (TextView) findViewById(R.id.textview_title);
}
/**
* 初始化調用
*/
public void ExpandView() {
expandView.setContentView();
lin_tv.setClickable(true);
lin_tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (expandView.isExpand()) {
expandView.collapse();
textview_title.setText("點擊向下展開");
img_shrink.setImageDrawable(getResources().getDrawable(R.mipmap.bottom));
} else {
expandView.expand();
textview_title.setText("點擊向上收縮");
img_shrink.setImageDrawable(getResources().getDrawable(R.mipmap.shrink));
}
}
});
}
}
4.收縮控件代碼:
package com.foldtext.Utils;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import com.foldtext.R;
/**
* 收縮控件
*/
public class ExpandView extends FrameLayout implements View.OnClickListener {
private Animation mExpandAnimation;
private Animation mCollapseAnimation;
private boolean mIsExpand;
public ExpandView(Context context) {
this(context, null);
}
public ExpandView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ExpandView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initExpandView();
}
private void initExpandView() {
LayoutInflater.from(getContext()).inflate(R.layout.layout_expand, this, true);
mExpandAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.expand);
mExpandAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
setVisibility(View.VISIBLE);
}
});
mCollapseAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.collapse);
mCollapseAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
setVisibility(View.GONE);
}
});
}
public void collapse() {
if (mIsExpand) {
mIsExpand = false;
clearAnimation();
startAnimation(mCollapseAnimation);
}
}
public void expand() {
if (!mIsExpand) {
mIsExpand = true;
clearAnimation();
startAnimation(mExpandAnimation);
}
}
public boolean isExpand() {
return mIsExpand;
}
public void setContentView() {
View view = null;
view = LayoutInflater.from(getContext()).inflate(R.layout.layout_expand, null);
removeAllViews();
addView(view);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
}
}
}
效果圖:
就這樣完啦!
————————————————
版權聲明:本文爲CSDN博主「Ke-Le8」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qq_19714505/article/details/71216308