2013-11-04 181 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