一、首先布局
1、主布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<EditText
android:id="@+id/attrText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:editable="false"
android:inputType="none"
android:hint=""
android:textSize="7pt"
android:maxLines="1" />
<Button
android:id="@+id/show"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:text="确定" />
<Button
android:id="@+id/cancel"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:text="取消" />
<!-- <Button-->
<!-- android:id="@+id/add"-->
<!-- android:layout_width="0dp"-->
<!-- android:layout_height="match_parent"-->
<!-- android:layout_weight="1"-->
<!-- android:text="添加NODE" />-->
</LinearLayout>
</LinearLayout>
2、item布局(layour_item_tree.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="40dp"
android:descendantFocusability="blocksDescendants"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv_expand"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="5dp"
android:gravity="center"
android:src="@drawable/arrow_down"
android:textColor="#000"
android:textSize="11sp" />
<TextView
android:id="@+id/tv"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="test"
android:textColor="#000"
android:textSize="15sp" />
<ImageView
android:id="@+id/iv_selected"
android:layout_width="50dp"
android:layout_height="40dp"
android:padding="10dp"
android:src="@drawable/ic_no" />
</LinearLayout>
二、Java代码
1、CheckModel.java
public class CheckModel extends TreeViewNode{
public String name;
public int marginLeft;
public boolean isSelected;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMarginLeft() {
return marginLeft;
}
public void setMarginLeft(int marginLeft) {
this.marginLeft = marginLeft;
}
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean selected) {
isSelected = selected;
}
@Override
public String toString() {
return "name='" + name + '\'';
}
}
2、TreeViewDataSources.java
public class TreeViewDataSource {
private TreeViewDataSource(){
}
private List<TreeViewNode> elements = new ArrayList<>();
private List<TreeViewNode> nodes;
public TreeViewDataSource(List<TreeViewNode> nodes){
this.nodes = nodes;
if(nodes == null){
nodes = new ArrayList<>();
}
collectElements(nodes);
}
private void collectElements(List<? extends TreeViewNode> nodes) {
for(TreeViewNode node:nodes){
elements.add(node);
if(node.isExpand == true && node.child !=null && node.child.size() > 0){
collectElements(node.child);
}
}
}
public void updateNodes(){
elements.clear();
collectElements(nodes);
}
public List<? extends TreeViewNode> getElements(){
return elements;
}
public List<? extends TreeViewNode> getNodes(){
return nodes;
}
}
3、TreeViewNode.java
public class TreeViewNode {
public boolean isExpand;
public List<TreeViewNode> child;
}
4、BaseListViewAdapter.java
public abstract class BaseListViewAdapter<VH extends BaseListViewAdapter.BaseListViewHolder> extends BaseAdapter {
public final void notifyItemChanged(ListView listView, int position){
int visiblePosition = listView.getFirstVisiblePosition();
if(position - visiblePosition >= 0){
View view = listView.getChildAt(position - visiblePosition);
if(view != null){
VH viewHolder = (VH) view.getTag();
onBindViewHolder(viewHolder, position);
}
}
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
VH holder;
if (convertView == null) {
int itemType = getItemViewType(position);
holder = onCreateViewHolder(parent, itemType);
holder.mItemViewType = itemType;
convertView = holder.itemView;
convertView.setTag(holder);
} else {
holder = (VH) convertView.getTag();
}
onBindViewHolder(holder, position);
return convertView;
}
protected abstract VH onCreateViewHolder(ViewGroup parent, int viewType);
protected abstract void onBindViewHolder(VH holder, int position);
public static class BaseListViewHolder {
public final View itemView;
int mItemViewType;
public BaseListViewHolder(View itemView) {
this.itemView = itemView;
}
}
}
5、主函数调用
public class OtherAttrSelectFragment extends Fragment implements AdapterView.OnItemClickListener{
private ListView listView;
private ListViewAdapter adapter;
private TreeViewDataSource dataSource;
private Button add,show,cancel;
private EditText text;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_other_attr_select, container, false);
super.onCreateView(inflater, container, savedInstanceState);
//初始化数据
initData();
text = view.findViewById(R.id.attrText);
listView = view.findViewById(R.id.list);
listView.setOnItemClickListener(this); //监听事件
adapter = new ListViewAdapter(); //适配器
listView.setAdapter(adapter);
//添加监听
// add = view.findViewById(R.id.add);
// add.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// add(v);
// }
// });
//显示监听
show = view.findViewById(R.id.show);
show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
show(v);
}
});
//取消监听
cancel = view.findViewById(R.id.cancel);
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//TODO
cancel(v);
}
});
return view;
}
//监听-添加事件
public void add(View view) {
CheckModel node = new CheckModel(); //一级节点
node.name = "手机品牌";
node.child = new ArrayList();
CheckModel node1 = new CheckModel();
node1.name = "Apple";
node1.marginLeft = 100;
node1.child = new ArrayList();
node.child.add(node1);
CheckModel node2 = new CheckModel();
node2.name = "颜色";
node2.marginLeft = 200;
node2.child = new ArrayList();
node1.child.add(node2);
CheckModel node3 = new CheckModel();
node3.name = "黑色";
node3.marginLeft = 300;
node3.child = new ArrayList();
node2.child.add(node3);
CheckModel node4 = new CheckModel();
node4.name = "iPhoneX";
node4.marginLeft = 300;
node3.child.add(node4);
List nodes = dataSource.getNodes();
nodes.add(node);
dataSource.updateNodes();
adapter.notifyDataSetChanged();
}
//监听-显示所选的节点
public void show(View view) {
List<CheckModel> list = getSelectedNodes();
if(list.isEmpty()){
Toast.makeText(getContext(), "选择为空!", Toast.LENGTH_LONG).show();
text.setText("");
}else{
text.setText(list.toString()); //显示获取的值
}
}
//监听-取消选择-已经监听到了
public void cancel(View view){
List<CheckModel> list = getSelectedNodes();
for(CheckModel obj:list){
obj.setSelected(false);
}
dataSource.updateNodes();
adapter.notifyDataSetChanged(); //监听更新
text.setText("");
Toast.makeText(getContext(), "已经取消!", Toast.LENGTH_LONG).show();
}
//遍历获取选中的值
public List<CheckModel> getSelectedNodes() {
List result = new ArrayList();
for (int i = 0; i < dataSource.getElements().size(); i++) {
CheckModel model = (CheckModel) dataSource.getElements().get(i);
if (model.isSelected) {
result.add(model);
}
}
return result;
}
//初始化数据
private void initData() {
//主要list一直add,然后通过递归获取树形图
List list = new ArrayList();
CheckModel node = new CheckModel();
node.name = "*****有限公司";
node.child = new ArrayList();
list.add(node);
CheckModel node1 = new CheckModel();
node1.name = "**中心";
node1.marginLeft = 100;
node1.child = new ArrayList();
node.child.add(node1);
CheckModel node10 = new CheckModel();
node10.name = "**研究";
node10.marginLeft = node1.marginLeft + 100;
node10.child = new ArrayList<>();
node1.child.add(node10);
CheckModel node100 = new CheckModel();
node100.name = "老赵";
node100.marginLeft = node10.marginLeft + 100;
node10.child.add(node100);
CheckModel node11 = new CheckModel();
node11.name = "**部";
node11.marginLeft = node1.marginLeft + 100;
node11.child = new ArrayList<>();
node1.child.add(node11);
CheckModel node110 = new CheckModel();
node110.name = "老李";
node110.marginLeft = node11.marginLeft + 100;
node11.child.add(node110);
CheckModel node12 = new CheckModel();
node12.name = "研发部";
node12.marginLeft = node1.marginLeft + 100;
node12.child = new ArrayList<>();
node1.child.add(node12);
CheckModel node120 = new CheckModel();
node120.name = "小王";
node120.marginLeft = node12.marginLeft + 100;
node12.child.add(node120);
CheckModel node2 = new CheckModel();
node2.name = "**信息中心";
node2.marginLeft = node1.marginLeft;
node2.child = new ArrayList();
node.child.add(node2);
// CheckModel node3 = new CheckModel();
// node3.name = "测试部";
// node3.marginLeft = node1.marginLeft;
// node2.child.add(node3);
CheckModel node21 = new CheckModel();
node21.name = "**部";
node21.marginLeft = node2.marginLeft + 100;
node21.child = new ArrayList<>();
node2.child.add(node21);
CheckModel node210 = new CheckModel();
node210.name = "老王";
node210.marginLeft = node21.marginLeft + 100;
node21.child.add(node210);
// CheckModel node22 = new CheckModel();
// node22.name = "胡萝卜";
// node22.marginLeft = node1.marginLeft;
// node2.child.add(node22);
//
// CheckModel node3 = new CheckModel();
// node3.name = "肉类";
// node3.marginLeft = node1.marginLeft;
// node3.child = new ArrayList();
// node.child.add(node3);
//
// CheckModel node30 = new CheckModel();
// node30.name = "猪肉";
// node30.marginLeft = node1.marginLeft;
// node3.child.add(node30);
//
// CheckModel node31 = new CheckModel();
// node31.name = "鱼肉";
// node31.marginLeft = node1.marginLeft;
// node3.child.add(node31);
//
// CheckModel node32 = new CheckModel();
// node32.name = "牛肉";
// node32.marginLeft = node1.marginLeft;
// node32.child = new ArrayList<>(); //新加
// node3.child.add(node32);
// CheckModel node210 = new CheckModel();
// node210.name = "老王";
// node210.marginLeft = node1.marginLeft;
// node2.child.add(node210);
dataSource = new TreeViewDataSource(list); //接收树形信息
}
//item点击事件
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CheckModel node = (CheckModel) dataSource.getElements().get(position);
node.isExpand = !node.isExpand;
if (node.child == null) {
node.isSelected = !node.isSelected;
adapter.notifyItemChanged(listView, position);
} else {
dataSource.updateNodes();
adapter.notifyDataSetChanged();
}
}
//listView适配器
private class ListViewAdapter extends BaseListViewAdapter<BaseListViewAdapter.BaseListViewHolder> {
//视图初始化
@Override
protected BaseListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//这里不同
View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_item_tree, parent, false);
TreeViewHolder holder = new TreeViewHolder(view);
return holder;
}
//绑定视图
@Override
protected void onBindViewHolder(BaseListViewHolder holder, int position) {
CheckModel node = (CheckModel) getItem(position);
TreeViewHolder treeViewHolder = (TreeViewHolder) holder;
treeViewHolder.textView.setText(node.name);
treeViewHolder.itemView.setPadding(node.marginLeft, 0, 0, 0);
//选择开关
if(node.isSelected){
treeViewHolder.ivSelected.setImageDrawable(getResources().getDrawable((R.drawable.ic_yes)));
}else{
treeViewHolder.ivSelected.setImageDrawable(getResources().getDrawable((R.drawable.ic_no)));
}
//是否展开
if(node.isExpand){
treeViewHolder.ivExpand.setImageDrawable(getResources().getDrawable((R.drawable.arrow_down)));
}else{
treeViewHolder.ivExpand.setImageDrawable(getResources().getDrawable((R.drawable.arrow_right)));
}
if(node.child != null && node.child.size() > 0){
treeViewHolder.ivExpand.setVisibility(View.VISIBLE);
treeViewHolder.ivSelected.setVisibility(View.INVISIBLE);
}else{
treeViewHolder.ivExpand.setVisibility(View.INVISIBLE);
treeViewHolder.ivSelected.setVisibility(View.VISIBLE);
}
}
@Override
public int getCount() {
return dataSource.getElements().size();
}
@Override
public Object getItem(int position) {
return dataSource.getElements().get(position);
}
}
private class TreeViewHolder extends BaseListViewAdapter.BaseListViewHolder{
public TextView textView;
public ImageView ivSelected;
public ImageView ivExpand;
public TreeViewHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.tv);
ivSelected = itemView.findViewById(R.id.iv_selected);
ivExpand = itemView.findViewById(R.id.iv_expand);
}
}
}
三、效果