Android 標題下的內容摺疊效果
有時候想把標題下的內容摺疊/展示以減少空間,這裡有個控制元件叫CollapseView,效果圖:
其中CollapseView.java
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.edwin.R;
/**
* 摺疊/展開ViewGroup
*
* @USER Edwin
* @DATE 16/6/5 上午1:57
*/
public class CollapseView extends LinearLayout {
private final Context mContext;
private long duration = 555;//展開/摺疊的時間(s)
private TextView mNumberTextView;
private TextView mTitleTextView;
private RelativeLayout mTitleRelativeLayout;
private RelativeLayout mContentRelativeLayout;
private ImageView mArrowImageView;
private int parentWidthMeasureSpec;
private int parentHeightMeasureSpec;
public CollapseView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
LayoutInflater.from(context).inflate(R.layout.collapse_layout, this);
init();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
parentWidthMeasureSpec = widthMeasureSpec;
parentHeightMeasureSpec = heightMeasureSpec;
}
private void init() {
mNumberTextView = (TextView) findViewById(R.id.numberTextView);
mTitleTextView = (TextView) findViewById(R.id.titleTextView);
mTitleRelativeLayout = (RelativeLayout) findViewById(R.id.titleRelativeLayout);
mContentRelativeLayout = (RelativeLayout) findViewById(R.id.contentRelativeLayout);
mArrowImageView = (ImageView) findViewById(R.id.arrowImageView);
mTitleRelativeLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
rotateArrow();
}
});
collapse(mContentRelativeLayout);
}
public void setNumber(String number) {
if (!TextUtils.isEmpty(number)) {
mNumberTextView.setText(number);
}
}
public void setTitle(String title) {
if (!TextUtils.isEmpty(title)) {
mTitleTextView.setText(title);
}
}
public void setContent(int resID) {
LinearLayout layout = new LinearLayout(mContext);
layout.setBackgroundColor(Color.parseColor("#E7E7EF"));
LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(params);
ImageView view = new ImageView(mContext);
params.gravity = Gravity.CENTER;
view.setLayoutParams(params);
view.setImageResource(resID);
layout.addView(view);
mContentRelativeLayout.addView(layout);
}
public void setContent(String content) {
TextView view = new TextView(mContext);
view.setText(content);
mContentRelativeLayout.addView(view);
}
private void rotateArrow() {
int angle = 0;
if (mArrowImageView.getTag() == null || mArrowImageView.getTag().equals(true)) {
mArrowImageView.setTag(false);
angle = -180;
//TODO 展開
expand(mContentRelativeLayout);
} else {
angle = 0;
mArrowImageView.setTag(true);
//TODO 摺疊
collapse(mContentRelativeLayout);
}
mArrowImageView.animate().setDuration(duration).rotation(angle);
}
/**
* 摺疊
*
* @param view 檢視
*/
private void collapse(final View view) {
final int measuredHeight = view.getMeasuredHeight();
Animation animation = new Animation() {
/*繪製動畫的過程中會反覆的呼叫applyTransformation 函式,每次呼叫引數interpolatedTime值都會變化,
該引數從0漸變為1,當該引數為1時表明動畫結束。*/
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
Log.e("TAG", "interpolatedTime = " + interpolatedTime);
if (interpolatedTime == 1) {
mContentRelativeLayout.setVisibility(GONE);
} else {
view.getLayoutParams().height = measuredHeight - (int) (measuredHeight * interpolatedTime);
view.requestLayout();
}
}
};
animation.setDuration(duration);
view.startAnimation(animation);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
}
/**
* 展開
*
* @param view 檢視
*/
private void expand(final View view) {
view.measure(parentWidthMeasureSpec, parentHeightMeasureSpec);
final int measuredHeight = view.getMeasuredHeight();
view.setVisibility(View.VISIBLE);
Animation animation = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
Log.e("TAG", "interpolatedTime = " + interpolatedTime);
if (interpolatedTime == 1) {
view.getLayoutParams().height = measuredHeight;
} else {
view.getLayoutParams().height = (int) (measuredHeight * interpolatedTime);
}
view.requestLayout();
}
@Override
public boolean willChangeBounds() {
return true;
}
};
animation.setDuration(duration);
view.startAnimation(animation);
}
}
java:
private CollapseView CollapseView1;
CollapseView1 = (CollapseView) findViewById(R.id.collapseView1);
CollapseView1.setNumber("1");
CollapseView1.setTitle("女友");
CollapseView1.setContent(R.mipmap.aaa);
xml:
<com.edwin.view.CollapseView
android:id="@+id/collapseView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
來源:https://github.com/why168/CollapseView
相關文章
- jQuery table表格同類內容摺疊效果jQuery
- 給 RecyclerView 加上摺疊的效果View
- [開發教程]第34講:Bootstrap摺疊內容boot
- 微信疑似推出朋友圈多內容摺疊功能HQTQT
- html網頁內容如何實現上標和下標效果HTML網頁
- CoordinatorLayout實現酷炫摺疊效果
- 用Javascript實現選單摺疊效果JavaScript
- 微信小程式過長文字摺疊效果的探索微信小程式
- 可摺疊,可標記日曆
- 聊天平臺原始碼,標題過長自動應用摺疊式標題欄原始碼
- Material Design之CollapsingToolbarLayout,實現Toolbar的摺疊效果Material Design
- css實現的禁止標籤中內容選中效果CSS
- Android之去掉文字內容的HTML標籤AndroidHTML
- BootStrap | 例項 - 摺疊boot
- java之常量摺疊Java
- Sublime程式碼摺疊
- 【BootStrap】--摺疊外掛boot
- UI之可摺疊的TextViewUITextView
- 手風琴方式展開和摺疊導航選單效果
- 蘋果OLED摺疊手機和可摺疊平板電腦情景分析蘋果
- jQuery table內容點選標題排序jQuery排序
- js和css3實現能夠展開和摺疊的摺扇效果JSCSSS3
- js和css實現的扇子展開和摺疊效果程式碼例項JSCSS
- 榮耀摺疊,太卷啦
- Android FoldingLayout 摺疊佈局 原理及實現(一)Android
- css3實現動態摺疊生成立方體旋轉效果CSSS3
- 可摺疊iPhone概念設計圖:手機可摺疊秒變筆記本iPhone筆記
- 曝蘋果摺疊屏iPhone再度延期2年!或將研發可摺疊MacBook蘋果iPhoneMac
- 使用Jquery和CSS摺疊影象jQueryCSS
- PaperView:像紙一樣摺疊View
- margin系列之外邊距摺疊
- 摺疊::Vim進階索引[2]索引
- js 選項卡 【滑鼠懸停標題,顯示對應內容,改變其他標題顏色並隱藏內容】...JS
- web 端展現報表資料時如何實現摺疊展開效果?Web
- css input文字框中的內容居中效果CSS
- win10右下角圖示如何摺疊 win10右下角圖示摺疊的方法Win10
- CSS中上下margin的傳遞和摺疊CSS
- 短視訊程式碼,摺疊cell的使用