listview 展开收起,实例(自创)
下面是代码
{ (adpter中)
private static ViewHolder viewHolder;//(记录上一个点击打开的viewholder)
public static void hasExpand() { // 让上一个的viewholder 隐藏和设置isMoreClick 为false
viewHolder.item2.setVisibility(View.GONE);
viewHolder.isMoreClick = false;
}
public static class ViewHolder {
@Bind(R.id.btn_more)
Button btnMore;
@Bind(R.id.item2)
RelativeLayout item2;
private View contentView;
private boolean isMoreClick;
private MusiciansSongs.DataBean.ListBean data;
public void setData(MusiciansSongs.DataBean.ListBean data) {
this.data = data;
refreshView();
}
public void refreshView() {
btnMore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!isMoreClick) {//接下来的那些有点绕
if (viewHolder == null) {//当第一次点击某个item的时候 什么都不做
} else if (viewHolder != null && viewHolder.isMoreClick == true) {
//判断是否有上一个item是不是没有收起来,没有收起来的就关闭,
hasExpand();
}
viewHolder = ViewHolder.this;
isMoreClick = true;
item2.setVisibility(View.VISIBLE);
} else {
isMoreClick = false;
item2.setVisibility(View.GONE);
}
}
});
}
public ViewHolder() {
contentView = UiUtils.inflate(R.layout.songs_item);
ButterKnife.bind(this, contentView);
contentView.setTag(this);
}
public View getContentView() {
return contentView;
}
}
}
xml文件:
<?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="wrap_content"
android:orientation="vertical">
<Button android:id="@+id/btn_more"
android:layout_width="30dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:background="@drawable/btn_more_selector"
/>
<RelativeLayout
android:id="@+id/item2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="40dp"
android:background="@color/background_tab_pressed"
android:visibility="gone" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:id="@+id/upload_time"
android:textColor="#000000"
android:textSize="16sp" >
</TextView>
</RelativeLayout>
这个排版有点蛋疼。
整个demo 地址为http://download.csdn.net/detail/a136558/9514308
不懂的 可以留言,我也不知道这样的性能怎么样,有高手请指教。