In Android development, the ListView should be said to be one of the most commonly used components. This article does not have its basic application to explain, needs to know the related content the colleague, asks the degree Niang by oneself.
In practice, after the ListView has been initialized, there are some default actions to make, for example, I have a layout that shows the department and subordinates, asks the subordinate of the first department to appear on the right when the department list is loaded, and identifies the currently selected department (red symbol). such as
From the degree Niang can see, have this similar demand of peers in quite a few, but, either is the solution general strip, or is very complex analysis and solution.
For most people, it is a better answer to give a solution directly. Below, I will share my solution, if there is better, I hope you can share to me.
I am also a novice in Android, if there are inappropriate places, but also look at the advice.
--------------------------------------------------------------
1. Create a new adapter to rewrite the GetView method (this is the key).
public class Defaultselectedadapter extends Simpleadapter {private Defalutselectedhandler handler = null;// Defines a default selected processing interface public defaultselectedadapter (Context context,list<? extends Map<string,?>> data, int Resource, string[] from,int[] to, Defalutselectedhandler Toselect) {Super (context, data, resource, from, to); This.handler = Toselect;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {//TODO auto-generated method Stubview vie W = Super.getview (position, convertview, parent);//before returning to view, call the process interface method, which is not judged by position==0, to accommodate more If the last handler.doselected (position, view, parent) is selected by default, return view;} public interface defalutselectedhandler{void doselected (int position, view view, viewgroup parent);//define the desired default action in this method}}
2. The ListView is generated as follows:
Final ListView listorg = (ListView) Viewselect.findviewbyid (r.id.listorg);//Department list final int listsize = Listorgdata.size () ;//Department list data Defaultselectedadapter adapter = new Defaultselectedadapter (Context_, Listorgdata,r.layout.org_list_item, New string[] {"Relatename", "Relateid"},new int[] {r.id.relatename, r.id.relateid},new Defaultselectedadapter.defalutselectedhandler () {//The implementation of the default selected Processing interface Boolean Initflag = false;// Used to identify whether the processing interface has been successfully executed once @overridepublic void doselected (int position, view view, ViewGroup parent) {if (Position==0 & &!initflag) {//To determine whether the first item, and the processing interface has not completed View.findviewbyid (R.id.itemicon). setvisibility (view.visible);// Sets the symbol that precedes the item to be visible, indicating that it is currently selected. if (parent!=null) {int size_ = (ListView) parent). Getchildcount ();//Check the number of the current list, and the number of data is equal, that is, the ListView has been initially loaded to complete if (size_ >0 && listsize==size_) {//To this point, it can be determined that the processing interface has been successfully executed once initflag = True;}}}); Listorg.setadapter (adapter); Listorg.setonitemclicklistener (new Listview.onitemclicklistener () {Boolean FirstClick = true;//logo First click @overridepublic void OnitemclicK (adapterview<?> Adapterview, View item, final int position, long row) {if (position!=0 && firstclick) {//First time When clicked, the check symbol Firstclick = False;listorg.getchildat (0) is forced to hide the first item since it was not previously set to the ListView tag. Findviewbyid (R.id.itemicon). Setvisibility (View.gone);} Object tag = Adapterview.gettag (), if (tag!=null) {View item0 = (view) Tag;item0.findviewbyid (R.id.itemicon). Setvisibility (View.gone);//cancels the selection state of the current item}/** the Performitemclick method below does not make this sentence effective. * From the development of debug information, I personally feel that should be the execution of this sentence is covered by the GetView method of the ListView (GetView will be called multiple times) */item.findviewbyid (R.id.itemicon). Setvisibility (view.visible); Adapterview.settag (item);//Set the ListView tag, easy to click on other items, cancel the selection state of the current item//The following is my business processing string Relateid = ((TextView) Item.findviewbyid (R.id.relateid)). GetText (). toString (); String Relatename = ((TextView) Item.findviewbyid (R.id.relatename)). GetText (). toString (); hashmap<string, string> map = new hashmap<string, string> (); Map.put ("Relatename", Relatename); Map.put (" Relateid ", Relateid); Renderuserlist (viewselect, map);}); Alertdialog.builDer Builder = new Alertdialog.builder (context_); final Alertdialog dialogselect = Builder.setcancelable (True). Setview ( Viewselect). Settitle ("Select Recipient"). Create ();d ialogselect.show (); if (curorg!=null) {//Simulate click on the first item, Used to load Data Listorg.performitemclick (adapter.getview (0, NULL, NULL), 0, Adapter.getitemid (0));}
The above two steps are all the code that implements this function.
The core is that in the GetView to call your default selection of the processing method, just add a lot of decisions to prevent the list changes when repeated calls to GetView cause doselected is also repeated execution.
About the default selection action for Android ListView