效果图
原理说明:
- 最外层为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。希望这篇博文可以帮助到需要的同仁。