1

사용자가 숫자를 실시간으로 기본 변환 할 수있는 앱을 개발 중입니다. 사용자는 편집 텍스트에서 번호를 입력하고, 플러스 및 마이너스 버튼을 사용하여베이스를 선택합니다. 지금까지 내가 직면 한 문제는 실시간 변환을 제공하는 것입니다. 리사이클 러 뷰 내의 모든 editText는 텍스트를베이스에 따라 변환 할 수있는 BigInteger로 설정합니다.RecyclerView 내에서 TextWatcher.onTextChanged() 내에서 notifyDataSetHasChanged()가 발생하면 오류가 발생합니다.

내 아이디어는 사용자가 새 번호를 입력함에 따라 BigInteger를 업데이트하는 것이 었습니다. 따라서 사용자가 숫자를 입력 할 때마다 BigInteger를 업데이트 할 수 있어야하며 데이터가 변경된 것으로 리사이클 러 뷰에 알리고 텍스트 뷰 편집이 자동으로 업데이트되어야 함을 알립니다. 여기 내 ConvertViewHolder

public ConvertViewHolder(final View itemView) { 
     super(itemView); 
     mBaseTextView = (TextView) itemView.findViewById(R.id.baseLabel); 
     mEditText = (EditText) itemView.findViewById(R.id.numberEditText); 
     mMinusButton = (Button) itemView.findViewById(R.id.minusButton); 
     mPlusButton = (Button) itemView.findViewById(R.id.plusButton); 
     mRemoveButton = (ImageButton)itemView.findViewById(R.id.removeButton); 

     mMinusButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       if (mRoot.get(getPosition()) > MIN_BASE) { 
        mRoot.set(getPosition(), (mRoot.get(getPosition()) - 1)); 
        notifyDataSetChanged(); 
       } 
      } 
     }); 

     mPlusButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       if (mRoot.get(getPosition()) < MAX_BASE){ 
        mRoot.set(getPosition(), (mRoot.get(getPosition()) +1)); 
        notifyDataSetChanged(); 
       } 
      } 
     }); 

     mRemoveButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       mRoot.remove(getPosition()); 
       notifyDataSetChanged(); 
      } 
     }); 

     mEditText.addTextChangedListener(new TextWatcher() { 
      @Override 
      public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 
      } 

      @Override 
      public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { 
      } 

      @Override 
      public void afterTextChanged(Editable editable) { 
       if(editable.toString().length() > 0) { 
        // change Big Integer 
        mNumber.setDecimalNumber(editable.toString(), mRoot.get(getPosition())); 
        // notify change 
        notifyDataSetChanged(); 
       } 
      } 
     }); 


     // TODO: convert numbers at the same time 

    } 

    public void bindConverter(final int root){ 
     mBaseTextView.setText(String.format("%02d", root)); 

     // String containing all the allowed digits depending on base 
     String digits = mNumber.getScaleFromBase(root); 

     if (root < 11) { 
      // filter input 
      mEditText.setInputType(InputType.TYPE_CLASS_NUMBER | 
        InputType.TYPE_TEXT_FLAG_MULTI_LINE); 
      mEditText.setKeyListener(DigitsKeyListener.getInstance(digits)); 
      mEditText.setSingleLine(false); 
      mEditText.setMaxLines(2); 

     } else { 
      mEditText.setInputType(InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS | 
        InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS); 
      mEditText.setSingleLine(false); 
      mEditText.setMaxLines(2); 
      mEditText.setImeOptions(EditorInfo.IME_FLAG_NO_ENTER_ACTION); 
      // TODO: filter input for base higher than 10 
     } 

     // set editText to BigInteger displaying it in the correct base 
     // i.e. if the BigInteger is "8" it will be displayed as 8 if the base is 10 
     // and as 1000 if the base is 2 

     mEditText.setText(mNumber.getDecimalNumber(mRoot.get(getPosition()))); 
    } 
} 

입니다하지만 분명히 내가 컴파일러는 나에게이 오류 발생으로 TextWatcher.onTextChanged 내부 notifySetDataHasChanged()를 호출 할 수 없습니다 오전 :

java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling 

자명 꽤이지만, 불행하게도 가능한 해결 방법을 찾지 못했습니다.

답변

1
view.post(

    new Runnable() { 

     public void run() { notifyDatasetChanged(); }; 
    } 


); 

자명 :

호 RecyclerView가 레이아웃 또는 스크롤링을 계산 한 후,이 방법 (다음 루프)

자세한 설명 : 동기

  • 한 스레드 = 코드 흐름
  • 메인 스레드 = 루퍼
  • looper = 메시지 큐 = 실행 가능 = 루프

다른 가능한 솔루션 ??

리사이클 뷰 메서드를 종료 한 후 호출