When scrolling custom ListView, the checkbox value changes

前端 未结 8 1948
不思量自难忘°
不思量自难忘° 2020-12-05 07:30


What I have: a custom listview with Textviews and checkbox.


Problem: Suppose my screen can show only 6 items of list at time, and
8条回答
  •  暖寄归人
    2020-12-05 07:54

    I have a good alternative for a CheckBox. You can use android:state_activated="true" for your ListView rows. It does not lose its state and does not require lots of code.

    Here is how to works

    • create active_row.xml in your drawable folder


    
        
    
    • Set the the Background of your Row layout as android:background="@drawable/active_row.xml"
    • And finally add this to your ListView android:choiceMode="multipleChoice"

    You can check the State of every row like this

    View v;
    
    LinearLayout yourRowLayout;
    for (int i = 0; i < yourListView.getChildCount(); i++) {
    v = yourListView.getChildAt(i);
    yourRowLayout = (LinearLayout) v.findViewById(R.id. yourRowLayout);
    if(yourRowLayout.isActivated()){....  }  }
    

提交回复
热议问题