效果圖
原理說明:
- 最外層爲ScrollView,包裹其他顯示內容+分組列表(採用第三方ListView:NestFullListView)
- 分組列表採用LinearLayout左右佈局,左邊控制展開/收縮控件爲CheckBox,中間爲分割線,右邊列表同樣採用NestFullListView
- 收縮/展開通過重新創建不同數據量的Adapter設置給NestFullListView
NestFullListView
我在這個項目中只是複製了NestFullListView當中的NestFullListView.java、NestFullListViewAdapter.java、NestFullViewHolder.java三個文件來使用。
代碼
因爲使用比較簡單,所以直接貼上代碼
1、該頁面xml佈局文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 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"
tools:context="com.example.zpf.animmenu.GroupListActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="180dp"
android:background="#e671df">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="這個就是當做其他的佈局,所以很隨意啦"
android:gravity="center"
android:textColor="#ffffff"
tools:ignore="HardcodedText" />
</FrameLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="給個標題"
android:background="#dadada"
android:lines="1"
android:textSize="12sp"/>
<customview.nestfulllistview.NestFullListView
android:id="@+id/nflv_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:divider="@drawable/shape_divider_group"
android:showDividers="middle"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
2、頁面的java代碼
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import customview.nestfulllistview.NestFullListView;
import customview.nestfulllistview.NestFullListViewAdapter;
import customview.nestfulllistview.NestFullViewHolder;
import model.GroupItemModel;
import model.GroupModel;
public class GroupListActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_group_list);
initView();
}
private void initView() {
//製造假數據
ArrayList<GroupModel> groupModels = new ArrayList<>();
for (int i=0; i<5; i++) {
GroupModel groupModel = new GroupModel();
String groupTitle = "第" + i + "組";
groupModel.setGroupTitle(groupTitle);
ArrayList<GroupItemModel> itemModels = new ArrayList<>();
for (int j=0; j<5; j++) {
GroupItemModel itemModel = new GroupItemModel();
itemModel.setTitle(groupTitle + j + "分隊");
itemModel.setUrl("這是是url你曉得不?第" + j + "條");
itemModels.add(itemModel);
}
groupModel.setItemModels(itemModels);
groupModels.add(groupModel);
}
//NestFullListView的使用請參考Github,項目地址:https://github.com/mcxtzhang/NestFullListView
NestFullListView nestFullListView = (NestFullListView) findViewById(R.id.nflv_group);
nestFullListView.setAdapter(
new NestFullListViewAdapter<GroupModel>(R.layout.item_group_nflv, groupModels) {
@Override
public void onBind(int pos, GroupModel groupModel, NestFullViewHolder holder) {
holder.setText(R.id.cb_nflv, groupModel.getGroupTitle());
final NestFullListView nflvInner = holder.getView(R.id.nflv_inner);
final List<GroupItemModel> itemModels = groupModel.getItemModels();
nflvInner.setAdapter(getInnerAdapter(itemModels, false));
CheckBox cb = holder.getView(R.id.cb_nflv);
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
nflvInner.setAdapter(getInnerAdapter(itemModels, b));
}
});
}
});
}
/**
* 重新獲取要設置顯示的Adapter
* @param isChecked 展開、收縮CheckBox是否選中,默認不展開
* */
private NestFullListViewAdapter<GroupItemModel> getInnerAdapter(
List<GroupItemModel> models, boolean isChecked) {
if (models == null)
return null;
//展開顯示全部數據,未展開顯示兩條數據
List<GroupItemModel> showModels = isChecked ? models :
(models.size() > 2 ? models.subList(0, 2) : models);
NestFullListViewAdapter<GroupItemModel> adapter =
new NestFullListViewAdapter<GroupItemModel>(R.layout.item_group_item_nflv, showModels) {
@Override
public void onBind(int pos, GroupItemModel groupItemModel, NestFullViewHolder holder) {
TextView tv = holder.getView(R.id.tv_group_inner);
tv.setText(groupItemModel.getTitle());
final String url = groupItemModel.getUrl();
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(GroupListActivity.this, url, Toast.LENGTH_SHORT).show();
}
});
}
};
return adapter;
}
}
3、第一層分組item佈局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical">
<CheckBox
android:id="@+id/cb_nflv"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:layout_height="wrap_content"
android:button="@null"
android:drawableTop="@mipmap/ic_nflv_group_icon"
android:drawablePadding="4dp"
android:drawableBottom="@drawable/selector_nflv_checkbox_arrow"/>
<View
android:layout_width="0.4dp"
android:layout_height="match_parent"
android:background="#dadada"/>
<customview.nestfulllistview.NestFullListView
android:id="@+id/nflv_inner"
android:layout_width="0dp"
android:layout_weight="4"
android:layout_height="wrap_content"
android:orientation="vertical"
android:showDividers="middle"/>
</LinearLayout>
4、單個分組內層內容item佈局文件(僅僅是一個TextView)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="48dp">
<TextView
android:id="@+id/tv_group_inner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:drawableRight="@mipmap/ic_arrow_right_group"
android:paddingLeft="16dp"
android:paddingRight="16dp"/>
</LinearLayout>
5、代碼當中用到的分割線Drawable和CheckBox切換Selector還有圖片大家可以自行編寫、找圖替換。
That’s all!代碼量不多,NestFullListView的使用也不難,具體使用可以參看作者的github。希望這篇博文可以幫助到需要的同仁。