Button listener setting and Optimization in ListView, listview listener

Source: Internet
Author: User

Button listener setting and Optimization in ListView, listview listener

ListView is often used in application development, and each Item contains buttons and other controls that require event listening. 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:

class MyOnClickListener implements OnClickListener {   private static MyOnClickListener instance = null;        private MyOnClickListener() {    }   public static MyOnClickListener getInstance() {       if (instance == null)            instance = new MyOnClickListener() ;        return instance;    }   @Override     public void onClick(View view) {        //TODO: do something here   }}


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. However, because our Listener uses the singleton mode, the parameters transmitted by each button to the Listner will inevitably

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,

The Button does not have the position information. Naturally, the solution came out: Reload the Button class.


class MyButton extends Button {    private int index = -1;    public int getIndex() {        return index;    }   public void setIndex(int index) {        this.index = index;   }    public MyButton(Context context) {        super(context);         // TODO: do something here if you want     }    public MyButton(Context context, AttributeSet attrs) {         super(context, attrs);         // TODO: do something here if you want    }    public MyButton(Context context, AttributeSet attrs, int defStyle) {       super(context, attrs, defStyle);        // TODO: do something here if you want     } }



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:

// ....MyButton button = null;// ....@Overridepublic View getView(int position, View convertView, ViewGroup parentView) {   View view = convertView;   if (convertView == null) {        view = LayoutInflater.from(activity).inflate(R.layout.company_detail_campus_talk_item, null);     }    // ....   button = (MyButton) view.findViewById(R.id.YOUR_BUTTON_ID);    button.setIndex(position);    button.setOnClickListener(MyOnClickListener.getInstance());}



In MyOnClickListener:

// ....@Overridepublic void onClick(View view) {    int index = ((MyButton)view).getIndex();   // ....}


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.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.