前几篇详细介绍了二级树形列表的实现,以及通用方法封装,今天就详细介绍一下三级树形列表的基本实现。如果理解了前几篇的二级树形列表实现,那么三级列表实现就简单了。
说直白点就是在二级列表里在嵌套一个二级列表,就简单解决了。不详细在去介绍了,直接上代码吧。
模型:ThreeTreeModel
package com.example.appview.mian_page.Frament.Preject_Tance_Frament.ItemA_Model; import java.util.Date; import java.util.List; public class ThreeTreeModel { private int CaiJiId; private String CaiJiSNO; private List<DianBiao> dianBiaoList; private Date AddTime; public void setCaiJiId(int caiJiId) { CaiJiId = caiJiId; } public void setCaiJiSNO(String caiJiSNO) { CaiJiSNO = caiJiSNO; } public void setDianBiaoList(List<DianBiao> dianBiaoList) { this.dianBiaoList = dianBiaoList; } public void setAddTime(Date addTime) { AddTime = addTime; } public int getCaiJiId() { return CaiJiId; } public String getCaiJiSNO() { return CaiJiSNO; } public List<DianBiao> getDianBiaoList() { return dianBiaoList; } public Date getAddTime() { return AddTime; } public static class DianBiao{ private int DianBiaoId; private String DianBiaoSNO; private List<SheBei> sheBeiList; private Date AddTime; public void setDianBiaoId(int dianBiaoId) { DianBiaoId = dianBiaoId; } public void setDianBiaoSNO(String dianBiaoSNO) { DianBiaoSNO = dianBiaoSNO; } public void setSheBeiList(List<SheBei> sheBeiList) { this.sheBeiList = sheBeiList; } public void setAddTime(Date addTime) { AddTime = addTime; } public int getDianBiaoId() { return DianBiaoId; } public String getDianBiaoSNO() { return DianBiaoSNO; } public List<SheBei> getSheBeiList() { return sheBeiList; } public Date getAddTime() { return AddTime; } } public static class SheBei{ private int SheBeiId; private String SheBeiName; private Date AddTime; public void setSheBeiId(int sheBeiId) { SheBeiId = sheBeiId; } public void setSheBeiName(String sheBeiName) { SheBeiName = sheBeiName; } public void setAddTime(Date addTime) { AddTime = addTime; } public int getSheBeiId() { return SheBeiId; } public String getSheBeiName() { return SheBeiName; } public Date getAddTime() { return AddTime; } } }
一级通用类:ParentAdapterHelper
package com.example.appview.mian_page.Frament.Preject_Tance_Frament.ItemA_Model.Tree; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.BaseExpandableListAdapter; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import com.example.appview.R; import com.example.appview.mian_page.Frament.Preject_Tance_Frament.ItemA_Model.ThreeTreeModel; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.ListIterator; public class ParentAdapterHelper<T1 extends ExpandableListViewInterface,T2 extends ExpandableListViewInterface,T3 extends ExpandableListViewInterface> extends BaseExpandableListAdapter { private Context context; private List<ThreeTreeModel> CaiJiModel;//国家的集合 LayoutInflater layoutInflater; private T1 t1; private T2 t2; private T3 t3; public ChildenAdapterHelper<T2,T3> adapter; public ParentAdapterHelper(Context context, List<ThreeTreeModel> CaiJiModel,T1 t1,T2 t2,T3 t3){ this.context=context; this.CaiJiModel=CaiJiModel; this.t1=t1; this.t2=t2; this.t3=t3; layoutInflater = LayoutInflater.from(context); } //添加一个组元素 public void add(ThreeTreeModel data) { if (CaiJiModel == null) { CaiJiModel = new ArrayList<>(); } CaiJiModel.add(data); notifyDataSetChanged(); } //往特定位置,添加一个组元素 public void add(int position, ThreeTreeModel data) { if (CaiJiModel == null) { CaiJiModel= new ArrayList<>(); } CaiJiModel.add(position, data); notifyDataSetChanged(); } //往特定位置,添加一个组元素 public void addItem(int position,int childenposition, ThreeTreeModel.DianBiao data) { CaiJiModel.get(position).getDianBiaoList().add(data); notifyDataSetChanged(); } //移除当前数据的组件 public void remove(ThreeTreeModel data) { if (CaiJiModel != null) { CaiJiModel.remove(data); } notifyDataSetChanged(); } //移除当前位置的组件 public void remove(int position) { if (CaiJiModel != null) { CaiJiModel.remove(position); } notifyDataSetChanged(); } public void clear() { if (CaiJiModel!= null) { CaiJiModel.clear(); } notifyDataSetChanged(); } // 组数量 @Override public int getGroupCount() { return CaiJiModel.size(); } //子View数量 @Override public int getChildrenCount(int i) { return 1; } //获取组对象 @Override public Object getGroup(int i) { return CaiJiModel.get(i); } //获取子View对象 @Override public Object getChild(int i, int i1) { return CaiJiModel.get(i).getDianBiaoList().get(i1); } // 组View下标 @Override public long getGroupId(int i) { return i; } //子View下标 @Override public long getChildId(int i, int i1) { return i1; } @Override public boolean hasStableIds() { return false; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { convertView= layoutInflater.inflate(R.layout.preject_item_addcaiji_onetree, null); TextView textView = convertView.findViewById(R.id.preject_item_caijiqi_onetree); textView.setText(CaiJiModel.get(groupPosition).getCaiJiSNO()); return t1.getGroupView(groupPosition,convertView); } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { AbsListView.LayoutParams params = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); ChildenExpandableListView childenExpandableListView=new ChildenExpandableListView(context); childenExpandableListView.setLayoutParams(params); adapter = new ChildenAdapterHelper(context,CaiJiModel.get(groupPosition).getDianBiaoList(),t2,t3); childenExpandableListView.setAdapter(adapter); childenExpandableListView.setPadding(30, 5, 0, 5); return childenExpandableListView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return false; } }
接口:ExpandableListViewInterface
package com.example.appview.mian_page.Frament.Preject_Tance_Frament.ItemA_Model.Tree; import android.view.View; import android.view.ViewGroup; public interface ExpandableListViewInterface { public View getGroupView(int groupPosition, View convertView); public View getChildView(int groupPosition, int childPosition, View convertView); }
二级:ChildenAdapterHelper
package com.example.appview.mian_page.Frament.Preject_Tance_Frament.ItemA_Model.Tree; import android.app.Activity; import android.content.Context; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.TextView; import com.example.appview.R; import com.example.appview.mian_page.Frament.Preject_Tance_Frament.ItemA_Model.ThreeTreeModel; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TransferQueue; public class ChildenAdapterHelper<T1 extends ExpandableListViewInterface,T2 extends ExpandableListViewInterface> extends BaseExpandableListAdapter { private Context context; private List<ThreeTreeModel.DianBiao> dianBiaoList;//电表集合 LayoutInflater layoutInflater; private T1 t1; private T2 t2; //构造参数 public ChildenAdapterHelper(Context context,List<ThreeTreeModel.DianBiao> dianBiaoList,T1 t1,T2 t2) { this.context=context; this.dianBiaoList=dianBiaoList; this.t1=t1; this.t2=t2; layoutInflater = LayoutInflater.from(context); } //添加一个组元素 public void add(ThreeTreeModel.DianBiao data) { if (dianBiaoList == null) { dianBiaoList = new ArrayList<>(); } dianBiaoList.add(data); notifyDataSetChanged(); } //往特定位置,添加一个组元素 public void add(int position, ThreeTreeModel.DianBiao data) { if (dianBiaoList == null) { dianBiaoList= new ArrayList<>(); } dianBiaoList.add(position, data); notifyDataSetChanged(); } //移除当前数据的组件 public void remove(ThreeTreeModel.DianBiao data) { if (dianBiaoList != null) { dianBiaoList.remove(data); } notifyDataSetChanged(); } //移除当前位置的组件 public void remove(int position) { if (dianBiaoList != null) { dianBiaoList.remove(position); } notifyDataSetChanged(); } //往特定位置,添加一个子元素 public void addItem(int position, ThreeTreeModel.SheBei data) { dianBiaoList.get(position).getSheBeiList().add(data); notifyDataSetChanged(); } //移除当前位置的组件 public void removeItem(int position,int childenposition) { if (dianBiaoList.get(position).getSheBeiList() != null) { dianBiaoList.get(position).getSheBeiList().remove(childenposition); } notifyDataSetChanged(); } //获取外层组个数 @Override public int getGroupCount() { return dianBiaoList.size(); } //获取内层组个数 @Override public int getChildrenCount(int i) { return dianBiaoList.get(i).getSheBeiList().size(); } //获取外层组 @Override public Object getGroup(int i) { return dianBiaoList.get(i); } //获取内层数据 @Override public Object getChild(int i, int i1) { return dianBiaoList.get(i).getSheBeiList().get(i1); } //组下标 @Override public long getGroupId(int i) { return i; } //子View下标 @Override public long getChildId(int i, int i1) { return i1; } //不知道做什么用 @Override public boolean hasStableIds() { return false; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { convertView= layoutInflater.inflate(R.layout.preject_item_addcaiji_twotree, null); TextView textView = convertView.findViewById(R.id.preject_item_dianbiao); convertView.setPadding(10,5,0,5); textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT); textView.setText(dianBiaoList.get(groupPosition).getDianBiaoSNO()); return t1.getGroupView(groupPosition,convertView); } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { convertView= layoutInflater.inflate(R.layout.preject_item_addcaiji_threetree, null); TextView textView = convertView.findViewById(R.id.preject_item_shebeiname); convertView.setPadding(30,5,0,5); textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT); textView.setText(dianBiaoList.get(groupPosition).getSheBeiList().get(childPosition).getSheBeiName()); return t2.getChildView(groupPosition,childPosition,convertView); } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return false; } }
自适应高度类:ChildenExpandableListView
package com.example.appview.mian_page.Frament.Preject_Tance_Frament.ItemA_Model.Tree; import android.content.Context; import android.util.AttributeSet; import android.widget.ExpandableListView; public class ChildenExpandableListView extends ExpandableListView { public ChildenExpandableListView(Context context) { super(context); } public ChildenExpandableListView(Context context, AttributeSet attrs) { super(context, attrs); } public ChildenExpandableListView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } //通过此方法重新计算expandableListView的高度,使得里边的expandableListView全部显示 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2 , MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }
实现类:
package com.example.appview.mian_page.Frament.Preject_Tance_Frament; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.GradientDrawable; import android.os.Build; import android.os.Bundle; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.PopupWindow; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import androidx.fragment.app.Fragment; import com.example.appview.Common.AdapterHelper; import com.example.appview.Common.AdapterInterface; import com.example.appview.MainActivity; import com.example.appview.Model.main_menu.Preject_ItemModel; import com.example.appview.R; import com.example.appview.mian_page.Activity.LoginMainPage; import com.example.appview.mian_page.Frament.Preject_Tance_Frament.ItemA_Activity.Preject_ItemA_AlterJZ; import com.example.appview.mian_page.Frament.Preject_Tance_Frament.ItemA_Activity.Preject_Item_AddJL; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * 探测待处理页面 */ public class Preject_ItemA_Frament extends Fragment { private List<Preject_ItemModel> preject_itemModels; private AdapterHelper<Preject_ItemModel,NoReceive> adapteritem; private ListView listView; public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.preject_item1_frament, container, false); listView=view.findViewById(R.id.preject_item1_listview); preject_itemModels=new ArrayList<>(); preject_itemModels=getPreject_itemModels(); adapteritem=new AdapterHelper<>(getContext(),preject_itemModels,new NoReceive(),R.layout.maingrid_layout4); listView.setAdapter(adapteritem); return view; } private List<Preject_ItemModel> getPreject_itemModels(){ List<Preject_ItemModel> preject_itemModels=new ArrayList<>(); preject_itemModels.add(new Preject_ItemModel(1,"XX端局",1,1,"待接收", new Date(System.currentTimeMillis()))); preject_itemModels.add(new Preject_ItemModel(2,"XX支局",2,2,"待接收", new Date(System.currentTimeMillis()))); preject_itemModels.add(new Preject_ItemModel(3,"XX基站",3,1,"待接收", new Date(System.currentTimeMillis()))); preject_itemModels.add(new Preject_ItemModel(4,"XX一体化",4,1,"待接收", new Date(System.currentTimeMillis()))); return preject_itemModels; } @Override public void onHiddenChanged(boolean hidden) { super.onHiddenChanged(hidden); if(hidden){ //TODO now visible to user // Toast.makeText(getContext(), "你离开了该页面", Toast.LENGTH_SHORT).show(); } else { //TODO now invisible to user preject_itemModels.add(new Preject_ItemModel(5,"XX宏基站",4,1,"待接收", new Date(System.currentTimeMillis()))); adapteritem=new AdapterHelper<>(getContext(),preject_itemModels,new NoReceive(),R.layout.maingrid_layout4); listView.setAdapter(adapteritem); } } class NoReceive implements AdapterInterface{ @Override public View getView(View view, int position){ ImageView imageView1=view.findViewById(R.id.preject_item_jztype); TextView textView1=view.findViewById(R.id.preject_item_jzname); TextView textView2=view.findViewById(R.id.preject_item_jzstate); TextView textView3=view.findViewById(R.id.preject_item_addtime); ImageView imageView2=view.findViewById(R.id.preject_item_jzcaozuo); LinearLayout linearLayout=view.findViewById(R.id.preject_item_layoutcaozuo); textView1.setText(adapteritem.list.get(position).getJZ_Name()); textView2.setText(adapteritem.list.get(position).getJZ_StateName()); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// HH:mm:ss textView3.setText(simpleDateFormat.format(adapteritem.list.get(position).getAddTime())); if (adapteritem.list.get(position).getJZ_TypeId()==1){ imageView1.setImageResource(R.mipmap.duanju); } if (adapteritem.list.get(position).getJZ_TypeId()==2){ imageView1.setImageResource(R.mipmap.zhiju); } if (adapteritem.list.get(position).getJZ_TypeId()==3){ imageView1.setImageResource(R.mipmap.jizhan); } if (adapteritem.list.get(position).getJZ_TypeId()==4){ imageView1.setImageResource(R.mipmap.qita); } if (adapteritem.list.get(position).getJZ_State()==1){ textView2.setTextColor(getResources().getColor(R.color.preject_item1)); imageView2.setImageResource(R.mipmap.preject_item_caozuo); } if (adapteritem.list.get(position).getJZ_State()==2){ textView2.setTextColor(getResources().getColor(R.color.preject_item2)); imageView2.setImageResource(R.mipmap.preject_itema_alter); } if (adapteritem.list.get(position).getJZ_State()==3){ textView2.setTextColor(getResources().getColor(R.color.preject_item3)); } imageView2.setOnClickListener(new View.OnClickListener() { @RequiresApi(api = Build.VERSION_CODES.KITKAT) @Override public void onClick(View v) { View popupView = View.inflate(getContext(), R.layout.preject_item_pop, null); // PopupWindowFactory.getPopupWindowInCenter(popupView,ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT,linearLayout); getScreen(linearLayout,position); } }); return view; } } @RequiresApi(api = Build.VERSION_CODES.KITKAT) private void getScreen(LinearLayout linearLayout,int position){ if (adapteritem.list.get(position).getJZ_State()==1){ getNoreceive(linearLayout,position); } if (adapteritem.list.get(position).getJZ_State()==2){ getAlter(linearLayout,position); } } private void getAlter(LinearLayout linearLayout,int position){ View popupView = View.inflate(getContext(), R.layout.preject_item_pop_alter, null); Button btn1=popupView.findViewById(R.id.preject_item_alter_btn1); Button btn2=popupView.findViewById(R.id.preject_item_alter_btn2); if (adapteritem.list.get(position).getJZ_TypeId()==3){ btn1.setText("添加采集器"); }else{ btn1.setText("添加机楼"); } GradientDrawable drawable1 =(GradientDrawable)btn1.getBackground(); drawable1.setColor(getResources().getColor(R.color.preject4)); GradientDrawable drawable2 =(GradientDrawable)btn2.getBackground(); drawable2.setColor(getResources().getColor(R.color.preject3)); PopupWindow popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setFocusable(true); popupWindow.setOutsideTouchable(true); popupWindow.setAnimationStyle(R.anim.move_pop_left_in); popupWindow.setBackgroundDrawable(new BitmapDrawable()); int popupWidth = popupView.getMeasuredWidth(); int popupHeight = popupView.getMeasuredHeight(); int[] location = new int[2]; linearLayout.getLocationOnScreen(location); popupWindow.showAtLocation(linearLayout, Gravity.NO_GRAVITY, location[0], location[1]); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { popupWindow.dismiss(); Intent i = new Intent(getContext(), Preject_Item_AddJL.class); startActivity(i); Toast.makeText(getContext(), "添加机楼", Toast.LENGTH_SHORT).show(); } }); btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { popupWindow.dismiss(); Intent i = new Intent(getContext(), Preject_ItemA_AlterJZ.class); startActivity(i); // Toast.makeText(getContext(), "编辑站点", Toast.LENGTH_SHORT).show(); } }); } private void getNoreceive(LinearLayout linearLayout,int position){ View popupView = View.inflate(getContext(), R.layout.preject_item_pop, null); Button btn1=popupView.findViewById(R.id.preject_itemA_btn1); Button btn2=popupView.findViewById(R.id.preject_itemA_btn2); Button btn3=popupView.findViewById(R.id.preject_itemA_btn3); GradientDrawable drawable1 =(GradientDrawable)btn1.getBackground(); drawable1.setColor(getResources().getColor(R.color.preject4)); GradientDrawable drawable2 =(GradientDrawable)btn2.getBackground(); drawable2.setColor(getResources().getColor(R.color.preject3)); GradientDrawable drawable3 =(GradientDrawable)btn3.getBackground(); drawable3.setColor(getResources().getColor(R.color.preject2)); PopupWindow popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setFocusable(true); popupWindow.setOutsideTouchable(true); popupWindow.setAnimationStyle(R.anim.move_pop_left_in); popupWindow.setBackgroundDrawable(new BitmapDrawable()); int popupWidth = popupView.getMeasuredWidth(); int popupHeight = popupView.getMeasuredHeight(); int[] location = new int[2]; linearLayout.getLocationOnScreen(location); popupWindow.showAtLocation(linearLayout, Gravity.NO_GRAVITY, location[0], location[1]); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { popupWindow.dismiss(); Toast.makeText(getContext(), "转派", Toast.LENGTH_SHORT).show(); ShowChoise(); } }); btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { popupWindow.dismiss(); Toast.makeText(getContext(), "接收", Toast.LENGTH_SHORT).show(); } }); btn3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { popupWindow.dismiss(); Toast.makeText(getContext(), "拒绝", Toast.LENGTH_SHORT).show(); } }); } private void getReceive(){ } private void ShowChoise() { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),android.R.style.Theme_Holo_Light_Dialog); //builder.setIcon(R.drawable.ic_launcher); builder.setTitle("请选择指派人"); // 指定下拉列表的显示数据 final String[] cities = {"张三", "李四", "王二","麻子"}; // 设置一个下拉的列表选择项 builder.setItems(cities, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getActivity(), "选择的指派人为:" + cities[which], Toast.LENGTH_SHORT).show(); } }); builder.show(); } }
其它的布局页面就不多说了,理解树形列表的话,这个可以自己定义了。下面是实现效果。
树形列表到这里基本就结束了,三级树形列表还没有做封装,这个可能要放到以后去做了。目前就先介绍到这里。