I want lock / disable one particular page(fragment) in viewpager. According to scenario for some people only able to access that page. How restrict swiping to that page.
I disabled tab click for Events page by using following code :
LinearLayout tabStrip = ((LinearLayout)tabLayout.getChildAt(0));
if(!isAccess){
tabStrip.getChildAt(3).setClickable(false);
tabStrip.getChildAt(3).setEnabled(false);
}
But now I need to restrict swiping for that page in view pager. How to do this?
I written this in pager adapter it works.
#Override
public int getCount() {
if(!isAccess) {
return 3;
} else {
return 4;
}
}
Use this
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
public void onPageScrollStateChanged(int state) {}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
public void onPageSelected(int position) {
if (position == 3)
{
// show dialog that you are not allowed
viewPager.setSelected(2);
}
}
});
Hope this helped you
You can also use
viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
// optional
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { }
// optional
#Override
public void onPageSelected(int position) { }
// optional
#Override
public void onPageScrollStateChanged(int state) { }
});
This is also the solution without using the deprecated method (setOnPageChangeListener).
viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
super.onPageScrolled(position, positionOffset, positionOffsetPixels);
}
#Override
public void onPageSelected(int position) {
super.onPageSelected(position);
if(position == 3) {
//To disable specific tab and set the previuos tab
viewPager.setCurrentItem(2); //We cannot provide the position in setSelected(boolean) now. This was missed in above solution
}
}
#Override
public void onPageScrollStateChanged(int state) {
super.onPageScrollStateChanged(state);
}
});
Initialize the first tab position
int currentTab = 0;
then make listener for viewPager
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
//4 is the position of the tab we want to disable
if (position == 4) {
if (currentTab < 4) {
// swipe from left to right
viewPager.setCurrentItem(5);
currentTab = 5;
} else {
// swipe from right to left
viewPager.setCurrentItem(3);
currentTab = 3;
}
} else {
currentTab = position;
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
Related
I want to set contentDescription for tabs of tabLayout but talkBack doesn't read the content description of the tab that is selected and it just say 'selected'. How do I have to set contentDescription for tabLayout?
I update content description in viewPager.addOnPageChangeListener for each tabs and also I tried to set content description in tabLayout.addOnTabSelectedListener for each tabs but none of them worked!
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
updateTabContentDescription();
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
...
private void updateTabContentDescription() {
for (int i = 0; i < adapter.getCount(); i++) {
TabLayout.Tab tab = tabLayout.getTabAt(i);
if (tab != null) {
tab.setContentDescription(adapter.getPageTitle(i));
}
}
}
and I test it for viewPager:
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
updateTabContentDescription();
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
Im searching in all internet but I dont find any REAL solution about my problem.
I have a class with a Viewpager. (Class DescripcionRutinas)
private void setupViewPager(ViewPager viewPager) {
adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addNumDias(numDias);
adapter.setDias(dias);
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(5);
}
And I have a ViewPagerAdapter
class ViewPagerAdapter extends FragmentPagerAdapter {
private int numDias;
private int count = 0;
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
ArrayList<Dia> dias;
#Override
public Fragment getItem(int position) {
OneFragmentSeries fragmento = new OneFragmentSeries();
Dia ex = dias.get(**I NEED CURRENT ITEM HERE**);
nomDia = ex.getNombre();
fragmento.setSeries((ArrayList<Serie>)((ArrayList<Dia>)WorkoutYourself.dameWorkoutYourself().dameRutina(nomRutina).getDias()).get(position).getSeries());
return fragmento;
}
I need the current item position here.
NOTE: ViewPagerAdapter is INSIDE DescripcionRutinas.
Thanks guys!
Try to use yourViewPager.getCurrentItem() it should work to get your visible pager position.
Update
onPageSelected(int currentPage) should do the stuff
dia.setOnPageChangeListener(new OnPageChangeListener() {
public void onPageScrollStateChanged(int arg0) {
}
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
public void onPageSelected(int currentPage) {
//here you get your current position
}
});
Read this documentation
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int positionCurrentPage) {
if(positionCurrentPage == 1){ // if you want the second page, for example
//Your code here
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
I have a viewPager of four different tabs, i'm using addOnPageChangeListener to manage page scrolling, the following code is trying to get a view from the first fragment when clicking on a button on the fourth fragment, this works fine when i click on tabs and break when i scroll the pages, which i couldn't find a reason why, the exception i get is a java.lang.NullPointerException for the EditTest view error
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float
positionOffset, int positionOffsetPixels) {
if(position == 3){
EditText title = (EditText) findViewById(R.id.title);
Log.d("Hello", title.getText().toString());
}
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
link to gif showing the problem here
i have tried to move this code to onPageSelected and it didn't work, i also tried to get the fragment view using the following code and no luck:
public View getFragmentView(int position) {
String fragmentD = "android:switcher:" + viewPager.getId() + ":" + position;
android.support.v4.app.Fragment fragment =
getSupportFragmentManager().findFragmentByTag(fragmentD);
return fragment.getView();
}
question is why this works with tab clicking and doesn't with scrolling?
You must call it in OnCreate
public class MainActivity extends Activity {
EditText title;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
title = (EditText) findViewById(R.id.title);
}
}
And at public void onPageSelected(int position)
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float
positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if(position == 3){
Log.d("Hello", title.getText().toString());
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
I am using a viewPager for its swipe abilities to swipe between two Fragments. However I want to refresh the Fragment contents on swipe. The FragmentStatePagerAdapter does a very good job of caching so its hard to get the Fragment to reload. I've tried putting everything in the onResume method but that has no effect.
Here is my code
slideadapter=new ScreenSlidePagerAdapter(this.getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.viewpager);
mViewPager.setOffscreenPageLimit(1);
mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int i, float v, int i2) {
//Not used
Log.v("TAG","onPageScrolled");
}
#Override
public void onPageSelected(int position) {
//Force the fragment to reload its data
Fragment f = slideadapter.getItem(position);
f.onResume();
}
#Override
public void onPageScrollStateChanged(int i) {
//Not used
Log.v("TAG","onPageScrollStateChanged");
}
});
mSlidingTabLayout = (SlidingTabLayout)findViewById(R.id.sliding_tabs);
mViewPager.setAdapter(slideadapter);
mViewPager.setCurrentItem(0);
mSlidingTabLayout.setViewPager(mViewPager);
you can override setUserVisibleHint method in your Fragments
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
doSomething();
}
}
Use the ViewPager.onPageChangeListener:
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
public void onPageScrollStateChanged(int state) {}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
public void onPageSelected(int position) {
// get fragment by position and do whatever you want to do on fragment selection.
}
});
you can try mViewPager.setOffscreenPageLimit(1); if you want to call the onresume method everytime the fragments becomes visible
Create a BaseFragment class with a method ex. loadData (),where
Viewpager child fragments extends from this BaseFragment and override the loadData () method and do the operation.
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
public void onPageScrollStateChanged(int state) {}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
public void onPageSelected(int position) {
if (slideadapter.getFragment(position) instanceof BaseFragment) {
((BaseFragment) slideadapter.getFragment (position)).loadData ();
}
}
});
In the SlideAdapter create method called
public Fragment getFragment(int key) {
return mPageReferences.get(key);
}
private final SparseArray<Fragment> mPageReferences = new SparseArray<Fragment>();
//add created fragment refernce to this array
#Override
public Fragment getItem(int position) {
mPageReferences.put(position, fragment);
}
//remove the reference when destroy
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
mPageReferences.remove(position);
}
Hope this will help!
I have extended the ViewPager class and implemented onPageChangeListener.
I use this ViewPager in conjunction with a CirclePageIndicator inside a SherlockFragment. The ViewPager contains ImageViews and TextViews, nothing fancy.
I need to do something below the ViewPager every time the Page is being changed. So, I figured I need to use the onPageScrollStateChanged and verify when the state is IDLE, or something.
The problem is that from the 3 implemented methods (onPageScrollStateChanged, onPageSelected and onPageScrolled), only onPageScrolled gets called (I checked with a log message in each method). Why is that ? Code below.
public class UninterceptableViewPager extends ViewPager implements OnPageChangeListener{
private ArrayList<TwoRowListViewAdapter> mAdaptersList = null;
private ListView mListView = null;
private ProgressBar mProgressBar = null;
public UninterceptableViewPager(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public boolean onInterceptTouchEvent(MotionEvent event)
{
getParent().requestDisallowInterceptTouchEvent(true);
return super.onInterceptTouchEvent(event);
}
public void setAdaptersList(ArrayList<TwoRowListViewAdapter> adaptersList)
{
mAdaptersList = adaptersList;
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
{
super.onPageScrolled(position++, positionOffset, positionOffsetPixels);
if (positionOffset == 0) // FIXME
loadHistoryPage(position);
}
#Override
public void onPageSelected(int position) {}
#Override
public void onPageScrollStateChanged(int state) {}
// -------------------------------------------------------------------------------------------
// -------------- Helper Methods
// -------------------------------------------------------------------------------------------
public void setListView(ListView listview)
{
mListView = listview;
}
public void setProgressBar(ProgressBar progressBar)
{
mProgressBar = progressBar;
}
private void loadHistoryPage(int position)
{
mListView.setVisibility(View.INVISIBLE);
mProgressBar.setVisibility(View.VISIBLE);
mListView.setAdapter(null);
mListView.setAdapter(mAdaptersList.get(position-1));
mProgressBar.setVisibility(View.INVISIBLE);
mListView.setVisibility(View.VISIBLE);
}
}
When you use PageIndicator in conjunction with the Viewpager then the onPageChangeListener of the ViewPager is not called. I don't know the reason for this but this happened when I was using both. May be cause the page indicator consumes the page change event. Instead you should set a page change listener to the PageIndicator and it will be called when the page changes. Here is how you can do this:
indicator.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected (int page)
{
//page changed
}
#Override
public void onPageScrolled (int arg0, float arg1, int arg2)
{
}
#Override
public void onPageScrollStateChanged (int arg0)
{
}
});
I hope this will help. Feel free to discuss.
setOnPageChangeListener is deprecated.
use addOnPageChangeListener
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});