您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關android RecycleView如何實現多級樹形列表的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
本文實例為大家分享了android RecycleView實現多級樹形列表的具體代碼,供大家參考,具體內容如下
實現多級樹狀列表:
1. Node.java
public class Node<T, B> implements Serializable { /** * 傳入的實體對象 */ public B bean; /** * 設置開啟的圖片 */ public int iconExpand = -1; /** * 設置關閉的圖片 */ public int iconNoExpand = -1; private T id; /** * 根節點pId為0 */ private T pId; private String name; /** * 當前的級別 */ private int level; /** * 是否展開 */ private boolean isExpand = false; private int icon = -1; /** * 下一級的子Node */ private List<Node> children = new ArrayList<>(); /** * 父Node */ private Node parent; /** * 是否被checked選中 */ private boolean isChecked; /** * 是否為新添加的 */ public boolean isNewAdd = true; /** * 單選標記 */ public boolean isSingle; /** * 支部code */ public String braCode; public Node(T id, T pId, String name) { super(); this.id = id; this.pId = pId; this.name = name; } public Node(T id, T pId, String name, B bean) { super(); this.id = id; this.pId = pId; this.name = name; this.bean = bean; } public String getBraCode() { return braCode; } public void setBraCode(String braCode) { this.braCode = braCode; } public boolean isSingle() { return isSingle; } public void setSingle(boolean single) { isSingle = single; } public boolean isChecked() { return isChecked; } public void setChecked(boolean isChecked) { this.isChecked = isChecked; } public Node() { } public int getIcon() { return icon; } public void setIcon(int icon) { this.icon = icon; } public T getId() { return id; } public void setId(T id) { this.id = id; } public T getpId() { return pId; } public void setpId(T pId) { this.pId = pId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void setLevel(int level) { this.level = level; } public boolean isExpand() { return isExpand; } public List<Node> getChildren() { return children; } public void setChildren(List<Node> children) { this.children = children; } public Node getParent() { return parent; } public void setParent(Node parent) { this.parent = parent; } /** * 是否為跟節點 * * @return */ public boolean isRoot() { return parent == null; } /** * 判斷父節點是否展開 * * @return */ public boolean isParentExpand() { if (parent == null){ return false; } return parent.isExpand(); } /** * 是否是葉子界點 * * @return */ public boolean isLeaf() { return children.size() == 0; } /** * 獲取level */ public int getLevel() { return parent == null ? 0 : parent.getLevel() + 1; } /** * 設置展開 * * @param isExpand */ public void setExpand(boolean isExpand) { this.isExpand = isExpand; if (!isExpand) { for (Node node : children) { node.setExpand(false); } } } }
2. OnTreeNodeClickListener.java
public interface OnTreeNodeClickListener { /** * 點擊事件方法 * @param node 節點 * @param position 條目位置 */ void onClick(Node node, int position); }
3. TreeHelper.java
public class TreeHelper { /** * 傳入node 返回排序后的Node * * @param datas 傳入數據 * @param defaultExpandLevel 默認展開等級 * @return * @throws IllegalArgumentException * @throws IllegalAccessException */ public static List<Node> getSortedNodes(List<Node> datas, int defaultExpandLevel) { List<Node> result = new ArrayList<Node>(); // 設置Node間父子關系 List<Node> nodes = convetData2Node(datas); // 拿到根節點 List<Node> rootNodes = getRootNodes(nodes); // 排序以及設置Node間關系 for (Node node : rootNodes) { addNode(result, node, defaultExpandLevel, 1); } return result; } /** * 過濾出所有可見的Node * * @param nodes * @return */ public static List<Node> filterVisibleNode(List<Node> nodes) { List<Node> result = new ArrayList<Node>(); for (Node node : nodes) { // 如果為跟節點,或者上層目錄為展開狀態 if (node.isRoot() || node.isParentExpand()) { setNodeIcon(node); result.add(node); } } return result; } /** * 設置Node間,父子關系;讓每兩個節點都比較一次,即可設置其中的關系 */ private static List<Node> convetData2Node(List<Node> nodes) { 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); //判斷id是String,Integer if (m.getpId() instanceof String) { if (m.getpId().equals(n.getId())) { n.getChildren().add(m); m.setParent(n); } else if (m.getId().equals(n.getpId())) { m.getChildren().add(n); n.setParent(m); } } else if(m.getpId() instanceof Integer) { //這里就是修改的地方,因為這是Integer類,節點的地址是不一樣的,所以不能直接使用==比較,要用equals()進行值的比較 if (m.getpId().equals(n.getId())) { n.getChildren().add(m); m.setParent(n); } else if (m.getId().equals(n.getpId())) { m.getChildren().add(n); n.setParent(m); } } } } return nodes; } /** * 獲取所有根節點 * * @param nodes * @return */ private static List<Node> getRootNodes(List<Node> nodes) { List<Node> root = new ArrayList<Node>(); for (Node node : nodes) { if (node.isRoot()) { root.add(node); } } return root; } /** * 把一個節點上的所有的內容都掛上去 */ private static <T, B> void addNode(List<Node> nodes, Node<T, B> node, int defaultExpandLeval, int currentLevel) { nodes.add(node); //判斷添加新節點的時候,保持以前的狀態不變 if (node.isNewAdd && defaultExpandLeval >= currentLevel) { node.setExpand(true); } if (node.isLeaf()) { //判斷是子節點 return; } for (int i = 0; i < node.getChildren().size(); i++) { addNode(nodes, node.getChildren().get(i), defaultExpandLeval, currentLevel + 1); } } /** * 設置節點的圖標 * * @param node */ private static void setNodeIcon(Node node) { if (node.getChildren().size() > 0 && node.isExpand()) { node.setIcon(node.iconExpand); } else if (node.getChildren().size() > 0 && !node.isExpand()) { node.setIcon(node.iconNoExpand); } else { node.setIcon(-1); } } }
4. TreeRecyclerAdapter.java -----RecycleView形式的樹形列表適配器
public abstract class TreeRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { protected Context mContext; /** * 存儲所有可見的Node */ protected List<Node> mNodes = new ArrayList<>(); protected LayoutInflater mInflater; /** * 存儲所有的Node */ protected List<Node> mAllNodes = new ArrayList<>(); /** * 點擊的回調接口 */ private OnTreeNodeClickListener onTreeNodeClickListener; /** * 默認不展開 */ private int defaultExpandLevel = 0; /** * 展開與關閉的圖片 */ private int iconExpand = -1, iconNoExpand = -1; /** * 當前被點擊的位置 */ private int layoutPosition = -1; public void setOnTreeNodeClickListener(OnTreeNodeClickListener onTreeNodeClickListener) { this.onTreeNodeClickListener = onTreeNodeClickListener; } public TreeRecyclerAdapter(RecyclerView mTree, Context context, List<Node> datas, int defaultExpandLevel, int iconExpand, int iconNoExpand) { this.iconExpand = iconExpand; this.iconNoExpand = iconNoExpand; for (Node node : datas) { node.getChildren().clear(); node.iconExpand = iconExpand; node.iconNoExpand = iconNoExpand; } this.defaultExpandLevel = defaultExpandLevel; mContext = context; /** * 對所有的Node進行排序 */ mAllNodes = TreeHelper.getSortedNodes(datas, defaultExpandLevel); /** * 過濾出可見的Node */ mNodes = TreeHelper.filterVisibleNode(mAllNodes); mInflater = LayoutInflater.from(context); } /** * @param mTree * @param context * @param datas * @param defaultExpandLevel 默認展開幾級樹 */ public TreeRecyclerAdapter(RecyclerView mTree, Context context, List<Node> datas, int defaultExpandLevel) { this(mTree, context, datas, defaultExpandLevel, -1, -1); } @Override public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) { Node node = mNodes.get(position); // convertView = getConvertView(node, position, convertView, parent); // 設置內邊距 int padding = DisplayUtil.dp2px(mContext,5); holder.itemView.setPadding(DisplayUtil.dp2px(mContext,(node.getLevel()+1) * 15), padding,padding,padding); //然后設置單選,修改如下 /** * 設置節點點擊時,可以展開以及關閉,將事件繼續往外公布 */ holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //獲取當前點擊的位置 layoutPosition = holder.getLayoutPosition(); expandOrCollapse(position); if (onTreeNodeClickListener != null) { onTreeNodeClickListener.onClick(mNodes.get(position), position); } } }); if(position == layoutPosition){ node.setSingle(true); }else { node.setSingle(false); } onBindViewHolder(node, holder, position); } @Override public int getItemCount() { return mNodes.size(); } /** * 清除掉之前數據并刷新 重新添加 * * @param mlists * @param defaultExpandLevel 默認展開幾級列表 */ public void addDataAll(List<Node> mlists, int defaultExpandLevel) { mAllNodes.clear(); addData(-1, mlists, defaultExpandLevel); } /** * 在指定位置添加數據并刷新 可指定刷新后顯示層級 * * @param index * @param mlists * @param defaultExpandLevel 默認展開幾級列表 */ public void addData(int index, List<Node> mlists, int defaultExpandLevel) { this.defaultExpandLevel = defaultExpandLevel; notifyData(index, mlists); } /** * 在指定位置添加數據并刷新 * * @param index * @param mlists */ public void addData(int index, List<Node> mlists) { notifyData(index, mlists); } /** * 添加數據并刷新 * * @param mlists */ public void addData(List<Node> mlists) { addData(mlists, defaultExpandLevel); } /** * 添加數據并刷新 可指定刷新后顯示層級 * * @param mlists * @param defaultExpandLevel */ public void addData(List<Node> mlists, int defaultExpandLevel) { this.defaultExpandLevel = defaultExpandLevel; notifyData(-1, mlists); } /** * 添加數據并刷新 * * @param node */ public void addData(Node node) { addData(node, defaultExpandLevel); } /** * 添加數據并刷新 可指定刷新后顯示層級 * * @param node * @param defaultExpandLevel */ public void addData(Node node, int defaultExpandLevel) { List<Node> nodes = new ArrayList<>(); nodes.add(node); this.defaultExpandLevel = defaultExpandLevel; notifyData(-1, nodes); } /** * 刷新數據 * * @param index * @param mListNodes */ private void notifyData(int index, List<Node> mListNodes) { for (int i = 0; i < mListNodes.size(); i++) { Node node = mListNodes.get(i); node.getChildren().clear(); node.iconExpand = iconExpand; node.iconNoExpand = iconNoExpand; } for (int i = 0; i < mAllNodes.size(); i++) { Node node = mAllNodes.get(i); node.getChildren().clear(); node.isNewAdd = false; } if (index != -1) { mAllNodes.addAll(index, mListNodes); } else { mAllNodes.addAll(mListNodes); } /** * 對所有的Node進行排序 */ mAllNodes = TreeHelper.getSortedNodes(mAllNodes, defaultExpandLevel); /** * 過濾出可見的Node */ mNodes = TreeHelper.filterVisibleNode(mAllNodes); //刷新數據 notifyDataSetChanged(); } /** * 獲取排序后所有節點 * * @return */ public List<Node> getAllNodes() { if (mAllNodes == null){ mAllNodes = new ArrayList<Node>(); } return mAllNodes; } /** * 相應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();// 刷新視圖 } } } /** * 設置多選 * * @param node * @param checked */ protected void setChecked(final Node node, boolean checked) { node.setChecked(checked); setChildChecked(node, checked); if (node.getParent() != null){ setNodeParentChecked(node.getParent(), checked); } notifyDataSetChanged(); } /** * 設置是否選中 * * @param node * @param checked */ public <T, B> void setChildChecked(Node<T, B> node, boolean checked) { if (!node.isLeaf()) { node.setChecked(checked); for (Node childrenNode : node.getChildren()) { setChildChecked(childrenNode, checked); } } else { node.setChecked(checked); } } private void setNodeParentChecked(Node node, boolean checked) { if (checked) { node.setChecked(checked); if (node.getParent() != null){ setNodeParentChecked(node.getParent(), checked); } } else { List<Node> childrens = node.getChildren(); boolean isChecked = false; for (Node children : childrens) { if (children.isChecked()) { isChecked = true; } } //如果所有自節點都沒有被選中 父節點也不選中 if (!isChecked) { node.setChecked(checked); } if (node.getParent() != null){ setNodeParentChecked(node.getParent(), checked); } } } public abstract void onBindViewHolder(Node node, RecyclerView.ViewHolder holder, final int position); //===================在這里添加修改選中位置的方法===================== /** * 設置選中位置 * @param selectedPosition */ public void setSelectedPosition(int selectedPosition) { this.layoutPosition = selectedPosition; } }
5. 自定義樣式:
TypeTreeRecyclerAdapter.java
public class TypeTreeRecyclerAdapter extends TreeRecyclerAdapter { private static final String TAG = "TypeTreeRecyclerAdapter"; private boolean isSingle; public TypeTreeRecyclerAdapter(RecyclerView mTree, Context context, List<Node> datas, int defaultExpandLevel, int iconExpand, int iconNoExpand,boolean isSingle) { super(mTree, context, datas, defaultExpandLevel, iconExpand, iconNoExpand); this.isSingle = isSingle; } public TypeTreeRecyclerAdapter(RecyclerView mTree, Context context, List<Node> datas, int defaultExpandLevel) { super(mTree, context, datas, defaultExpandLevel); } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { return new MyHoder(View.inflate(mContext, R.layout.tree_list_item,null)); } @Override public void onBindViewHolder(final Node node, RecyclerView.ViewHolder holder, int position) { final MyHoder viewHolder = (MyHoder) holder; //todo do something viewHolder.cb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // setChecked(node,viewHolder.cb.isChecked()); setChildChecked(node,viewHolder.cb.isChecked()); listener.onItemClick(position); } }); viewHolder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if (isSingle) { if (b) { //如果checkbox的狀態是選中的,那么除了被選中的那條數據,其他Node節點的checkbox狀態都為false for (int i = 0; i < mAllNodes.size(); i++) { if (((Node) mAllNodes.get(i)).getId().equals(node.getId())) { Log.e(TAG,"note="+mAllNodes.get(i).getName()); ((Node) mAllNodes.get(i)).setChecked(b); } else { // Log.e(TAG,"1111 note="+mAllNodes.get(i).getName()); ((Node) mAllNodes.get(i)).setChecked(false); } } } else {//如果checkbox的狀態是選中的,所有Node節點checkbox狀態都為false for (int i = 0; i < mAllNodes.size(); i++) { if (((Node) mAllNodes.get(i)).getId().equals(node.getId())) { ((Node) mAllNodes.get(i)).setChecked(b); } } } } else { 如果checkbox是多選的,對應node節點的checkbox狀態視用戶的操作而定 for (int i = 0; i < mAllNodes.size(); i++) { if (((Node) mAllNodes.get(i)).getId().equals(node.getId())) ((Node) mAllNodes.get(i)).setChecked(b); } } } }); if (node.isChecked()){ viewHolder.cb.setChecked(true); }else { viewHolder.cb.setChecked(false); } if (node.getIcon() == -1) { viewHolder.icon.setVisibility(View.INVISIBLE); } else { viewHolder.icon.setVisibility(View.VISIBLE); viewHolder.icon.setImageResource(node.getIcon()); } viewHolder.label.setText(node.getName()); } public interface onAItemClickListener{ void onItemClick(int position); } private onAItemClickListener listener; public void setOnItemClickListener (onAItemClickListener onItemClickListener) { listener = onItemClickListener; } class MyHoder extends RecyclerView.ViewHolder{ public CheckBox cb; public TextView label; public ImageView icon; public MyHoder(View itemView) { super(itemView); cb = (CheckBox) itemView.findViewById(R.id.cb_select_tree); label = (TextView) itemView.findViewById(R.id.id_treenode_label); icon = (ImageView) itemView.findViewById(R.id.icon); } } }
6. tree_list_item.xml
<?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"> <ImageView android:id="@+id/icon" android:layout_width="16dp" android:layout_height="16dp" android:scaleType="centerCrop"/> <CheckBox android:id="@+id/cb_select_tree" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:focusable="false" /> <TextView android:id="@+id/id_treenode_label" android:layout_width="match_parent" android:layout_height="wrap_content" android:singleLine="true" android:ellipsize="marquee" android:textColor="#000" android:textSize="14sp"/> </LinearLayout>
7. 頁面:
public class OfficeTypeDialog extends Dialog implements View.OnClickListener { private static final String TAG = "OfficeTypeDialog"; private Context mContext; private RecyclerView rv_typeTree; private TypeTreeRecyclerAdapter typeTreeRecyclerAdapter; private TypeTreeBean selectedBean; private Button btn_confirm,btn_cancel; private String source; private String orgId; public OfficeTypeDialog(@NonNull Context context) { super(context); mContext = context; } public OfficeTypeDialog(@NonNull Context context, int themeResId) { super(context, themeResId); mContext = context; } public OfficeTypeDialog(@NonNull Context context, int themeResId,String source) { super(context, themeResId); mContext = context; this.source = source; } public OfficeTypeDialog(@NonNull Context context, int themeResId,String source,String orgId) { super(context, themeResId); mContext = context; this.source = source; this.orgId = orgId; } protected OfficeTypeDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) { super(context, cancelable, cancelListener); mContext = context; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.dialog_office_type); getWindow().setGravity(Gravity.CENTER); //顯示在 WindowManager m = getWindow().getWindowManager(); Display d = m.getDefaultDisplay(); WindowManager.LayoutParams p = getWindow().getAttributes(); p.width = d.getWidth()/5*4; //設置dialog的寬度為當前手機屏幕的寬度 p.height = d.getHeight()/5*4; getWindow().setAttributes(p); rv_typeTree = findViewById(R.id.rv_typeTree); btn_confirm = findViewById(R.id.btn_confirm); btn_cancel = findViewById(R.id.btn_cancel); btn_confirm.setOnClickListener(this); btn_cancel.setOnClickListener(this); getOfficeType(); } List<Node> mDatas = new ArrayList<>(); private List<TypeTreeBean> typeTreeBeans = new ArrayList<>(); private void getOfficeType() { String url = ""; if (source.equals("officeType")) { url = WebApi.OFFICE_TYPE; } else if(source.equals("area")) { url = WebApi.OFFICE_AREA; } else if(source.equals("nmpa")) { url = WebApi.OFFICE_NMPA; } else if(source.equals("nmpaUser")) { if (orgId != null) { url = WebApi.OFFICE_NMPA_USER+"?orgId="+orgId; } } OkHttpUtil.get(TAG, url, new StringCallback() { @Override public void onError(Call call, Exception e, int id) { Log.e(TAG,"getOfficeType e="+e); } @Override public void onResponse(String response, int id) { Log.e(TAG,"getOfficeType response="+response); try { JSONObject object = new JSONObject(response); if (object.getInt("code") == 200) { JSONArray array = object.getJSONArray("data"); Gson gson = new Gson(); for (int i = 0;i<array.length();i++) { TypeTreeBean typeTreeBean = gson.fromJson(array.get(i).toString(), TypeTreeBean.class); typeTreeBeans.add(typeTreeBean); } for (int j=0;j<typeTreeBeans.size();j++) { TypeTreeBean typeTreeBean = typeTreeBeans.get(j); mDatas.add(new Node(typeTreeBean.getId(),0,typeTreeBean.getName(),typeTreeBean)); if (typeTreeBean.getChildren() != null && typeTreeBean.getChildren().size() != 0) { List<TypeTreeBean> typeTreeBeans1 = typeTreeBean.getChildren(); for (int j1 = 0;j1<typeTreeBeans1.size();j1++) { TypeTreeBean typeTreeBean1 = typeTreeBeans1.get(j1); mDatas.add(new Node(typeTreeBean1.getId(),typeTreeBean.getId(),typeTreeBean1.getName(),typeTreeBean1)); if (typeTreeBean1.getChildren() != null && typeTreeBean1.getChildren().size() != 0) { List<TypeTreeBean> typeTreeBeans2 = typeTreeBean1.getChildren(); for (int j2 = 0;j2<typeTreeBeans2.size();j2++) { TypeTreeBean typeTreeBean2 = typeTreeBeans2.get(j2); mDatas.add(new Node(typeTreeBean2.getId(),typeTreeBean1.getId(),typeTreeBean2.getName(),typeTreeBean2)); if (typeTreeBean2.getChildren() != null && typeTreeBean2.getChildren().size() != 0) { List<TypeTreeBean> typeTreeBeans3 = typeTreeBean2.getChildren(); for (int j3 = 0;j3<typeTreeBeans3.size();j3++) { TypeTreeBean typeTreeBean3 = typeTreeBeans3.get(j3); mDatas.add(new Node(typeTreeBean3.getId(),typeTreeBean2.getId(),typeTreeBean3.getName(),typeTreeBean3)); if (typeTreeBean3.getChildren() != null && typeTreeBean3.getChildren().size() != 0) { List<TypeTreeBean> typeTreeBeans4 = typeTreeBean3.getChildren(); for (int j4 = 0;j4<typeTreeBeans4.size();j4++) { TypeTreeBean typeTreeBean4 = typeTreeBeans4.get(j4); mDatas.add(new Node(typeTreeBean4.getId(),typeTreeBean3.getId(),typeTreeBean4.getName(),typeTreeBean4)); } } } } } } } } } initTypeList(); } } catch (JSONException e) { e.printStackTrace(); } } }); } private void initTypeList() { rv_typeTree.setLayoutManager(new LinearLayoutManager(mContext)); typeTreeRecyclerAdapter = new TypeTreeRecyclerAdapter(rv_typeTree, mContext, mDatas, 1,R.drawable.tree_expand,R.drawable.tree_econpand, true); rv_typeTree.setAdapter(typeTreeRecyclerAdapter); typeTreeRecyclerAdapter.setOnItemClickListener(new TypeTreeRecyclerAdapter.onAItemClickListener() { @Override public void onItemClick(int position) { Log.e(TAG,"position="+position); updatePosition(position); clickShow(); } }); } /** * 更新選擇窗 * * @param selectedPosition 選中位置 */ public void updatePosition(int selectedPosition) { if (typeTreeRecyclerAdapter != null) { typeTreeRecyclerAdapter.setSelectedPosition(selectedPosition); typeTreeRecyclerAdapter.notifyDataSetChanged(); } } /** * 顯示選中數據 */ public void clickShow(){ // StringBuilder sb = new StringBuilder(); List<TypeTreeBean> typeTreeBeanList = new ArrayList<>(); final List<Node> allNodes = typeTreeRecyclerAdapter.getAllNodes(); for (int i = 0; i < allNodes.size(); i++) { if (allNodes.get(i).isChecked()){ // sb.append(allNodes.get(i).getName()+","); typeTreeBeanList.add((TypeTreeBean) allNodes.get(i).bean); } } // String strNodesName = sb.toString(); if (typeTreeBeanList != null && typeTreeBeanList.size() != 0) { Log.e(TAG,"bean="+typeTreeBeanList.get(0).getName()); selectedBean = typeTreeBeanList.get(0); } } /** * 設置確定取消按鈕的回調 */ public OnClickBottomListener onClickBottomListener; public void setOnClickBottomListener(OnClickBottomListener onClickBottomListener) { this.onClickBottomListener = onClickBottomListener; } public interface OnClickBottomListener{ /** * 點擊確定按鈕事件 */ public void onPositiveClick(TypeTreeBean typeBean); /** * 點擊取消按鈕事件 */ public void onNegtiveClick(); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.btn_confirm: if ( onClickBottomListener!= null) { onClickBottomListener.onPositiveClick(selectedBean); } break; case R.id.btn_cancel: if ( onClickBottomListener!= null) { onClickBottomListener.onNegtiveClick(); } break; } } }
8. 調用dialog
OfficeTypeDialog typeDialog = new OfficeTypeDialog(mContext,R.style.Dialog,"officeType"); typeDialog.setOnClickBottomListener(new OfficeTypeDialog.OnClickBottomListener() { @Override public void onPositiveClick(TypeTreeBean typeBean) { tv_officeType.setText(typeBean.getName()); officeType = typeBean; typeDialog.dismiss(); } @Override public void onNegtiveClick() { typeDialog.dismiss(); } }); typeDialog.show();
9. 效果圖
感謝各位的閱讀!關于“android RecycleView如何實現多級樹形列表”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。