Android: ListView bug

前端 未结 4 734
余生分开走
余生分开走 2021-01-15 00:58

I have a custom list view which has a Textview and an image. When I click on the textview a hidden layout will be expanded for that particular row. But what happening was, f

4条回答
  •  佛祖请我去吃肉
    2021-01-15 01:21

    You need to add a attribute to your adapter, a simple int. When you click on an item, update the current selected position. When you build the view, check if position equals the selected position.

    public View getView(final int position, View convertView, ViewGroup parent) {
         if(this.selectedPosition == position){
         //add a holder
         else{
         //don't add a holder
    
         return convertview;
    }
    

    What I'm saying is that you are encountering a View recycling problem. If you modify item n°2 and then the view of item n°10 is build from the View n°2, you end up with an item unwanted (that will look as it was clicked).

    When an item of your list is clicked:

    1) update the selected item (attribute of your adapter). Example, if you click on item 12 of your listview, selectedItem = 12;

    2) call method notifyDataSetChanged(). This will refresh the view of the list.

    3) When you build the view (getView() of adapter), check for each position if it corresponds to the selected item. If it does, build your special view (I don't know exactly what you want to do). If it doesn't correspond to the selected item, build your view "normally"

提交回复
热议问题