先看一下我做的demo:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:id="@+id/view_pager_box" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="360dp" android:layout_marginTop="30dp" android:clipChildren="false" > <android.support.v4.view.ViewPager android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="50dp" android:layout_marginRight="50dp" android:clipChildren="false" /> </RelativeLayout>
public class MainActivity extends AppCompatActivity { ViewPager mViewPager; RelativeLayout mViewPagerBox; private ViewPagerAdapter1 mViewPagerAdapter1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mViewPager = (ViewPager) findViewById(R.id.view_pager); mViewPagerBox = (RelativeLayout) findViewById(R.id.view_pager_box); mViewPager.setOffscreenPageLimit(3); mViewPagerAdapter1 = new ViewPagerAdapter1(this); mViewPager.setAdapter(mViewPagerAdapter1); mViewPagerBox.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return mViewPager.dispatchTouchEvent(event); } }); } }