Android ListView ImageView Implementation radio button instance _android

Source: Internet
Author: User

To do Android development for two years, the technology has a little upgrade, just the function of their realization to write out, record, if you can help other people in the peer, I have done a good thing, haha!! No more nonsense, just the last picture.

First the previous code:

1 if (lastposition = = position) {
2           viewholder.setimageresource (r.id.iv_yuandian1,r.mipmap.ic_button_ checked);
3         } else {
4           viewholder.setimageresource (r.id.iv_yuandian1,r.mipmap.ic_button_unchecked);
5         }

I first record the default position in the adapter, because I built a listviewadapter abstract class in the project, so each time I use it, I just need to make a new one in the activity, and then set the text content or picture resources directly in the method ( As the above code to write), will share with you this abstract class, of course, this abstract class or under the guidance of the master to complete, we can ignore this writing, according to the normal baseadapter written on the line. Pull it off, say back to the subject. In fact, the main part of the entire feature in the ListView item click event, its main code is as follows:

Lv_choosecompany.setonitemclicklistener (New Adapterview.onitemclicklistener () {
      @Override public
      Void Onitemclick (adapterview<?> adapterview, view view, int position, long L) {
        //Select the currently selected entry with the selected effect
        old_ ImageView = (ImageView) View.findviewbyid (r.id.iv_yuandian1);
        Old_imageview.setimageresource (r.mipmap.ic_button_checked);

        New_imageview = (ImageView) View.findviewbyid (r.id.iv_yuandian1);
        if (lastposition!=-1 && lastposition!= position) {
          //If the entry has already been clicked and the last saved item position differs from the current position
          New_ Imageview.setimageresource (r.mipmap.ic_button_unchecked)//Remove the last selected style
        }
        New_imageview = Old_imageview //Save the current entry
        lastposition = position;//Save the current position

        listviewadapter.notifydatasetchanged ();

      }

    );

The whole process is almost like this, if you want to retain the selected text information, only need to click on the time, will be the content of the TextView to save, here is not the display code, the lack of places to ask the colleagues a lot of advice.

Thank you for reading, I hope to help you, thank you for your support for this site!

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.