Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to customize the tree shape in android

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

In view of how to customize the tree shape in android, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

TreeView.java Code:

Package com.tbx.android.widget;import java.util.ArrayList;public class TreeView extends ListView implements OnItemClickListener {private static String TAG = "TreeView"; List treeElements = null;// all node sets List currentElements = null;// the currently displayed node set List tempElements = null;// is used to temporarily store HashMap deleteMap = new HashMap (); / / to store child nodes to be deleted, as well as indirect child nodes. TreeViewAdapter adapter = null;// for data filling LastLevelItemClickListener itemClickCallBack;// user click event callback public TreeView (final Context context, AttributeSet attrs) {super (context, attrs); Log.d (TAG, "create with TreeView (Context context, AttributeSet attrs)"); treeElements = new ArrayList (); currentElements = new ArrayList (); adapter = new TreeViewAdapter (context, currentElements); this.setAdapter (adapter) ItemClickCallBack = new LastLevelItemClickListener () {@ Override public void onLastLevelItemClick (int position,TreeViewAdapter adapter) {Log.d (TAG, "last level element" + currentElements.get (position). GetTitle () + "is clicked");}; this.setOnItemClickListener (this) } public void initData (Context context, List treeElements) {this.treeElements = treeElements; getFirstLevelElements (context); adapter.notifyDataSetChanged ();} / * set click event callback API * * @ param itemClickCallBack * / public void setLastLevelItemClickCallBack (LastLevelItemClickListener itemClickCallBack) {this.itemClickCallBack = itemClickCallBack Initialize tree-structured list data and add first-level data to currentElements * / public void getFirstLevelElements (Context context) {Log.d (TAG, "initCurrentElements"); int size = treeElements.size (); Log.d (TAG, "tree elements num is:" + size); if (currentElements = = null) {currentElements = new ArrayList () } currentElements.clear (); for (int I = 0; I

< size; i++) { if (treeElements.get(i).getLevel() == 1) { currentElements.add(treeElements.get(i)); Log.d(TAG, "find a first level element: " + treeElements.get(i)); } } } /** * 从所有节点集合中获取某父节点的子节点集合 * * @param parentId * @return */ private List getChildElementsFromAllById(String parentId) { tempElements = new ArrayList(); int size = treeElements.size(); for (int i = 0; i < size; i++) { String temptParentId = treeElements.get(i).getParentId() ; if(temptParentId != null){ if (temptParentId.equalsIgnoreCase(parentId)) { tempElements.add(treeElements.get(i)); Log.d(TAG, "find a child element: " + treeElements.get(i)); } } } return tempElements; } /** * 从当前显示的节点集合中 , 获取某父节点的子节点集合 * @param parentId * @return */ private List getChildElementsFromCurrentById(String parentId) { Log.d(TAG, "getChildElementsFromCurrentById parentId: " + parentId); if (tempElements == null) { tempElements = new ArrayList(); } else { tempElements.clear(); } int size = currentElements.size(); if(size >

0) {for (TreeElement treeElement: currentElements) {String temp = treeElement.getParentId (); if (temp! = null) {if (temp.equalsIgnoreCase (parentId)) {tempElements.add (treeElement); Log.d (TAG, "find a child element to delete:" + treeElement) } return tempElements;} / * Delete all child node collections of a parent node (including direct and indirect nodes) * @ param parentId * @ return * / private synchronized boolean delAllChildElementsByParentId (String parentId) {Log.e (TAG, "delAllChildElementsByParentId:" + parentId) List childElments = getChildElementsFromCurrentById (parentId); List treeElementsToDel = deleteMap.get (parentId); if (treeElementsToDel = = null) {treeElementsToDel = new ArrayList (); deleteMap.put (parentId, treeElementsToDel);} else {treeElementsToDel.clear ();} int size = childElments.size (); Log.e (TAG, "childElments size:" + size); TreeElement childElement = null For (int I = 0; I

< size; i++) { childElement = childElments.get(i); //寻找间接节点 if (childElement.hasChild && childElement.fold) { treeElementsToDel.add(childElement); } } size = treeElementsToDel.size(); Log.e(TAG, "treeElementsToDel size : " + size); if(size >

0) {/ / delete indirect node List deleteTreeElement = treeElementsToDel; for (TreeElement element: deleteTreeElement) {delAllChildElementsByParentId (element.getId ());}} / / delete direct node delDirectChildElementsByParentId (parentId); return true } / * delete the collection of direct child nodes of a parent node * * @ param parentId * @ return * / private synchronized boolean delDirectChildElementsByParentId (String parentId) {Log.d (TAG, "delDirectChildElementsByParentId ():" + parentId); boolean success = false If (currentElements = = null | | currentElements.size () = 0) {Log.d (TAG, "delChildElementsById () failed,currentElements is null or it's size is 0"); return success;} synchronized (currentElements) {int size = currentElements.size (); Log.d (TAG, "begin delete"); for (int I = size-1; I > = 0 ) {String temp = currentElements.get (I) .getParentId (); if (temp! = null & & temp.equalsIgnoreCase (parentId)) {currentElements.get (I) .fold = false;// set the unfold state to false currentElements.remove (I) when you remember to hide the child node. } success = true; return success;} / * look up the subscript according to id * * @ param id * @ return * / @ SuppressWarnings ("unused") private int getElementIndexById (String id) {int num = currentElements.size (); for (int I = 0; I

< num; i++) { if (currentElements.get(i).getId().equalsIgnoreCase(id)) { return i; } } return -1; } @SuppressWarnings("unused") private TreeElement getElementById(String id){ int num = currentElements.size(); if( num >

0) {for (int I = 0; I < num; iTunes +) {if (currentElements.get (I). GetId () .equalsIgnoreCase (id)) {return currentElements.get (I);} return null } @ Override public void onItemClick (AdapterView arg0, View convertView, int position, long id) {TreeElement element = currentElements.get (position) If (element.isHasChild ()) {/ / only perform operations such as data display or hiding when the current node has a child node if (! element.isFold ()) {/ / the current parent node is an unexpanded state currentElements.addAll (position + 1this.getChildElementsFromAllById (element.getId () } else if (element.fold) {/ / the current parent node is expanded boolean success = this.delAllChildElementsByParentId (element.getId ()); Log.d (TAG, "delete child state:" + success); if (! success) {return;}} element.setFold (! element.isFold ()) / / set anti-state adapter.notifyDataSetChanged (); / / refresh data display} else {/ / only perform user-defined operations itemClickCallBack.onLastLevelItemClick (position,adapter) when the current node has child nodes. }} / * Custom internal interface for callback of events when the user clicks on the final node * / public interface LastLevelItemClickListener {public void onLastLevelItemClick (int position,TreeViewAdapter adapter);}}

TreeElement.java Code:

/ * Class name: TreeElement.java* Class description: tree structure Node Class * creation time: 2012-11-23 16:32 * / public class TreeElement {String id = null;// current node id String title = null;// current node text boolean hasParent = false;// whether the current node has a parent node boolean hasChild = false;// whether the current node has a child node boolean childShow = false / / if the child node, byte when currently displayed String parentId = null;// parent node id int level =-1 dominating / current interface level boolean fold = whether false;// is expanded.}

TreeViewAdapter.java

Package com.tbx.android.adapter;import java.util.List;/** * class name: TreeViewAdapter.java * class description: class used to populate data * creation time: 2012-11-23 16:32*/public class TreeViewAdapter extends BaseAdapter {class ViewHolder {ImageView icon; TextView title;} Context context; ViewHolder holder; LayoutInflater inflater; List elements; public TreeViewAdapter (Context context, List elements) {this.context = context This.elements = elements;} @ Override public int getCount () {return elements.size ();} @ Override public Object getItem (int position) {return elements.get (position);} @ Override public long getItemId (int position) {return position } @ Override public View getView (int position, View convertView, ViewGroup parent) {/ *-get holder---- * / if (convertView = = null) {if (inflater = = null) {inflater = LayoutInflater.from (context) } holder = new ViewHolder (); convertView = inflater .propagate (R.layout.tree_view_item, null); holder.icon = (ImageView) convertView .findViewById (R.id.tree_view_item_icon); holder.title = (TextView) convertView .findViewById (R.id.tree_view_item_title) ConvertView.setTag (holder);} else {holder = (ViewHolder) convertView.getTag () } / * *-- set holder-- * / if (elements.get (position). IsHasChild ()) {/ / has child nodes To display the icon if (elements.get (position) .isFold ()) {holder.icon.setImageResource (R.drawable.tree_view_icon_open) } else if (! elements.get (position). IsFold ()) {holder.icon.setImageResource (R.drawable.tree_view_icon_close);} holder.icon.setVisibility (View.VISIBLE);} else {/ / does not have child nodes, so hide the icon holder.icon.setImageResource (R.drawable.tree_view_icon_close) Holder.icon.setVisibility (View.INVISIBLE);} holder.icon.setPadding (25 * (elements.get (position). GetLevel ()), 0,0,0); / / indent holder.title.setText (elements.get (position). GetTitle ()) according to hierarchy settings; holder.title.setTextSize (35-elements.get (position). GetLevel () * 5); / / set font size return convertView according to hierarchy }}

MainActivity.java

Public class MainActivity extends Activity {private TreeView lvAddress; / / address list private List lvAddressData = new ArrayList (); @ Override public void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); lvAddress = (TreeView) findViewById (R.id.frame_listview_address) LastLevelItemClickListener itemClickCallBack = new LastLevelItemClickListener () {/ / create node click event listener @ Override public void onLastLevelItemClick (int position TreeViewAdapter adapter) {TreeElement element = (TreeElement) adapter.getItem (position) / / then deal with}}; / / initialize the data initData (); lvAddress.initData (this, lvAddressData); / / initialize the data lvAddress.setLastLevelItemClickCallBack (itemClickCallBack) / / set node click event listening} private void initData () {/ / Test data TreeElement root = new TreeElement (); root.setId ("01"); root.setLevel (1); root.setTitle ("friend"); root.setFold (false) Root.setHasChild (true); root.setHasParent (false); root.setParentId (null); lvAddressData.add (root); TreeElement twoLevel = new TreeElement (); twoLevel.setId ("0101"); twoLevel.setLevel (2) TwoLevel.setTitle ("junior high school classmate"); twoLevel.setFold (false); twoLevel.setHasChild (true); twoLevel.setHasParent (true); twoLevel.setParentId ("01"); lvAddressData.add (twoLevel); TreeElement threeLevel = new TreeElement () ThreeLevel.setId ("010101"); threeLevel.setLevel (3); threeLevel.setTitle ("get rich"); threeLevel.setFold (false); threeLevel.setHasChild (false); threeLevel.setHasParent (true); threeLevel.setParentId ("0101"); lvAddressData.add (threeLevel) } @ Override public boolean onCreateOptionsMenu (Menu menu) {getMenuInflater () .inflate (R.menu.activity_main, menu); return true;}}

Tree_view_item.xml layout content

This is the answer to the question about how to customize the tree in android. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report