2013-11-04 214 views
0

我跟着一個在線教程,展示瞭如何在Android應用程序中實現滑動操作,但是當我在設備上測試它時,它不會滑動。是否有人可以發現此實現中出現了什麼問題?滑動功能無法正常工作

我的主類看起來是這樣的:

public class MainActivity extends FragmentActivity implements OnClickListener{ 

    /** 
    * The {@link android.support.v4.view.PagerAdapter} that will provide 
    * fragments for each of the sections. We use a 
    * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which 
    * will keep every loaded fragment in memory. If this becomes too memory 
    * intensive, it may be best to switch to a 
    * {@link android.support.v4.app.FragmentStatePagerAdapter}. 
    */ 
    SectionsPagerAdapter mSectionsPagerAdapter; 

    /** 
    * The {@link ViewPager} that will host the section contents. 
    */ 
    ViewPager mViewPager; 
    EditText offsetLength,offsetDepth,ductDepth; 
    Button calculate; 
    //DemoCollectionPagerAdapter mDemoCollectionPagerAdapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     final Intent intent1=new Intent(this,AboutActivity.class); 
     final Intent intent2=new Intent(this,MainActivity.class); 
     offsetLength = (EditText)findViewById(R.id.offLength); 
     offsetDepth = (EditText)findViewById(R.id.offDepth); 
     ductDepth = (EditText)findViewById(R.id.ductDepth); 
     calculate = (Button)findViewById(R.id.calc); 
     calculate.setOnClickListener(this); 

     final ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater().inflate(R.layout.a,null); 

     // Create the adapter that will return a fragment for each of the three 
     // primary sections of the app. 
     mSectionsPagerAdapter = new SectionsPagerAdapter(
       getSupportFragmentManager()); 

     // Set up the ViewPager with the sections adapter. 
     mViewPager = (ViewPager) findViewById(R.id.pager); 
     mViewPager.setAdapter(mSectionsPagerAdapter); 

    } 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 

     try { 

      String getoffsetlength = offsetLength.getText().toString(); 
      String getoffsetdepth = offsetDepth.getText().toString(); 
      String getductdepth = ductDepth.getText().toString(); 

      double tri1,tri2; 
      double marking1,marking2,marking3; 

      double off1 = Double.parseDouble(getoffsetlength); 
      double off2 = Double.parseDouble(getoffsetdepth); 
      double off3 = Double.parseDouble(getductdepth); 

      Intent myIntent = new Intent(MainActivity.this, CalcResult.class); 
      myIntent.putExtra("number1", marking1); 
      myIntent.putExtra("number2", marking2); 
      myIntent.putExtra("number3", marking3); 
      startActivity(myIntent); 

      Toast.makeText(getBaseContext(), "Calculating!", Toast.LENGTH_SHORT).show(); 


     } catch (NumberFormatException e) { 
      // TODO: handle exception 
      System.out.println("Must enter a numeric value!"); 


     } 
//  
    } 

    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 

     return true; 
    } 

    /** 
    * A {@link FragmentPagerAdapter} that returns a fragment corresponding to 
    * one of the sections/tabs/pages. 
    */ 
    public class SectionsPagerAdapter extends FragmentPagerAdapter { 

     public SectionsPagerAdapter(FragmentManager fm) { 
      super(fm); 
     } 

     @Override 
     public Fragment getItem(int position) { 
      // getItem is called to instantiate the fragment for the given page. 
      // Return a DummySectionFragment (defined as a static inner class 
      // below) with the page number as its lone argument. 
      Fragment fragment = new DummySectionFragment(); 
      Bundle args = new Bundle(); 
      args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1); 
      fragment.setArguments(args); 
      return fragment; 
     } 

     @Override 
     public int getCount() { 
      // Show 3 total pages. 
      return 3; 
     } 

     @Override 
     public CharSequence getPageTitle(int position) { 
      Locale l = Locale.getDefault(); 
      switch (position) { 
      case 0: 
       return getString(R.string.title_section1).toUpperCase(l); 
      case 1: 
       return getString(R.string.title_section2).toUpperCase(l); 
      case 2: 
       return getString(R.string.title_section3).toUpperCase(l); 
      } 
      return null; 
     } 
    } 

    /** 
    * A dummy fragment representing a section of the app, but that simply 
    * displays dummy text. 
    */ 
    public static class DummySectionFragment extends Fragment { 
     /** 
     * The fragment argument representing the section number for this 
     * fragment. 
     */ 
     public static final String ARG_SECTION_NUMBER = "section_number"; 

     public DummySectionFragment() { 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.fragment_main_dummy, 
        container, false); 
      TextView dummyTextView = (TextView) rootView 
        .findViewById(R.id.section_label); 
      dummyTextView.setText(Integer.toString(getArguments().getInt(
        ARG_SECTION_NUMBER))); 
      return rootView; 
     } 
    } 

} 
+0

您能否提供教程鏈接? – Rahin

回答

0

嘗試使用viewpageindicator使您的應用程序將可以更低的API也和它在這裏更容易爲ofiical網站:

http://viewpagerindicator.com/

,這裏是如此漂亮的教程:

http://forum.xda-developers.com/showthread.php?t=2450910

,或者您可以使用OnSwipeTouchListener類巫不建議:

在您的項目首先創建OnSwipeTouchListener類這段代碼複製到:

import android.view.GestureDetector; 
import android.view.GestureDetector.SimpleOnGestureListener; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 

public class OnSwipeTouchListener implements OnTouchListener { 

    private final GestureDetector gestureDetector = new GestureDetector(new GestureListener()); 

    public boolean onTouch(final View view, final MotionEvent motionEvent) { 
     return gestureDetector.onTouchEvent(motionEvent); 
    } 

    private final class GestureListener extends SimpleOnGestureListener { 

     private static final int SWIPE_THRESHOLD = 100; 
     private static final int SWIPE_VELOCITY_THRESHOLD = 100; 

     @Override 
     public boolean onDown(MotionEvent e) { 
      return true; 
     } 

     @Override 
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
      boolean result = false; 
      try { 
       float diffY = e2.getY() - e1.getY(); 
       float diffX = e2.getX() - e1.getX(); 
       if (Math.abs(diffX) > Math.abs(diffY)) { 
        if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) { 
         if (diffX > 0) { 
          onSwipeRight(); 
         } else { 
          onSwipeLeft(); 
         } 
        } 
       } else { 
        if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) { 
         if (diffY > 0) { 
          onSwipeBottom(); 
         } else { 
          onSwipeTop(); 
         } 
        } 
       } 
      } catch (Exception exception) { 
       exception.printStackTrace(); 


     } 
     return result; 
    } 
} 

public void onSwipeRight() { 
} 

public void onSwipeLeft() { 
} 

public void onSwipeTop() { 
} 

public void onSwipeBottom() { 
} 

}

和用法是這樣的:

RelativeLayout n = (RelativeLayout) findViewById(R.id.RelativeLayout1); 
     n.setOnTouchListener(new OnSwipeTouchListener() { 
      public void onSwipeRight() { 
      } 
      public void onSwipeLeft() { 
      } 
      public void onSwipeTop() { 
      } 

      public void onSwipeBottom() { 
      } 
     }); 
+0

很酷,謝謝,我會試試你已經展示過的'OnSwipeTouchListener'類。所以我只是創建了監聽器類並設置我的主類來實現'OnSwipeTouchListener'?然後將左,右手勢設置爲與活動對應的意圖。你也確定這會工作,然後我去黑客的代碼。 –

+0

@Brian J是的!它的測試!(不需要修改代碼)你不需要實現OnSwipeTouchListener你只需要使用setOnTouchListener而不是新的OnTouchListener你必須編寫新的OnSwipeTouchListener():)完成:)如果我的文章幫助不忘記接受! :) – Mahfa