Android 多級樹形結構顯示
1.專案截圖
實體類1
MyNodeBean
public class MyNodeBean {
private String ids;
private String pIds;
/**
* 節點Id
*/
private int id;
/**
* 節點父id
*/
private int pId;
/**
* 節點name
*/
private String name;
/**
*
*/
private String desc;
/**
* 節點名字長度
*/
private long length;
public MyNodeBean(int id, int pId, String ids,String pIds,String name) {
super();
this.id = id;
this.pId = pId;
this.ids=ids;
this.pIds=pIds;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPid() {
return pId;
}
public void setPid(int pId) {
this.pId = pId;
}
public String getIds() {
return ids;
}
public void setIds(String ids) {
this.ids = ids;
}
public String getpIds() {
return pIds;
}
public void setpIds(String pIds) {
this.pIds = pIds;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public long getLength() {
return length;
}
public void setLength(long length) {
this.length = length;
}
}
實體2
Node
public class Node {
private String ids;
private String pIds;
/**
* 節點id
*/
private int id;
/**
* 父節點id
*/
private int pId;
/**
* 是否展開
*/
private boolean isExpand = false;
private boolean isChecked = false;
private boolean isHideChecked = true;
/**
* 節點名字
*/
private String name;
/**
* 節點級別
*/
private int level;
/**
* 節點展示圖示
*/
private int icon;
/**
* 節點所含的子節點
*/
private List<Node> childrenNodes = new ArrayList<Node>();
/**
* 節點的父節點
*/
private Node parent;
public Node() {
}
public Node(int id, int pId, String ids,String pIds,String name) {
super();
this.id = id;
this.pId = pId;
this.ids=ids;
this.pIds=pIds;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getpId() {
return pId;
}
public void setpId(int pId) {
this.pId = pId;
}
public String getIds() {
return ids;
}
public void setIds(String ids) {
this.ids = ids;
}
public String getpIds() {
return pIds;
}
public void setpIds(String pIds) {
this.pIds = pIds;
}
public boolean isExpand() {
return isExpand;
}
/**
* 當父節點收起,其子節點也收起
* @param isExpand
*/
public void setExpand(boolean isExpand) {
this.isExpand = isExpand;
if (!isExpand) {
for (Node node : childrenNodes) {
node.setExpand(isExpand);
}
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getLevel() {
return parent == null ? 0 : parent.getLevel() + 1;
}
public void setLevel(int level) {
this.level = level;
}
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
public List<Node> getChildrenNodes() {
return childrenNodes;
}
public void setChildrenNodes(List<Node> childrenNodes) {
this.childrenNodes = childrenNodes;
}
public Node getParent() {
return parent;
}
public void setParent(Node parent) {
this.parent = parent;
}
/**
* 判斷是否是根節點
*
* @return
*/
public boolean isRoot() {
return parent == null;
}
/**
* 判斷是否是葉子節點
*
* @return
*/
public boolean isLeaf() {
return childrenNodes.size() == 0;
}
/**
* 判斷父節點是否展開
*
* @return
*/
public boolean isParentExpand()
{
if (parent == null)
return false;
return parent.isExpand();
}
public boolean isChecked() {
return isChecked;
}
public void setChecked(boolean isChecked) {
this.isChecked = isChecked;
}
public boolean isHideChecked() {
return isHideChecked;
}
public void setHideChecked(boolean isHideChecked) {
this.isHideChecked = isHideChecked;
}
}
介面卡1
TreeListViewAdapter
public abstract class TreeListViewAdapter<T> extends BaseAdapter {
protected Context mContext;
/**
* 儲存所有可見的Node
*/
protected List<Node> mNodes;
protected LayoutInflater mInflater;
/**
* 儲存所有的Node
*/
protected List<Node> mAllNodes;
/**
* 點選的回撥介面
*/
private OnTreeNodeClickListener onTreeNodeClickListener;
public interface OnTreeNodeClickListener {
/**
* 處理node click事件
* @param node
* @param position
*/
void onClick(Node node, int position);
/**
* 處理checkbox選擇改變事件
* @param node
* @param position
* @param checkedNodes
*/
void onCheckChange(Node node, int position,List<Node> checkedNodes);
}
public void setOnTreeNodeClickListener(
OnTreeNodeClickListener onTreeNodeClickListener) {
this.onTreeNodeClickListener = onTreeNodeClickListener;
}
/**
*
* @param mTree
* @param context
* @param datas
* @param defaultExpandLevel
* 預設展開幾級樹
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public TreeListViewAdapter(ListView mTree, Context context, List<T> datas,
int defaultExpandLevel, boolean isHide)
throws IllegalArgumentException, IllegalAccessException {
mContext = context;
/**
* 對所有的Node進行排序
*/
mAllNodes = TreeHelper
.getSortedNodes(datas, defaultExpandLevel, isHide);
/**
* 過濾出可見的Node
*/
mNodes = TreeHelper.filterVisibleNode(mAllNodes);
mInflater = LayoutInflater.from(context);
/**
* 設定節點點選時,可以展開以及關閉;並且將ItemClick事件繼續往外公佈
*/
mTree.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
expandOrCollapse(position);
if (onTreeNodeClickListener != null) {
onTreeNodeClickListener.onClick(mNodes.get(position),
position);
}
}
});
}
/**
* 相應ListView的點選事件 展開或關閉某節點
*
* @param position
*/
public void expandOrCollapse(int position) {
Node n = mNodes.get(position);
if (n != null)// 排除傳入引數錯誤異常
{
if (!n.isLeaf()) {
n.setExpand(!n.isExpand());
mNodes = TreeHelper.filterVisibleNode(mAllNodes);
notifyDataSetChanged();// 重新整理檢視
}
}
}
@Override
public int getCount() {
return mNodes.size();
}
@Override
public Object getItem(int position) {
return mNodes.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Node node = mNodes.get(position);
convertView = getConvertView(node, position, convertView, parent);
// 設定內邊距
convertView.setPadding(node.getLevel() * 30, 3, 3, 3);
if (!node.isHideChecked()) {
//獲取各個節點所在的父佈局
RelativeLayout myView = (RelativeLayout) convertView;
//父佈局下的CheckBox
CheckBox cb = (CheckBox) myView.getChildAt(1);
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
TreeHelper.setNodeChecked(node, isChecked);
List<Node> checkedNodes = new ArrayList<Node>();
for(Node n:mAllNodes){
if(n.isChecked()){
checkedNodes.add(n);
}
}
onTreeNodeClickListener.onCheckChange(node,position,checkedNodes);
TreeListViewAdapter.this.notifyDataSetChanged();
}
});
}
return convertView;
}
public abstract View getConvertView(Node node, int position, View convertView, ViewGroup parent);
/**
* 更新
* @param isHide
*/
public void updateView(boolean isHide){
for(Node node:mAllNodes){
node.setHideChecked(isHide);
}
this.notifyDataSetChanged();
}
}
介面卡2
MyTreeListViewAdapter
public class MyTreeListViewAdapter<T> extends TreeListViewAdapter<T> {
public MyTreeListViewAdapter(ListView mTree, Context context, List<T> datas, int defaultExpandLevel, boolean isHide) throws IllegalArgumentException, IllegalAccessException {
super(mTree, context, datas, defaultExpandLevel, isHide);
}
@Override
public View getConvertView(Node node, int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.icon = (ImageView) convertView.findViewById(R.id.id_treenode_icon);
viewHolder.label = (TextView) convertView.findViewById(R.id.id_treenode_name);
viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.id_treeNode_check);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
if (node.getIcon() == -1) {
viewHolder.icon.setVisibility(View.INVISIBLE);
} else {
viewHolder.icon.setVisibility(View.VISIBLE);
viewHolder.icon.setImageResource(node.getIcon());
}
if(node.isLeaf()){
if (node.isHideChecked()) {
viewHolder.checkBox.setVisibility(View.INVISIBLE);
} else {
viewHolder.checkBox.setVisibility(View.VISIBLE);
setCheckBoxBg(viewHolder.checkBox, node.isChecked());
}
}else{//父節點不顯示選擇框
viewHolder.checkBox.setVisibility(View.GONE);
}
viewHolder.label.setText(node.getName());
return convertView;
}
/**
* ViewHolder類
* */
private final class ViewHolder {
private ImageView icon;
private TextView label;
private CheckBox checkBox;
}
/**
* checkbox是否選中
*/
private void setCheckBoxBg(CheckBox cb, boolean isChecked) {
if (isChecked) {
cb.setBackgroundResource(R.drawable.check_box_bg_check);
} else {
cb.setBackgroundResource(R.drawable.check_box_bg);
}
}
}
幫助類
TreeHelper
public class TreeHelper {
/**
* 根據所有節點獲取可見節點
*
* @param allNodes
* @return
*/
public static List<Node> filterVisibleNode(List<Node> allNodes) {
List<Node> visibleNodes = new ArrayList<Node>();
for (Node node : allNodes) {
// 如果為根節點,或者上層目錄為展開狀態
if (node.isRoot() || node.isParentExpand()) {
setNodeIcon(node);
visibleNodes.add(node);
}
}
return visibleNodes;
}
/**
* 獲取排序的所有節點
*
* @param datas
* @param defaultExpandLevel
* @return
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public static <T> List<Node> getSortedNodes(List<T> datas, int defaultExpandLevel, boolean isHide) throws IllegalAccessException, IllegalArgumentException {
List<Node> sortedNodes = new ArrayList<Node>();
// 將使用者資料轉化為List<Node>
List<Node> nodes = convertData2Nodes(datas, isHide);
// 拿到根節點
List<Node> rootNodes = getRootNodes(nodes);
// 排序以及設定Node間關係
for (Node node : rootNodes) {
addNode(sortedNodes, node, defaultExpandLevel, 1);
}
return sortedNodes;
}
/**
* 把一個節點上的所有的內容都掛上去
*/
private static void addNode(List<Node> nodes, Node node, int defaultExpandLeval, int currentLevel) {
nodes.add(node);
if (defaultExpandLeval >= currentLevel) {
node.setExpand(true);
}
if (node.isLeaf())
return;
for (int i = 0; i < node.getChildrenNodes().size(); i++) {
addNode(nodes, node.getChildrenNodes().get(i), defaultExpandLeval, currentLevel + 1);
}
}
/**
* 獲取所有的根節點
*
* @param nodes
* @return
*/
public static List<Node> getRootNodes(List<Node> nodes) {
List<Node> rootNodes = new ArrayList<Node>();
for (Node node : nodes) {
if (node.isRoot()) {
rootNodes.add(node);
}
}
return rootNodes;
}
/**
* 將泛型datas轉換為node
*
* @param datas
* @return
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public static <T> List<Node> convertData2Nodes(List<T> datas, boolean isHide)
throws IllegalAccessException, IllegalArgumentException {
List<Node> nodes = new ArrayList<Node>();
Node node = null;
for (T t : datas) {
int id = -1;
int pId = -1;
String ids=null;
String pids=null;
String name = null;
Class<? extends Object> clazz = t.getClass();
Field[] declaredFields = clazz.getDeclaredFields();
/**
* 與MyNodeBean實體一一對應
*/
for (Field f : declaredFields) {
if ("id".equals(f.getName())) {
f.setAccessible(true);
id = f.getInt(t);
}
if ("pId".equals(f.getName())) {
f.setAccessible(true);
pId = f.getInt(t);
}
if ("ids".equals(f.getName())) {
f.setAccessible(true);
ids = (String) f.get(t);
}
if ("pIds".equals(f.getName())) {
f.setAccessible(true);
pids = (String) f.get(t);
}
if ("name".equals(f.getName())) {
f.setAccessible(true);
name = (String) f.get(t);
}
if ("desc".equals(f.getName())) {
continue;
}
if ("length".equals(f.getName())) {
continue;
}
if (id == -1 && pId == -1 && ids==null&& pids==null&&name == null) {
break;
}
}
node = new Node(id, pId,ids,pids, name);
node.setHideChecked(isHide);
nodes.add(node);
}
/**
* 比較nodes中的所有節點,分別新增children和parent
*/
for (int i = 0; i < nodes.size(); i++) {
Node n = nodes.get(i);
for (int j = i + 1; j < nodes.size(); j++) {
Node m = nodes.get(j);
if (n.getId() == m.getpId()) {
n.getChildrenNodes().add(m);
m.setParent(n);
} else if (n.getpId() == m.getId()) {
n.setParent(m);
m.getChildrenNodes().add(n);
}
}
}
for (Node n : nodes) {
setNodeIcon(n);
}
return nodes;
}
/**
* 設定開啟,關閉icon
*
* @param node
*/
public static void setNodeIcon(Node node) {
if (node.getChildrenNodes().size() > 0 && node.isExpand()) {
node.setIcon(R.drawable.tree_expand);
} else if (node.getChildrenNodes().size() > 0 && !node.isExpand()) {
node.setIcon(R.drawable.tree_econpand);
} else
node.setIcon(-1);
}
public static void setNodeChecked(Node node, boolean isChecked) {
// 自己設定是否選擇
node.setChecked(isChecked);
/**
* 非葉子節點,子節點處理
*/
setChildrenNodeChecked(node, isChecked);
/** 父節點處理 */
setParentNodeChecked(node);
}
/**
* 非葉子節點,子節點處理
*/
private static void setChildrenNodeChecked(Node node, boolean isChecked) {
node.setChecked(isChecked);
if (!node.isLeaf()) {
for (Node n : node.getChildrenNodes()) {
// 所有子節點設定是否選擇
setChildrenNodeChecked(n, isChecked);
}
}
}
/**
* 設定父節點選擇
*
* @param node
*/
private static void setParentNodeChecked(Node node) {
/** 非根節點 */
if (!node.isRoot()) {
Node rootNode = node.getParent();
boolean isAllChecked = true;
for (Node n : rootNode.getChildrenNodes()) {
if (!n.isChecked()) {
isAllChecked = false;
break;
}
}
if (isAllChecked) {
rootNode.setChecked(true);
} else {
rootNode.setChecked(false);
}
setParentNodeChecked(rootNode);
}
}
}
activity_treeview
<LinearLayout 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"
android:orientation="vertical">
<include layout="@layout/apptoplayout_withtextview" />
<RelativeLayout
android:id="@+id/activity_treeview_sellayout"
android:layout_width="match_parent"
android:visibility="gone"
android:layout_height="wrap_content">
<TextView
android:id="@+id/activity_treeview_seltextview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:gravity="center|left"
android:lineSpacingExtra="5dp"
android:text="已選擇:備件或人工裝置備件或人工裝置"
android:textColor="@color/textviewcolor_333333"
android:textSize="14sp"
android:visibility="visible" />
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_below="@+id/activity_treeview_seltextview"
android:layout_marginTop="10dp"
android:background="@color/appline_color" />
</RelativeLayout>
<ListView
android:id="@+id/activity_treeview_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@color/appline_color"
android:dividerHeight="0.5dp"
android:scrollbars="none"
android:visibility="visible" />
<TextView
android:id="@+id/activity_treeview_textview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="@color/textviewcolor_696969"
android:textSize="14sp"
android:visibility="gone" />
</LinearLayout>
list_item
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp">
<ImageView
android:id="@+id/id_treenode_icon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:layout_marginLeft="10dp"
android:scaleType="fitCenter"
android:src="@drawable/tree_econpand" />
<CheckBox
android:id="@+id/id_treeNode_check"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_centerInParent="true"
android:layout_marginLeft="10dp"
android:visibility="visible"
android:layout_toRightOf="@id/id_treenode_icon"
android:background="@drawable/check_box_bg"
android:button="@null"
android:focusable="false" />
<TextView
android:id="@+id/id_treenode_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@id/id_treeNode_check"
android:text="備件或人工裝置備件或人工裝置備件或人工裝置備件或人工裝置備件或人工裝置備件或人工裝置"
android:singleLine="true"
android:ellipsize="end"
android:textColor="@color/textviewcolor_696969"
android:textSize="14sp" />
</RelativeLayout>
TreeViewActivity
public class TreeViewActivity extends BaseActivity implements View.OnClickListener,TreeListViewAdapter.OnTreeNodeClickListener{
private RelativeLayout backlayout;
private TextView titletextview;
private TextView suretextview;
private List<String> list;
private List<String> showlist;
private String result;
private String showresult;
private HashMap<String,Integer> idhashmap;
private HashMap<String,Integer> pIdhashmap;
private ToastUtils toast;
private RelativeLayout sellayout;
private TextView seltextview;
private TextView nodatatextview;
private ListView treeLv;
private MyTreeListViewAdapter<MyNodeBean> adapter;
private List<MyNodeBean> mDatas = new ArrayList<MyNodeBean>();
private List<MyNodeBean> mDatass = new ArrayList<MyNodeBean>();
private boolean isHide = false; //標記是顯示Checkbox還是隱藏
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_treeview);
initView();
}
/**
* 初始化各種View
* */
private void initView(){
list=new ArrayList<>();
showlist=new ArrayList<>();
toast=new ToastUtils(this);
backlayout= (RelativeLayout) findViewById(R.id.apptoplayout_withtextview_backlayout);
titletextview= (TextView) findViewById(R.id.apptoplayout_withtextview_titletextview);
suretextview= (TextView) findViewById(R.id.apptoplayout_withtextview_righttextview);
treeLv = (ListView) this.findViewById(R.id.activity_treeview_listview);
nodatatextview= (TextView) findViewById(R.id.activity_treeview_textview);
sellayout= (RelativeLayout) findViewById(R.id.activity_treeview_sellayout);
seltextview= (TextView) findViewById(R.id.activity_treeview_seltextview);
backlayout.setOnClickListener(this);
suretextview.setOnClickListener(this);
suretextview.setVisibility(View.INVISIBLE);
sellayout.setVisibility(View.GONE);
titletextview.setText(R.string.activity_treeviewtextview1);
suretextview.setText(R.string.activity_treeviewtextview2);
getSparePartsData();//獲取備件資料
}
/**
* 請求備件資料
* */
private void getSparePartsData(){
//臨時資料
String context="[\n" +
// " \n" +
// " {\n" +
// " \"id\": \"002\",\n" +
// " \"pId\": \"001\",\n" +
// " \"name\": \"NUM2\"\n" +
// " },\n" +
// "\t{\n" +
// " \"id\": \"003\",\n" +
// " \"pId\": \"002\",\n" +
// " \"name\": \"NUM3\"\n" +
// " },\n" +
// "\t{\n" +
// " \"id\": \"004\",\n" +
// " \"pId\": \"003\",\n" +
// " \"name\": \"NUM4\"\n" +
// " },\n" +
// "\t{\n" +
// " \"id\": \"005\",\n" +
// " \"pId\": \"004\",\n" +
// " \"name\": \"NUM5\"\n" +
// " },\n" +
// "\t{\n" +
// " \"id\": \"001\",\n" +
// " \"pId\": \"100\",\n" +
// " \"name\": \"NUM1\"\n" +
// " }\n" +
// "\t\n" +
// "]";
parseSparePartsData(context);
}
/**
* Json解析備件資料
* */
private void parseSparePartsData(String context){
try {
JSONArray array=new JSONArray(context);
int num=array.length();
JSONObject object=null;
MyNodeBean myNodeBean=null;
idhashmap=new HashMap<>();
pIdhashmap=new HashMap<>();
for(int i=0;i<num;i++){
object= (JSONObject) array.opt(i);
String idstr=object.getString("id");
String pidstr=object.getString("pId");
String name=object.getString("name");
myNodeBean=new MyNodeBean(0,0,idstr,pidstr,name);
mDatass.add(myNodeBean);
//將集合中的資料按pidstr倒序排序
ComparatorTreeView comparatorTreeView=new ComparatorTreeView();
Collections.sort(mDatass, comparatorTreeView);
}
} catch (JSONException e) {
e.printStackTrace();
}
int nums=mDatass.size();
if(nums>0){
for(int i=0;i<nums;i++){
MyNodeBean myNodeBean=mDatass.get(i);
String idstr=myNodeBean.getIds();
String pidstr=myNodeBean.getpIds();
String name=myNodeBean.getName();
/**
* 演算法1
* */
if(!BooleanUtils.isEmpty(idstr)&&!BooleanUtils.isEmpty(pidstr)){
//Id 在Pid集合中找
if(pIdhashmap.containsKey(idstr)){
Integer value=pIdhashmap.get(idstr);
idhashmap.put(idstr,value);
}else{
idhashmap.put(idstr,(i+1));
}
//Pid 在Id集合中找
if(idhashmap.containsKey(pidstr)){
Integer value=idhashmap.get(pidstr);
pIdhashmap.put(pidstr,value);
}else{
pIdhashmap.put(pidstr,(i+1));
}
}
int id=idhashmap.get(idstr);
int pid=pIdhashmap.get(pidstr);
/**
* 演算法2
* */
// int id=0;
// int pid=0;
// if(!(BooleanUtils.isEmpty(idstr))&&BooleanUtils.isNum(idstr)){
// id=Integer.parseInt(idstr);
// }
// if(!(BooleanUtils.isEmpty(pidstr))&&BooleanUtils.isNum(pidstr)){
// pid=Integer.parseInt(pidstr);
// }
myNodeBean=new MyNodeBean(id,pid,idstr,pidstr,name);
mDatas.add(myNodeBean);
}
}
if(mDatas.size()>0){//有資料
treeLv.setVisibility(View.VISIBLE);
nodatatextview.setVisibility(View.GONE);
try {
adapter = new MyTreeListViewAdapter<MyNodeBean>(treeLv, this, mDatas, 2, isHide);
treeLv.setAdapter(adapter);
adapter.setOnTreeNodeClickListener(this);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}else{//無資料
treeLv.setVisibility(View.GONE);
nodatatextview.setVisibility(View.VISIBLE);
nodatatextview.setText(R.string.activity_treeviewtextview3);
}
}
/**
* TreeView點選
* */
@Override
public void onClick(Node node, int position) {
if (node.isLeaf()) {//葉子節點
node.setHideChecked(false);//子節點始終不隱藏選擇框
String id=node.getIds();//NodeId
String name=node.getName();//NodeName
Node fathernode=node.getParent();//父Node
String fathername="";
if(null!=fathernode){//逐級獲取父節點的NodeName
boolean b=!fathernode.isLeaf();
while (b){
fathername=fathernode.getName()+"-"+fathername;
fathernode=fathernode.getParent();
if(null!=fathernode){
b=!fathernode.isLeaf();
}else{
b=false;
}
}
}
//獲取點選的子節點結果(帶id傳給伺服器)和子節點結果(不帶id前臺顯示)
result=fathername+name+","+id;
showresult=fathername+name;
//點選的子節點選中狀態
if(node.isChecked()){//選擇框已選擇 取消選擇 並將結果刪除
node.setChecked(false);
list.remove(result);
showlist.remove(showresult);
}else{//選擇框未選擇 選中 並將結果新增
node.setChecked(true);
list.add(result);
showlist.add(showresult);
}
if(list.size()>0&&showlist.size()>0&&list.size()==showlist.size()){
suretextview.setVisibility(View.VISIBLE);
sellayout.setVisibility(View.VISIBLE);
//顯示結果
int num=showlist.size();
String result="";
for(int i=0;i<num;i++){
result=result+showlist.get(i)+";";
}
seltextview.setText("已選擇:"+result);
//上傳伺服器
int nums=list.size();
String results="";
for(int j=0;j<nums;j++){
results=results+list.get(j)+";";
}
toast.showToast(results);
}else{
suretextview.setVisibility(View.INVISIBLE);
sellayout.setVisibility(View.GONE);
seltextview.setText("");
}
adapter.notifyDataSetChanged();
}
}
/**
* TreeView選中
* */
@Override
public void onCheckChange(Node node, int position, List<Node> checkedNodes) {
// //首先以前選中的全部取消選中
// int num=checkedNodes.size();
// for(int i=0;i<num;i++){
// Node nodes=checkedNodes.get(i);
// nodes.setHideChecked(false);
// nodes.setChecked(false);
// }
// //獲取當前選中的節點
// if (node.isLeaf()) {//葉子節點
// node.setHideChecked(false);
// node.setChecked(true);
// String name=node.getName();
// String id=String.valueOf(node.getId());
// Node fathernode=node.getParent();
// String fathername="";
// if(null!=fathernode){
// boolean b=!fathernode.isLeaf();
// while (b){
// fathername=fathernode.getName()+"-"+fathername;
// fathernode=fathernode.getParent();
// if(null!=fathernode){
// b=!fathernode.isLeaf();
// }else{
// b=false;
// }
// }
// }
// result=fathername+name+","+id;
// showresult=fathername+name;
// adapter.notifyDataSetChanged();
// if(!BooleanUtils.isEmpty(result)){
// suretextview.setVisibility(View.VISIBLE);
// sellayout.setVisibility(View.VISIBLE);
// seltextview.setText("已選擇:"+showresult);
// }else{
// suretextview.setVisibility(View.INVISIBLE);
// sellayout.setVisibility(View.GONE);
// seltextview.setText("");
// }
// toast.showToast(result);
// Log.d("TreeViewActivity","name----:"+name);
// Log.d("TreeViewActivity","id----:"+id);
// Log.d("TreeViewActivity","fathername----:"+fathername);
// Log.d("TreeViewActivity","result----:"+result);
// }else{//根節點
// suretextview.setVisibility(View.INVISIBLE);
// sellayout.setVisibility(View.GONE);
// seltextview.setText("");
// }
}
/**
* 各種點選事件的方法
* */
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.apptoplayout_withtextview_backlayout://返回
finish();
overridePendingTransition(0,R.anim.activity_right_open);
break;
case R.id.apptoplayout_withtextview_righttextview://確定新增
toast.showToast("確定!");
break;
}
}
/**
* onKeyDown方法
* */
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
overridePendingTransition(0,R.anim.activity_right_open);
return false;
}
return super.onKeyDown(keyCode, event);
}
}
相關文章
- MySql樹形結構(多級選單)查詢設計方案MySql
- 樹形結構
- 樹形結構處理
- 七、基本資料結構(樹形結構)資料結構
- Linux基礎命令---顯示樹形程式pstreeLinux
- markdown樹形結構生成工具
- MySQL多層級結構-樹搜尋介紹MySql
- 資料結構中樹形結構簡介資料結構
- 工具函式:普通陣列如何轉為樹形結構資料(多層級)陣列?函式陣列
- LayUI—tree樹形結構的使用UI
- Android 12(S) 圖形顯示系統 - 開篇Android
- 字串陣列轉為樹形結構字串陣列
- Linux 下樹形結構的檢視Linux
- 淺談樹形結構的特性和應用(上):多叉樹,紅黑樹,堆,Trie樹,B樹,B+樹...
- Android UI 顯示原理分析小結AndroidUI
- 如何在 Laravel 中去構建部門樹形結構 APILaravelAPI
- 將List物件列表轉為樹形結構物件
- Android 12(S) 圖形顯示系統 - 示例應用(二)Android
- Android 12(S) 圖形顯示系統 - 基本概念(一)Android
- Android 12(S) 圖形顯示系統 - createSurface的流程(五)Android
- JAVA樹形結構 通用程式碼(高效能)Java
- Android 12(S) 圖形顯示系統 - BufferQueue/BLASTBufferQueue之初識(六)AndroidAST
- iView元件庫之下拉框dropdown(樹形結構)View元件
- Vue遞迴元件實現樹形結構選單Vue遞迴元件
- 聊聊mysql的樹形結構儲存及查詢MySql
- Android 12(S) 圖形顯示系統 - BufferQueue的工作流程(十一)Android
- Android 12(S) 圖形顯示系統 - BufferQueue的工作流程(十)Android
- Android 12(S) 圖形顯示系統 - BufferQueue的工作流程(八)Android
- Android 12(S) 圖形顯示系統 - 初識ANativeWindow/Surface/SurfaceControl(七)Android
- Android Libgdx 顯示文字Android
- 【Android】自定義樹形控制元件Android控制元件
- Android 12(S) 圖形顯示系統 - 解讀Gralloc架構及GraphicBuffer建立/傳遞/釋放(十四)Android架構
- 在vue專案中使用樹形結構的穿梭框Vue
- 值得了解的九種樹形資料結構 - Franco資料結構
- MySQL怎樣透過Adjacency List儲存樹形結構?MySql
- 樹形結構的選單表設計與查詢
- win10系統下顯示資料夾樹形目錄的方法Win10
- Android技能樹 - Rxjava原始碼(1) 之 初步結構AndroidRxJava原始碼