2016-10-26 3 views
1

Ich habe ein seltsames Problem, und ich bin nicht sicher, genannt, was man gerade geht. Ich habe es geschafft, das Problem zu umgehen, indem ich manuell überprüfe, ob das erste Element in der Listenansicht, das überprüft wurde, das ist, das den zweiten Anruf bekommt.onItemCheckedStateChanged zweimal

Das Problem ist, dass, wenn der Benutzer lange auf einem Listview-Element klicken (onlongclicklistener innerhalb des Adapters), wird das Listview-Element überprüft. Nachdem der Artikel veröffentlicht wurde (langer Klick wird freigegeben), wird der Artikel auf isChecked = false gesetzt. Ich kann nicht herausfinden, was dieses Problem verursacht. Wenn ich Correclty verstehen, setMultiChoiceModeListener hat ein eigener onlongclick Zuhörer, die aufgerufen wird. Gibt es eine Möglichkeit, es zu überschreiben und die gleiche Funktionalität wie die meisten Apps zu erreichen (langer Klick startet das Auswahlverfahren)?

Adapter klicken Zuhörer

vi.setOnLongClickListener(new View.OnLongClickListener() { 
      @Override 
      public boolean onLongClick(View v) { 
       Log.d("test abc", "ovo je krenulo"); 
       ((MainActivity) mContext).list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); 
       ((MainActivity) mContext).lvPos = position; 
       ((MainActivity) mContext).lvPosChecked = true; 

       if(!((MainActivity) mContext).ListSelectionInProgress) 
       { 
        ((MainActivity) mContext).ListSelectionInProgress = true; 
       } 

       if(!((MainActivity) mContext).list.isItemChecked(position)) 
       { 
        ((MainActivity) mContext).list.setItemChecked(position, true); 
       } 
       else 
       { 
        ((MainActivity) mContext).list.setItemChecked(position, false); 
       } 
       return true; 
      } 
     }); 

     vi.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Log.d("CL", "State called"); 

       if(((MainActivity) mContext).ListSelectionInProgress) 
       { 
        ((MainActivity) mContext).lvPos = position; 
        ((MainActivity) mContext).lvPos = -1; 
        if(!((MainActivity) mContext).list.isItemChecked(position)) 
        { 
         ((MainActivity) mContext).list.setItemChecked(position, true); 
        } 
        else 
        { 
         ((MainActivity) mContext).list.setItemChecked(position, false); 
        } 

        Log.d("State", Integer.toString(((MainActivity) mContext).list.getCheckedItemCount())); 
       } 
      } 

     }); 

setMultiChoiceModeListener

list.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() { 
      @Override 
      public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) { 
       if(position == lvPos && checked != lvPosChecked) 
       { 
        list.setItemChecked(position, true); 
       } 
       mode.setTitle(Integer.toString(list.getCheckedItemCount())); 
      } 

      @Override 
      public boolean onCreateActionMode(ActionMode mode, Menu menu) 
      { 
       ListSelectionInProgress = true; 
       MenuInflater inflater = mode.getMenuInflater(); 
       if(SUBLEVEL == 1) { 
        inflater.inflate(R.menu.action_mode_add, menu); 
       } 
       else if(SUBLEVEL == 5) // SATNICA 
       { 
        inflater.inflate(R.menu.action_mode_remove, menu); 
       } 

       return true; 
      } 

      @Override 
      public boolean onPrepareActionMode(ActionMode mode, Menu menu) { 
       return false; 
      } 

      @Override 
      public boolean onActionItemClicked(ActionMode mode, MenuItem item) { 

       int c = 0; 
       switch(item.getItemId()) 
       { 
        case R.id.selAll: 
         for(int i=0;i<list.getAdapter().getCount();i++) { 
          list.setItemChecked(i, true); 
         } 
         break; 
        case R.id.addSch: 
         c = 0; 
         for(int i=0;i<list.getAdapter().getCount();i++) 
         { 
          if(list.isItemChecked(i)) 
          { 
           addToSchedule(i); 
           c++; 
          } 
         } 
         if(c > 1) 
         { 
          Toast.makeText(MainActivity.this, R.string.addToMyScheduleSuccessPlural, Toast.LENGTH_SHORT).show(); 
         } 
         else 
         { 
          Toast.makeText(MainActivity.this, R.string.addToMyScheduleSuccess, Toast.LENGTH_SHORT).show(); 
         } 
         mode.finish(); 
         break; 
        case R.id.remSch: 
         c = 0; 
         if(list.getAdapter() != null) 
         { 
          for(int i=0;i<list.getAdapter().getCount();i++) 
          { 
           if(list.isItemChecked(i)) 
           { 
            removeFromSchedule(i); 
            c++; 
           } 
          } 
          if(c > 1) { 
           Toast.makeText(MainActivity.this, R.string.removeFromMyScheduleSuccessPlural, Toast.LENGTH_SHORT).show(); 
          } 
          else 
          { 
           Toast.makeText(MainActivity.this, R.string.removeFromMyScheduleSuccess, Toast.LENGTH_SHORT).show(); 
          } 
          loadMySchedule(); 
          mode.finish(); 
         } 

         break; 
       } 

       return false; 
      } 

      @Override 
      public void onDestroyActionMode(ActionMode mode) { 
       ListSelectionInProgress = false; 
      } 
     }); 

listview_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="80dp" 
    android:id="@+id/classItemLV" 
    android:background="@drawable/list_row" 
    android:minHeight="80dp" 
    android:descendantFocusability="blocksDescendants" 
    android:elevation="2dp"> 

    <RelativeLayout 
     android:layout_width="16dp" 
     android:layout_height="wrap_content" 
     android:id="@+id/lday" 
     android:layout_alignParentStart="true" 
     android:layout_marginStart="10dp" 
     android:layout_centerVertical="true" 
     android:background="@color/colorPrimary" 
     android:minWidth="16dp"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:text="P\nO\nN" 
      android:id="@+id/DAY1" 
      android:layout_alignParentStart="false" 
      android:textColor="#fff" 
      android:textSize="15sp" 
      android:layout_margin="2dp" 
      android:textAlignment="center" 
      android:layout_centerInParent="true" /> 

    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/lTime" 
     android:layout_alignWithParentIfMissing="false" 
     android:layout_centerVertical="true" 
     android:layout_marginStart="10dp" 
     android:layout_toEndOf="@+id/lday"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:text="10" 
      android:id="@+id/timeFrom" 
      android:textColor="@color/colorLV3Dark" 
      android:layout_alignParentTop="true" 
      android:layout_alignParentStart="true" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textIsSelectable="false" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:text="12" 
      android:id="@+id/timeTo" 
      android:textColor="@color/colorLV3Dark" 
      android:layout_below="@+id/timeFrom" /> 
    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/lInfo" 
     android:layout_marginStart="10dp" 
     android:layout_centerVertical="true" 
     android:layout_toEndOf="@id/lTime"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:text="Analiza financijskih izvještaja" 
      android:id="@+id/className" 
      android:textColor="@color/colorLV3Dark" 
      android:layout_alignParentStart="true" 
      android:layout_marginEnd="10dp" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:text="prof. dr. sc. Ivan Ivić" 
      android:id="@+id/classTutor" 
      android:focusableInTouchMode="false" 
      android:textColor="@color/colorLV3" 
      android:layout_below="@+id/className" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:text="PREDAVANJE - DV41" 
      android:id="@+id/classTypePlace" 
      android:textIsSelectable="false" 
      android:textColor="@color/colorLV3" 
      android:textSize="12sp" 
      android:layout_below="@+id/classTutor" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:text="Datum: 5.10.-25.1." 
      android:id="@+id/classDate" 
      android:clickable="false" 
      android:textSize="12sp" 
      android:textColor="@color/colorLV3" 
      android:layout_alignParentEnd="true" 
      android:layout_alignBottom="@+id/classTypePlace" 
      android:layout_marginEnd="10dp" /> 
    </RelativeLayout> 

</RelativeLayout> 

Ich habe den Code, so gut ich kann, und ich habe folgende bemerkt debuggt:

1) onItemCheckedStateChanged aufgerufen wird, nach dem langen Klick

freigegeben wird

2) OnClick wird aufgerufen, normal und wird nicht von der longclick betroffen haupt Hörer (mit Ausnahme der Auswahl ermöglicht Trog ein public boolean Variable)

3), wenn ich außerhalb der Ansicht, meine Finger bewegen, oder auf der anderen Seite davon, während halten das Touch-Ereignis nach unten, bleibt geprüft

4), wenn ich schnell ein anderes Element auswählen, überprüft sowohl Aufenthalt

Es scheint, wie etwas, das mein Listview-Element des Überprüfungsstatus wird überschreiben. Was könnte es sein?

Antwort

1

Nach zwei Tagen meinen Kopf kratzen, ich es endlich geschafft, eine Lösung zu finden. Ich habe mit meinen normalen und langen Klicks in einem Adapter gearbeitet.

Scheint, wie

setMultiChoiceModeListener 

beide von denen selbst Griffe und es war nicht nötig, diese beiden Arten von Klicks manuell zu handhaben. Ich wünschte, ich hätte es früher irgendwo gelesen. Ich hoffe, diese Antwort wird auch in Zukunft jemandem helfen.