如何使用 ViewPager 实现视图更改条件
Posted
技术标签:
【中文标题】如何使用 ViewPager 实现视图更改条件【英文标题】:How to implement condition on view change using ViewPager 【发布时间】:2014-08-30 05:07:37 【问题描述】:在我的程序中,我使用Fragments with ViewPager
,其中每个Fragment
都包含3 checkboxes
,如果用户没有checked
任何checkbox
,我想put a condition
不要切换到other fragment
当swipe
.
我知道condition
可能是什么,但我不知道where to put
?
这个condition
对我来说是work
,但是where to use
不知道:
if(!cb1.isChecked() && !cb2.isChecked() && !cb3.isChecked())
Toast.makeText(getActivity(), "Choose any one !", Toast.LENGTH_SHORT).show();
return view;
MainActivity.java:-
public class MainActivity extends FragmentActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
// Get the view from activity_main.xml
setContentView(R.layout.activity_main);
// Locate the viewpager in activity_main.xml
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
// Set the ViewPagerAdapter into ViewPager
viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
ViewPagerAdapter.java:-
public class ViewPagerAdapter extends FragmentPagerAdapter
final int PAGE_COUNT = 2;
// Tab Titles
private String tabtitles[] = new String[]
"Fragment 1",
"Fragment 2"
;
Context context;
public ViewPagerAdapter(FragmentManager fm)
super(fm);
@Override
public int getCount()
return PAGE_COUNT;
@Override
public Fragment getItem(int position)
switch (position)
// Open FragmentTab1.java
case 0:
FragmentTab1 fragmenttab1 = new FragmentTab1();
return fragmenttab1;
// Open FragmentTab2.java
case 1:
FragmentTab2 fragmenttab2 = new FragmentTab2();
return fragmenttab2;
return null;
@Override
public CharSequence getPageTitle(int position)
return tabtitles[position];
FragmentTab1.java:-
public class FragmentTab1 extends Fragment
View view;
CheckBox cb1, cb2, cb3;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
// Get the view from fragmenttab1.xml
view = inflater.inflate(R.layout.fragmenttab1, container, false);
cb1 = (CheckBox) view.findViewById(R.id.cb1);
cb2 = (CheckBox) view.findViewById(R.id.cb2);
cb3 = (CheckBox) view.findViewById(R.id.cb3);
// control status of body frame checkboxes
cb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
// TODO Auto-generated method stub
if(isChecked)
cb2.setChecked(false);
cb3.setChecked(false);
);
cb2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
// TODO Auto-generated method stub
if(isChecked)
cb1.setChecked(false);
cb3.setChecked(false);
);
cb3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
// TODO Auto-generated method stub
if(isChecked)
cb1.setChecked(false);
cb2.setChecked(false);
);
/**
** if(!cb1.isChecked() && !cb2.isChecked() && !cb3.isChecked())
Toast.makeText(getActivity(), "Choose any one !", Toast.LENGTH_SHORT).show();
return view;
***/
return view;
【问题讨论】:
不确定...试试OnPageChangeListener.. 【参考方案1】:更好的办法是禁用 viewpager 滑动,然后再次允许它,因此创建自定义一个并使用方法 setPagingEnabled(boolean enabled)
public class CustomViewPager extends ViewPager
private boolean enabled;
public CustomViewPager(Context context, AttributeSet attrs)
super(context, attrs);
this.enabled = true;
@Override
public boolean onTouchEvent(MotionEvent event)
if (this.enabled)
return super.onTouchEvent(event);
return false;
@Override
public boolean onInterceptTouchEvent(MotionEvent event)
if (this.enabled)
return super.onInterceptTouchEvent(event);
return false;
public void setPagingEnabled(boolean enabled)
this.enabled = enabled;
在你的xml中你需要:
<mypackage.CustomViewPager
android:id="@+id/myViewPager"
android:layout_
android:layout_ />
参考: (我相信版权:-))
How do disable paging by swiping with finger in ViewPager but still be able to swipe programmatically?
【讨论】:
以上是关于如何使用 ViewPager 实现视图更改条件的主要内容,如果未能解决你的问题,请参考以下文章
如何让viewpager取消预加载且能有tabpageindicator
如何在viewPager的视图顶部使用nestedScrollView中的viewpager