Android中的列表樹形展示,AndroidTreeView的使用動態設定樹形結構
阿新 • • 發佈:2019-02-18
1、在專案中用到了這個控制元件【展示並選擇某公司的部門】,但是在使用過程中又出一個問題——我並不知道該公司有幾個部門,部門下面是否有子部門?
2、實現步驟如下:【開發工具:Android studio】
① 在專案中新增依賴:compile 'com.github.bmelnychuk:atv:1.2.+' 【是GitHub上的一個框架,搜尋AndroidTreeView】
② 跟著GitHub上的介紹來做就可以了【如果是已知的部門結構的話,照著GitHub上寫就能完成了,問題:不知道多少部門,不知道到部門層級】
③ 具體程式碼如下:【注意看註釋,會有你想要的】
public class JoinDepartmentActivity extends AppCompatActivity implements JoinDepartmentCallView, LoadBackShowLayout.OnLoadErrorReload { @BindView(R.id.pcenter_toolb) Toolbar pcenterToolb; @BindView(R.id.contentView) RelativeLayout contentView; private Unbinder unbinder; private RegisterMResult.RegisterListBean bean; private JoinDepartmentPresenter presenter; private List<DepartmentResult.DepartmentListBean> dataList; private TreeNode root; private TreeNode parent; private TreeNode temp; private ArrayList<String> departmentIdList; private ArrayList<String> departmentNameList; private final int AUDYES = 1; private String departmentString = ""; private int level = 0; private int RESULT_CODE = 0x3333; private ProgressDialog dialog; private LoadBackShowLayout showLayout; private LinearLayout.LayoutParams params; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EApplication.addActivities(this); setContentView(R.layout.activity_join_department); unbinder = ButterKnife.bind(this); setToolbar(); initView(); initData(); } private void initView() { showLayout = new LoadBackShowLayout(this, contentView, this); } private void setToolbar() { setSupportActionBar(pcenterToolb); } private void initData() { departmentIdList = new ArrayList<>(); departmentNameList = new ArrayList<>(); dataList = new ArrayList<>(); /** 這個在下面的節點holder中會使用 */ params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); Intent intent = this.getIntent(); if (intent == null) return; bean = (RegisterMResult.RegisterListBean) intent.getSerializableExtra("data"); /** 建立樹根節點——不會顯示,從第一個節點開始顯示 */ root = TreeNode.root(); /** 第一個節點是parent */ parent = new TreeNode(new DepartmentResult.DepartmentListBean("0", bean.getCompanyName(), 1)).setViewHolder(new MyHolder(this, 0)); /** 將第一個節點新增進根節點 */ root.addChild(parent); /** 生成樹形佈局檢視 */ AndroidTreeView tView = new AndroidTreeView(this, root); /** 將樹形結構佈局檢視新增到佈局中 */ contentView.addView(tView.getView()); if (bean == null) return; presenter = new JoinDepartmentPresenter(this); dialog = ProgressDialog.show(this, "", "載入中...", true); loadData(); } private void loadData() { presenter.getDepartmentSonList(this, "", parent, level); } @OnClick({R.id.toolbar_back, R.id.pc_child_toolbar_subtitle, R.id.pc_child_toolbar_searchbtn}) public void onClick(View view) { switch (view.getId()) { case R.id.toolbar_back: finish(); break; case R.id.pc_child_toolbar_subtitle: finish(); break; case R.id.pc_child_toolbar_searchbtn: showDialog(); break; } } //加入部門彈出框 private void showDialog() { departmentString = ""; if (departmentIdList.size() == 0) { departmentIdList.add(bean.getCompanyId()); departmentNameList.add(bean.getCompanyName()); } for (int i = 0; i < departmentNameList.size(); i++) { if (i < departmentNameList.size() - 1) departmentString += departmentNameList.get(i) + ","; else departmentString += departmentNameList.get(i); } new AlertDialog.Builder(this) .setTitle("系統提示") .setMessage("確定將【" + bean.getRealname() + "】加入以下部門?\n" + departmentString.trim()) .setNegativeButton("取消", null) .setPositiveButton("確定加入", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { presenter.audRegisterUser(JoinDepartmentActivity.this, bean.getRegId(), AUDYES, null, departmentIdList); } }).setCancelable(true).show(); } @Override protected void onDestroy() { super.onDestroy(); if (unbinder != null) unbinder.unbind(); if (presenter != null) presenter.setCallViewNull(this); } //-------------------------------------回撥開始------------------------------- @Override public void loadStart() {//開始起查詢部門 if (!dialog.isShowing()) dialog.onStart(); } @Override public void getDepartmentListFailure(String msg) { if (dialog.isShowing()) dialog.dismiss(); Toast.makeText(this, "獲取部門失敗," + msg, Toast.LENGTH_SHORT).show(); } /** * @param result 返回結果 * @param node 父節點 * @param level 層級 */ @Override public void getDepartmentListSuccess(DepartmentResult result, TreeNode node, int level) { dataList.clear(); if (result != null && result.getDepartmentList() != null && result.getDepartmentList().size() > 0) { dataList.addAll(result.getDepartmentList()); } for (int i = 0; i < dataList.size(); i++) { temp = new TreeNode(dataList.get(i)).setViewHolder(new MyHolder(this, (level + 1))); /** 將得到的子節點新增到父節點——引數傳過來的節點 */ node.addChild(temp); /** 判斷有子部門則繼續請求 */ if (dataList.get(i).getSubCount() > 0) presenter.getDepartmentSonList(this, dataList.get(i).getDepartmentId(), temp, (level + 1)); } if (dialog.isShowing()) dialog.dismiss(); } @Override public void joinSuccess() { showLayout.showOnloadOk(); Toast.makeText(this, "已成功將該人員加入部門", Toast.LENGTH_SHORT).show(); setResult(RESULT_CODE); this.finish(); } @Override public void joinFailure(String msg) { Toast.makeText(this, "加入部門失敗," + msg, Toast.LENGTH_SHORT).show(); } @Override public void loadErrorMsg(String msg) { showLayout.showOnError(msg); } @Override public void reLoad() { level = 0; loadData(); } //-------------------------------------回撥結束------------------------------- /** * 自定義節點檢視的holder類 */ class MyHolder extends TreeNode.BaseNodeViewHolder<DepartmentResult.DepartmentListBean> { private int level;//表示層級 private boolean isExpand = false; private DepartmentResult.DepartmentListBean bean; public MyHolder(Context context, int level) { super(context); this.level = level; } @Override public View createNodeView(final TreeNode node, final DepartmentResult.DepartmentListBean value) { this.bean = value; final LayoutInflater inflater = LayoutInflater.from(context); final View view = inflater.inflate(R.layout.item_only_text, null, false); /** 設定節點的佈局,使其寬度——martch_parent,高度——wrap_content */ view.setLayoutParams(params); /** 根據層級來設定左邊的縮排 */ view.setPadding(level * 50, view.getPaddingTop(), view.getRight(), view.getPaddingBottom()); TextView tvValue = (TextView) view.findViewById(R.id.only_text); final CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkbox); final CheckBox checkBoxRight = (CheckBox) view.findViewById(R.id.checkbox_right); if (value.getSubCount() > 0) { checkBoxRight.setVisibility(View.VISIBLE); } else { checkBoxRight.setVisibility(View.GONE); } checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { //選中則新增 departmentIdList.add(value.getDepartmentId()); departmentNameList.add(value.getName()); } else { //取消選中則移除 departmentIdList.remove(value.getDepartmentId()); departmentNameList.remove(value.getName()); } } }); node.setClickListener(new TreeNode.TreeNodeClickListener() { @Override public void onClick(TreeNode node, Object value) { if (isExpand) { checkBoxRight.setBackgroundResource(R.drawable.arrow_right_gray); isExpand = false; /** 沒有子部門點選整列(即:點選節點)選中該部門 */ if (bean.getSubCount() == 0) { checkBox.setChecked(false); } } else { checkBoxRight.setBackgroundResource(R.drawable.arrow_down_gray); isExpand = true; if (bean.getSubCount() == 0) { checkBox.setChecked(true); } } } }); tvValue.setText(value.getName()); return view; } } }
④ 程式碼執行過程:進入該Activity新增根節點,根節點新增parent節點【公司】,得到parent下面的子部門,判斷子部門下還有子部門則繼續請求,沒有則不請求。
開始請求就彈出“載入中..”的progressdialog【cancelable=false】,返回則取消,直到最後一次返回才dialog消失。這個時候可以點節點點出子級部門
⑤ 下面節點佈局
⑥ 下面是Activity佈局<?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="wrap_content" android:paddingLeft="5dp" android:paddingRight="5dp" android:paddingTop="10dp" android:paddingBottom="10dp" android:orientation="horizontal" android:background="@drawable/line_top" > <CheckBox android:id="@+id/checkbox" android:layout_width="20dp" android:layout_height="20dp" android:layout_marginLeft="3dp" android:layout_gravity="center" android:button="@null" android:background="@drawable/radiobtn_bg" /> <TextView android:id="@+id/only_text" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_weight="1" android:gravity="left|center_vertical" android:layout_gravity="center_vertical" android:maxLines="1" android:ellipsize="end" /> <CheckBox android:id="@+id/checkbox_right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:button="@null" android:background="@drawable/arrow_right_gray" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_join_department" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <android.support.v7.widget.Toolbar android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white" android:id="@+id/pcenter_toolb" android:contentInsetStart="0dp" > <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_gravity="center_vertical" android:paddingRight="@dimen/pc_child_toolbar_paddingright" > <ImageButton android:id="@+id/toolbar_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:background="@color/white" android:src="@drawable/pc_child_toolbar_back" /> <TextView android:id="@+id/pc_child_toolbar_subtitle" style="@style/pc_toolbar_subtitle" android:layout_toRightOf="@+id/toolbar_back" android:text="@string/registerm_unaud" /> <TextView android:id="@+id/pc_child_toolbar_title" style="@style/pc_toolbar_title" android:text="@string/registerm_choosedepartment" /> <TextView android:id="@+id/pc_child_toolbar_searchbtn" android:layout_width="wrap_content" android:layout_height="@dimen/pc_child_toolbar_searchbar_w_h" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:gravity="center" android:text="加入部門" android:textColor="@color/deepred" /> </RelativeLayout> </android.support.v7.widget.Toolbar> <!-- 節點將會新增到此佈局中 --> <RelativeLayout android:id="@+id/contentView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/line_top" /> </LinearLayout>