2012-05-23 9 views
2

Falks, 어리석은 실수가 될 것입니다.하지만 실제로 어디 있는지 추측 할 수 없습니다. OnTouchListener를 설정하려고했지만 onTouch 메소드가 호출되지 않았습니다.OnTouchListener를 설정할 수 없습니다.

Here`s 내 코드 :

public class StepActivity extends SherlockActivity 
    { 
    private Recipe recipe = null; 
    private String TAG = "StepActivity"; 
    private ArrayList<Step> steps; 
    private int caret = 0; 
    private int currentStep = 1; 
    private int stepQuantity; 
    private TextView viewStepBody; 
    private TextView stepNumber; 
    private ImageView viewPicture; 
    private String bodyText; 
    private String picture; 
    private String [] pictures = new String [5]; 



    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.step_page); 
     RelativeLayout view = new RelativeLayout(this); 
     view.setOnTouchListener(new OnTouchListener() { 
      @Override 
      public boolean onTouch(View view, MotionEvent motion) { 

       Log.i(TAG, "In onTouch"); 
       float downXValue = 0; 
       // Get the action that was done on this touch event 
       switch (motion.getAction()) 
       { 

        case MotionEvent.ACTION_DOWN: 
        { 
         Log.i(TAG, "In onTouch1"); 
         // store the X value when the user's finger was pressed down 
         downXValue = motion.getX(); 
         break; 
        } 

        case MotionEvent.ACTION_UP: 
        { 
         Log.i(TAG, "In onTouch2"); 
         // Get the X value when the user released his/her finger 
         float currentX = motion.getX();    
         // going backwards: pushing stuff to the right 
         if (downXValue < currentX) 
         { 
          Log.i(TAG, "In onTouch previous"); 
          goPrevious(); 
         } 

         // going forwards: pushing stuff to the left 
         if (downXValue > currentX) 
         { 
          Log.i(TAG, "In onTouch next"); 
          goNext(); 
         } 
         break; 
        } 
       } 
       // if you return false, these actions will not be recorded 
       return true; 
      } 

     }); 

     viewStepBody = (TextView)findViewById(R.id.step_body); 
     stepNumber = (TextView)findViewById(R.id.step_number); 
     viewPicture = (ImageView)findViewById(R.id.picture); 


     TextView recipeTitle = (TextView)findViewById(R.id.recipe_title); 
     try { 
      getSupportActionBar().setDisplayShowTitleEnabled(false); 
      recipe = (Recipe)getIntent().getSerializableExtra("Recipe1"); 
      steps = recipe.getSteps(); 
      stepQuantity = steps.size(); 

      Log.d(TAG,"steps: " + steps.size()); 
      if (stepQuantity > 0) { 
       Step step = steps.get(0); 
       pictures[0] = Constants.URL + step.getImg_url(); 
       bodyText = step.getInstruction(); 
       new DownloadImageTask().execute(pictures); 
       recipeTitle.setText(recipe.getTitle()); 
      } 
      updateInfo(); 
     } catch (Exception e) { 
      Toast.makeText(this, "Error occured" + e.getMessage(), 200); 
     } 

    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater(); 
     inflater.inflate(R.menu.step_menu, (com.actionbarsherlock.view.Menu) menu); 
     return super.onCreateOptionsMenu(menu); 
    } 

    private void updateInfo() { 
     new DownloadImageTask().execute(pictures); 
     viewStepBody.setText(bodyText); 
     stepNumber.setText(currentStep + "/" + stepQuantity); 
    } 

    private void goNext() { 
     if (currentStep != stepQuantity) { 
      caret++; 
      currentStep++; 
      Step newStep = steps.get(caret); 
      pictures[0] = Constants.URL + newStep.getImg_url(); 
      bodyText = newStep.getInstruction(); 
      updateInfo(); 
     } else { 
      caret = 0; 
      currentStep = 1; 
      Step newStep = steps.get(caret); 
      bodyText = newStep.getInstruction(); 
      pictures[0] = Constants.URL + newStep.getImg_url(); 
      updateInfo(); 
     } 
    } 

    private void goPrevious() { 
     if (currentStep != 1) { 
      caret--; 
      currentStep--; 
      Step newStep = steps.get(caret); 
      bodyText = newStep.getInstruction(); 
      pictures[0] = Constants.URL + newStep.getImg_url(); 
      updateInfo(); 
     } 
     else { 
      caret = stepQuantity - 1; 
      currentStep = stepQuantity; 
      Step newStep = steps.get(caret); 
      bodyText = newStep.getInstruction(); 
      pictures[0] = Constants.URL + newStep.getImg_url(); 
      updateInfo(); 
     } 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch(item.getItemId()) { 
     case R.id.next: 
      goNext(); 
      return true; 
     case R.id.previous: 
      goPrevious(); 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
     } 
    } 

Here`s 내 step_page.xml 된 setContentView() 메서드에 전달.

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/step_layout1"> 
<ScrollView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <TextView android:id="@+id/recipe_title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="23dp" 
      android:layout_alignParentRight="true" 
      android:layout_alignParentTop="true" 
      android:textColor="#9d9d9d" 
      android:textStyle="bold" 
      android:layout_marginTop="4dp"/> 

     <TextView android:id="@+id/step_word" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="25dp" 
      android:layout_below="@+id/recipe_title" 
      android:textColor="#000000" 
      android:textStyle="bold" 
      android:text="@string/step_word" 
      android:layout_marginTop="4dp"/> 

     <TextView android:id="@+id/step_number" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="25dp" 
      android:layout_toRightOf="@+id/step_word" 
      android:layout_below="@+id/recipe_title" 
      android:textColor="#000000" 
      android:textStyle="bold" 
      android:layout_marginLeft="3dp" 
      android:text="1/12" 
      android:layout_marginTop="4dp"/> 



     <ImageView 
      android:id="@+id/picture" 
      android:layout_width="300dp" 
      android:layout_height="200dp" 
      android:layout_below="@+id/step_word" 
      android:layout_centerHorizontal="true" 
      /> 

     <TextView android:id="@+id/step_body" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="22dp" 
      android:textColor="#000000" 
      android:paddingTop="2dp" 
      android:layout_below="@+id/picture" 
      /> 

    </RelativeLayout> 
</ScrollView> 
</RelativeLayout> 

여기서 한 가지 더 많은 접근법이 효과가 없습니다. :

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.step_page); 
    RelativeLayout view = (RelativeLayout) findViewById(R.id.step_layout1); 
    view.setOnTouchListener(new OnTouchListener() { 
     @Override 
     public boolean onTouch(View view, MotionEvent motion) { 

      Log.i(TAG, "In onTouch"); 
      float downXValue = 0; 
      // Get the action that was done on this touch event 
      switch (motion.getAction()) 
      { 

       case MotionEvent.ACTION_DOWN: 
       { 
        Log.i(TAG, "In onTouch1"); 
        // store the X value when the user's finger was pressed down 
        downXValue = motion.getX(); 
        break; 
       } 

       case MotionEvent.ACTION_UP: 
       { 
        Log.i(TAG, "In onTouch2"); 
        // Get the X value when the user released his/her finger 
        float currentX = motion.getX();    
        // going backwards: pushing stuff to the right 
        if (downXValue < currentX) 
        { 
         Log.i(TAG, "In onTouch previous"); 
         goPrevious(); 
        } 

        // going forwards: pushing stuff to the left 
        if (downXValue > currentX) 
        { 
         Log.i(TAG, "In onTouch next"); 
         goNext(); 
        } 
        break; 
       } 
      } 
      // if you return false, these actions will not be recorded 
      return true; 
     } 

    }); 

비고란?

+0

예외가 있습니까? – Praveenkumar

+0

아니요, onTouch 메소드의 반응이 없습니다. Log.d (TAG, "Message")가 몇 개 있습니다. 그 중 아무 것도 호출되지 않습니다. – Stas

+0

'RelativeLayout' 출력을 볼 수 있습니까? – Praveenkumar

답변

1

내가 생각한 것과 반대로 RelativeLayout view = new RelativeLayout(this);은 활동의 레이아웃에 대한 부모 RelativeLayout보기를 제공하지 않습니다. 부모 RelativeLayout에 id를 할당하고, findViewById를 사용하여 ID를 보유한 다음 onTouchListener 등을 첨부 할 수 있습니다.

편집 (당신이 XML 레이아웃을 게시 이후)

부모 RelativeLayout의

가 노출되지 않습니다 - 터치 접근 결코 뒤에 전체 화면이있는 ScrollView에 의해 점령되고, 그래서 RelativeLayout의. 내부 RelativeLayout (ScrollView 내부의 클릭 가능보기)이 없으므로 onTouch를 첨부 할 수 있습니다.

+0

나는 당신이 말하는 것처럼 질문을 편집하고 코드를 추가했습니다. 그것은 잘 작동하지 않습니다. 내가 뭘 잘못하고 있니? – Stas

+0

내 편집 된 답변보기 – josephus

+0

감사합니다. 하지만 이제는 비어있는 표면을 미끄러 져 움직이는 경우에만 작동합니다. 사진 또는 textView 경우 - 그것은 작동하지 않습니다. 이 경우에는 onTouthListener를 textView 및 사진으로 설정해야합니까? – Stas

관련 문제