2014-01-23 2 views
1

조각을 확장하는 "HomeFragment"클래스가 있습니다. 모든 것이 잘 작동하고 있습니다. 단편이 처음 만들어졌습니다. 그러나 다른 조각으로 전환하여 다시 돌아 오면 UI가 업데이트되지 않습니다. 사실 TextSwitcher에서는 아무 것도 보여주지 않습니다. LogQ를 사용하여 ChangeText 스레드가 백그라운드에서 항상 실행되고 있는지 확인했습니다. 실수는 어디 있습니까?조각이 다시 시작될 때 AsyncTask UI 업데이트 안 함

public class HomeFragment extends Fragment { 

    private TextSwitcher mSwitcher; 
    String textToShow[]={"Main HeadLine","Your Message"}; 
    int messageCount=textToShow.length; 
    int currentIndex=-1; 
    private Handler myHandler; 
    ChangeText CT; 

    public HomeFragment(){} 

    @SuppressLint("HandlerLeak") 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     View rootView = inflater.inflate(R.layout.fragment_home, container, false); 

     mSwitcher = (TextSwitcher) rootView.findViewById(R.id.textSwitcher); 
     mSwitcher.setFactory(new ViewFactory() { 

      public View makeView() { 
       // TODO Auto-generated method stub 
       TextView myText = new TextView(getActivity()); 
       myText.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL); 
       myText.setTextSize(36); 
       myText.setTextColor(Color.BLUE); 
       return myText; 
      } 
     }); 

// Declare the in and out animations and initialize them 
Animation in = AnimationUtils.loadAnimation(getActivity(),android.R.anim.slide_in_left); 
Animation out = AnimationUtils.loadAnimation(getActivity(),android.R.anim.slide_out_right); 
     mSwitcher.setInAnimation(in); 
     mSwitcher.setOutAnimation(out); 


     myHandler = new Handler() { 
      public void handleMessage(Message msg) { 
       doUpdate(); 
      } 
     }; 




     CT = new ChangeText(); 
     CT.execute(); 

     return rootView; 
    } 

class ChangeText extends AsyncTask<String, String, String> { 

     @Override 
     protected String doInBackground(String... f_url) { 
      try { 

       while(true) 
       { 
        if (isCancelled()) break; 
        myHandler.sendEmptyMessage(currentIndex); 
        SystemClock.sleep(1500); 
       } 


      } catch (Exception e) { 
       Log.e("VIVEK: ", e.getMessage()); 
      } 

      return null; 
     } 

    } 

private void doUpdate() { 
     currentIndex++; 
     if(currentIndex==messageCount) 
     { 
      currentIndex=0; 
     } 
     Log.d("VIVEK", textToShow[currentIndex]); 
     mSwitcher.setText(textToShow[currentIndex]); 
    } 
} 
+0

asynctask에서 onPostExecute() 메서드를 추가하고 doUpdate()를 호출하십시오. –

+0

onPostExecute()에서 무엇을해야합니까? – gomchikbhoka

+0

doUpdate() 호출 그 방법은 조각을 업데이 트하는 경우, havent 정말 모든 코드를 읽을 것 : D 예를 확인하십시오 : http://stackoverflow.com/questions/9671546/asynctask-android-example?rq=1 –

답변

0

먼저, CountDownTimer 클래스를 살펴 보시기 바랍니다. 나는 당신의 유스 케이스에 더 적합하다고 생각합니다. 둘째, 이러한 이동하십시오 :

CT = new ChangeText(); 
CT.execute(); 

onResume() 조각의 및이 문제가 해결되는지 확인합니다.