我在我的布局中开发了一个带有viewpager和pagertabstrip的应用程序。我的问题是,当我在pagertabstrip上向左或向右滑动时,它显示我不想要的左页或右页。我究竟想要的是,在pagertabstrip上滑动时,只有pagertabstrip中的标题应向左或向右移动。选择标题时,它应显示特定页面。希望我的问题清楚。这是我的代码
public class ViewPagerAdapter extends FragmentPagerAdapter {
// Declare the number of ViewPager pages
final int PAGE_COUNT = 10;
private String titles[] = new String[] { "English", "Hindi","Assamese","Marathi","Telugu","Gujarati","Bengali","Kannada","Malayalam","Tamil"};
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
// Open FragmentTab1.java
case 0:
//View view=LayoutInflater.from(ViewPagerAdapter.this)
FragmentTab1 fragmenttab1 = new FragmentTab1();
return fragmenttab1;
//View view=LayoutInflater.from(R.layout.web);
// Open FragmentTab2.java
case 1:
FragmentTab2 fragmenttab2 = new FragmentTab2();
return fragmenttab2;
case 2:
FragmentTab3 fragmenttab3=new FragmentTab3();
return fragmenttab3;
case 3:
FragmentTab4 fragmenttab4=new FragmentTab4();
return fragmenttab4;
case 4:
FragmentTab5 fragmenttab5=new FragmentTab5();
return fragmenttab5;
case 5:
FragmentTab6 fragmenttab6=new FragmentTab6();
return fragmenttab6;
case 6:
FragmentTab7 fragmenttab7=new FragmentTab7();
return fragmenttab7;
case 7:
FragmentTab8 fragmenttab8=new FragmentTab8();
return fragmenttab8;
case 8:
FragmentTab9 fragmenttab9=new FragmentTab9();
return fragmenttab9;
case 9:
FragmentTab10 fragment10=new FragmentTab10();
return fragment10;
}
return null;
}
public CharSequence getPageTitle(int position) {
return titles[position];
}
@Override
public int getCount() {
return PAGE_COUNT;
}
}
public class WebFragment extends SherlockFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view=inflater.inflate(R.layout.web,container,false);
ViewPager mviewpager=(ViewPager) view.findViewById(R.id.viewPager);
PagerTabStrip tabstrip=(PagerTabStrip) view.findViewById(R.id.pagerTabStrip);
mviewpager.setAdapter(new ViewPagerAdapter(getChildFragmentManager()));
final int pageMargin=(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources().getDisplayMetrics());
mviewpager.setPageMargin(pageMargin);
tabstrip.setGravity(Gravity.LEFT);
return view;
}
@Override
public void onDetach() {
super.onDetach();
try {
Field childFragmentManager = Fragment.class
.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, null);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
答案 0 :(得分:0)
最简单的方法是setOnTouchListener并为ViewPager返回true。
mviewpager.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
return true;
}
});
另一个解决方案是你需要继承你的viewpager
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
public class CustomViewPager extends ViewPager {
public CustomViewPager (Context context) {
super(context);
}
public CustomViewPager (Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
// Never allow swiping to switch between pages
return false;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// Never allow swiping to switch between pages
return false;
}
}
和你的xml
<youpackage.CustomViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>