Home > Mobile >  Problems after the Listview to the Treeview
Problems after the Listview to the Treeview

Time:11-25

Listview to Treeview is implemented, but I want to achieve the following effects:


ItemClick events of the following is to monitor events, and can't distinguish which one is an Item of controls by clicking:

Public void onItemClick (AdapterView<?> The parent, View View, int position, long id) {
//... The code is slightly...
}


Attach treeview_item. XML source code is as follows:

<? The XML version="1.0" encoding="utf-8"?>

The android: id="@ + id/ll_Main"
Android: layout_width="match_parent"
Android: layout_height="wrap_content"
Android: fitsSystemWindows="true"
Android: orientation="vertical" & gt;


The android: id="@ + id/llItem"
Android: layout_width="match_parent"
Android: layout_height="43 dp"
The android: orientation="horizontal" & gt;

The android: id="@ + id/disclosureImg"
Android: layout_width="wrap_content"
Android: layout_height="wrap_content"
Android: layout_alignParentLeft="false"
Android: layout_centerVertical="false"
Android: layout_gravity="center_vertical"/& gt;

The android: id="@ + id/deviceImg"
Android: layout_width="wrap_content"
Android: layout_height="wrap_content"
Android: layout_alignParentLeft="false"
Android: layout_centerVertical="false"
Android: layout_gravity="center_vertical"
Android: layout_marginLeft="5 dp/& gt;"

The android: id="@ + id/contentText"
Android: layout_width="wrap_content"
Android: layout_height="wrap_content"
Android: layout_centerVertical="false"
Android: layout_gravity="center_vertical"
Android: layout_marginLeft="5 dp"
Android: textColor="# 000000"
Android: textSize="14 sp/& gt;"






Have a master who can solve this problem?

CodePudding user response:

Or also, convenient for your reference, according to the following basic method of dealing with the click event:

Public class TreeViewItemClickListener implements OnItemClickListener {
Adapter/* * */
Private TreeViewAdapter TreeViewAdapter;

Public TreeViewItemClickListener (TreeViewAdapter TreeViewAdapter) {
Enclosing treeViewAdapter=treeViewAdapter;
}

@ Override
Public void onItemClick (AdapterView<?> The parent, View View, int position,
Long id) {
//click on the item of representative element
Element Element=(Element) treeViewAdapter. The getItem (position);
//tree of elements in the
ArrayList Elements=treeViewAdapter. GetElements ();
//elements of the data source
ArrayList ElementsData=(https://bbs.csdn.net/topics/treeViewAdapter.getElementsData);

//click no child item returned directly
if (! Element. IsHasChildren ()) {
return;
}

{if (element. IsExpanded ())
Element. The setExpanded (false);
//delete node corresponding internal child node data, including the child nodes of the child node...
ArrayList ElementsToDel=new ArrayList (a);
For (int I=position + 1; I & lt; Elements. The size (); I++) {
If (element. GetLevel () & gt;=elements. The get (I). GetLevel ())
break;
ElementsToDel. Add (elements. The get (I));
}
Elements. RemoveAll (elementsToDel);
TreeViewAdapter. NotifyDataSetChanged ();
} else {
Element. The setExpanded (true);
//child nodes data extracted from the data source added to the tree, note that there is only added to the next level child nodes, in order to simplify the logic
Int I=1;//note that the counter on the outside for counting to ensure effective
For (Element e: elementsData) {
If (um participant etParendId ()==element. The getId ()) {
E.s etExpanded (false);
Elements. The add (position + I, e);
I + +;
}
}
TreeViewAdapter. NotifyDataSetChanged ();
}
}

}

Don't distinguish which is the control of an Item by clicking

CodePudding user response:

Or can handle in Adapter
Adapter is to be able to get to the component, the component and event listeners,
In the new adapter when hand in the Listener,
Probably this
A custom interface MyListener
The activity of
ListView. Setadapter (new MyAdapter (data, the listener));
.

in the adapterPublic MyAdapter (List data, MyListenerlistener) {
MListener=listener;
}

Private getView (int position) {
The view button=... ;
View the text=... ;

Button. Setonxxx () {
MListener. OnClick (' BTN, position);
}

Text. Setonxxx () {
MListener. OnClick (TXT, position);
}
}
  • Related