Inflate ListView row from OnClickListener in Android?

后端 未结 2 457
执念已碎
执念已碎 2020-12-07 05:40

I have a class (A_Main.java) extending ArrayAdapter. I set my ListView to use A_Main as it\'s ListAdapter. I

相关标签:
2条回答
  • 2020-12-07 06:21

    Edit:

    Solution is probably simple Take boolean array globally like this

     private final boolean[] selectedstates;
    

    And initialize it with the size of your list in your constructor

     selectedstates= new boolean[yourlist.size()];
    

    And in out side of onclick listener set like this

     yourbutton.setSelected(selectedstates[position]);
    

    I hope this will help you


    Try this

    Take a custom selector with two different state images for selection and non selection

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:drawable="@drawable/pause_button"
              android:state_selected="true" />
        <item android:drawable="@drawable/play_button" />
    </selector>
    

    1.Create a global variable

    Imageview previous; 
    

    in your Custom Adapter and Initialize it in the constructor where you'll get the content

    previous=new ImageView(context);
    

    Add in your adapter getView() method you will probably have a onclickListener for your Imageview do like this

     imagPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 ImageView current=((ImageView)v);
                  current.setSelected(true);
                  previous.setSelected(false);
                  previous=current;
            }
        });
    

    This will work, I was confident because I have used it in my app. I hope this will help you

    0 讨论(0)
  • 2020-12-07 06:46

    Should look at this video. http://www.youtube.com/watch?v=wDBM6wVEO70. Especially the viewholder part to reuse views and avoid memory leaks. To highlight a listview row button check the position of the item on which you click and highlight the button by setting a background for the button.

    0 讨论(0)
提交回复
热议问题