1. 程式人生 > 其它 >小遊戲APP原始碼,帶頭部可摺疊的ListVivew

小遊戲APP原始碼,帶頭部可摺疊的ListVivew

小遊戲APP原始碼,帶頭部可摺疊的ListVivew實現的相關程式碼
1.新增依賴

```cpp
dependencies {
compile 'com.zgh.collapsiblelistview:collapsiblelistview:1.0.1'
}
```

2.xml佈局

```cpp
<com.zgh.collapsiblelistview.CollapsibleListView
android:id="@+id/clv_3"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal
="true" android:layout_marginTop="20dp" app:CollapsibleContentColor="#7CFBE1" app:CollapsibleDuration="250" app:CollapsibleHight="150dp" app:CollapsibleTitle="列表三" app:CollapsibleCornerRadius="10dp" app:CollapsibleTitleTextColor="#000000" app:CollapsibleTitleColor="#A4D2D2" /> ```

3.設定adapter與點選事件

```cpp
ArrayAdapter
<String> adapter; CollapsibleListView clv_1,clv_2,clv_3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); adapter=new ArrayAdapter<String>(this,R.layout.simple_list_item,new String[]{"Item1","Item2"
,"Item3","Item4","Item5","Item6","Item7"}); clv_1= (CollapsibleListView) this.findViewById(R.id.clv_1); clv_1.setAdapter(adapter); clv_1.getListview().setOnItemClickListener(new MyOnItemClickListener("first")); } public class MyOnItemClickListener implements AdapterView.OnItemClickListener{ String title; public MyOnItemClickListener(String title){ this.title=title; } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(MainActivity.this,title+" "+parent.getAdapter().getItem(position)+" click",Toast.LENGTH_SHORT).show(); } } ```

屬性

```cpp
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CollapsibleListView">
<!--標題-->
<attr name="CollapsibleTitle" format="string" />
<!--展開listview的高度-->
<attr name="CollapsibleHight" format="dimension" />
<!--展開時間-->
<attr name="CollapsibleDuration" format="integer" />
<!--標題佈局顏色-->
<attr name="CollapsibleTitleColor" format="color" />
<!--內容佈局顏色-->
<attr name="CollapsibleContentColor" format="color" />
<!--圓角-->
<attr name="CollapsibleCornerRadius" format="dimension" />
<!--標題文字顏色-->
<attr name="CollapsibleTitleTextColor" format="color" />
</declare-styleable>
</resources>
```

實現
實現的部分沒有什麼難度,主要講兩點,一個是摺疊效果的實現,還有一個是背景的生成

1.摺疊效果實現,
通過使用ValueAnimator在動畫執行的時候,修改ListView的高度。

```cpp
ValueAnimator animatorOpen, animatorClose;

private void initAnimator() {
animatorClose = creatAnimator(openHight, 0);
animatorOpen = creatAnimator(0, openHight);
}

private ValueAnimator creatAnimator(int from, int hight) {
ValueAnimator animator = ValueAnimator.ofInt(from, hight);
animator.setDuration(openDuration);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int h = (int) animation.getAnimatedValue();
ViewGroup.LayoutParams params = listview.getLayoutParams();
params.height = h;
listview.setLayoutParams(params);
}
});
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationCancel(Animator animation) {
isAnimatting = false;
}

@Override
public void onAnimationEnd(Animator animation) {
isOpen = !isOpen;
Drawable drawable = isOpen ? openDrawable : closeDrawable;
tv_title.setCompoundDrawables(null, null, drawable, null);
isAnimatting = false;
}

@Override
public void onAnimationStart(Animator animation) {
isAnimatting = true;
}
});
return animator;
}
```

2.背景生成
由於需要可定製圓角大小,所以不能單純的直接設定佈局的背景色,此處採用的是在解析完屬性以後根據屬性動態生成ShapeDrawable做完佈局的背景

```cpp
private static final int DEFAULT_HEIGHT = 150;//dp
private static final int DEFAULT_DURATION = 500;//ms
private static final String DEFAULT_TITLE = "標題";
private static final int DEFAULT_TITLE_COLOR = Color.parseColor("#E14C60");
private static final int DEFAULT_CONTENT_COLOR = Color.parseColor("#C76C78");
private static final int DEFAULT_TITLE_TEXT_COLOR = Color.WHITE;
private static final int DEFAULT_CORNER_RADIUS = 10;
private Drawable openDrawable, closeDrawable;
private int titleColor;
private int contentColor;
private int cornerRadius = 5;
private int titleTextColor;

private void resolveAttrs(Context context, AttributeSet attrs) {
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CollapsibleListView);
String title = array.getString(R.styleable.CollapsibleListView_CollapsibleTitle);
if (title == null) {
title = DEFAULT_TITLE;
}
mTitle = title;
openHight = (int) array.getDimension(R.styleable.CollapsibleListView_CollapsibleHight, dip2px(getContext(), DEFAULT_HEIGHT));
openDuration = array.getInt(R.styleable.CollapsibleListView_CollapsibleDuration, DEFAULT_DURATION);
if (openDuration < 0) {
openDuration = DEFAULT_DURATION;
}
titleColor = array.getColor(R.styleable.CollapsibleListView_CollapsibleTitleColor, DEFAULT_TITLE_COLOR);
contentColor = array.getColor(R.styleable.CollapsibleListView_CollapsibleContentColor, DEFAULT_CONTENT_COLOR);
cornerRadius = (int) array.getDimension(R.styleable.CollapsibleListView_CollapsibleCornerRadius, DEFAULT_CORNER_RADIUS);
titleTextColor = array.getColor(R.styleable.CollapsibleListView_CollapsibleTitleTextColor, DEFAULT_TITLE_TEXT_COLOR);
array.recycle();
}

private void initBackground() {
// 外部矩形弧度
float[] outerR = new float[8];
for (int i = 0; i < outerR.length; i++) {
outerR[i] = cornerRadius;
}
RoundRectShape rr = new RoundRectShape(outerR, null, null);
ShapeDrawable background = new ShapeDrawable(rr);
//指定填充顏色
background.getPaint().setColor(contentColor);
// 指定填充模式
background.getPaint().setStyle(Paint.Style.FILL);
findViewById(R.id.ll_back).setBackgroundDrawable(background);
ShapeDrawable title = new ShapeDrawable(rr);
title.getPaint().setColor(titleColor);
title.getPaint().setStyle(Paint.Style.FILL);
tv_title.setBackgroundDrawable(title);

}
```

以上就是小遊戲APP原始碼,帶頭部可摺疊的ListVivew實現的相關程式碼, 更多內容歡迎關注之後的文章