ListView is often used during Android Application Development, and buttons and other controls in each Item need to be monitored for events. When you add an OnClickListener to a Button, you may subconsciously find each button in the getView method of the ListView adapter and assign an OnClickListener to it. However, when there is a large amount of data, the new listeners will inevitably put a certain amount of pressure on the memory, and each Listener has the same function. What you need to know in Listener is, it is the index of the Item where the caller is located. How can we make better use of memory?
Since each Listener has the same function, a Listener can be constructed in singleton mode. As follows:
1 class MyOnClickListener implements OnClickListener {
2
3 private static MyOnClickListener instance = null;
4
5 private MyOnClickListener (){
6}
7
8 public static MyOnClickListener getInstance (){
9 if (instance = null)
10 instance = new MyOnClickListener ();
11 return instance;
12}
13
14 @ Override
15 public void onClick (View view ){
16 // TODO: do something here
17}
18}
In the getView method, after obtaining the button instance, you only need to set the listener for the button through button. setOnClickListener (MyOnClickListener. getInstance. In this way, each button must use the same Listener object.
However, our requirements do not end with this. In many cases, we also need to know which position button is clicked. We need to make different actions in the Listener according to position.
To understand the attributes of external controls within the Listener, we first think of passing parameters, but because our Listener uses the singleton mode, parameters passed by each button to the Listner must overwrite the parameters passed by the previous button. Therefore, there is only one solution left, that is, the information is obtained through the onClick function parameter (View view. However, the view here should be a Button, and the Button does not have the position information. Naturally, the solution came out: Reload the Button class.
1 class MyButton extends Button {
2
3 private int index =-1;
4
5 public int getIndex (){
6 return index;
7}
8
9 public void setIndex (int index ){
10 this. index = index;
11}
12
13 public MyButton (Context context ){
14 super (context );
15 // TODO: do something here if you want
16}
17
18 public MyButton (Context context, AttributeSet attrs ){
19 super (context, attrs );
20 // TODO: do something here if you want
21}
22
23 public MyButton (Context context, AttributeSet attrs, int defStyle ){
24 super (context, attrs, defStyle );
25 // TODO: do something here if you want
26}
27}
Next, we need to change the type of the Button in the item to the custom MyButton in the xml file. Change <Button> </Button> to <your. package. name. myButton> </your. package. name. myButton>, while in the getView function of the adapter, the return value obtained by findViewById () is forcibly converted to MyButton, and its setIndex function is called to set the Index value. At the same time, the overload onClick function in MyOnClickListener converts the view object to the MyButton type, and calls the getIndex function to obtain the position information for corresponding operations.
Adapter:
1 //....
2 MyButton button = null;
3 //....
4 @ Override
5 public View getView (int position, View convertView, ViewGroup parentView ){
6 View view = convertView;
7 if (convertView = null ){
8 view = LayoutInflater. from (activity). inflate (R. layout. company_detail_campus_talk_item, null );
9}
10
11 //....
12
13 button = (MyButton) view. findViewById (R. id. YOUR_BUTTON_ID );
14 button. setIndex (position );
15 button. setOnClickListener (MyOnClickListener. getInstance ());
16}
In MyOnClickListener:
1 //....
2
3 @ Override
4 public void onClick (View view ){
5
6 int index = (MyButton) view). getIndex ();
7
8 //....
9}
In this way, we implement the business logic of using the same Listener to listen to events for different items in the ListView. To share data between the Adapter and Listener, you can add parameters of the Listener getInstance function and member variables of the Listener class.