can't disable viewpager swipe programmatically - android

I have a Fragment inside my app which contains a ViewPager. I have a String variable code. If code equals "NBG" I want to disable swipe of the ViewPager.
public class CurrencyFragment extends BaseFragment {
#Override
public View onCreateView(final LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_currency_layout, container, false);
mViewpager = (ViewPager) view.findViewById(R.id.Detailpager);
Bundle bundle = getArguments();
if (bundle != null) {
code = object.getCode();
}
if (code.equals("NBG")) {
CustomViewPager pager = new CustomViewPager(getActivity(), null);
pager.setPagingEnabled(false);
}
return view;
};
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;
}
}

You're using two different ViewPagers. The ViewPager that's initialized using View.findViewById isn't the same as the CustomViewPager being created when code.equals(NBG).
In fragment_currency_layout instead of using android.support.v4.view.ViewPager, use path_to_CustomViewPager. Then call mViewpager.setPagingEnabled instead of pager.setPagingEnabled.

mViewpager = (ViewPager) view.findViewById(R.id.Detailpager);
In your XML layout, where you declare your ViewPager, you should declare your CustomViewPager instead. CustomViewPager will need to be in its own file.
Currently, you are instantiating the regular ViewPager (I assume, as you haven't given the XML code). Then you do:
CustomViewPager pager = new CustomViewPager(getActivity(), null);
pager.setPagingEnabled(false);
But this is an extra ViewPager you've instantiated which isn't related to any UI in your screen.

Related

Is there a way to stop a fragment 1 going to fragment 2 when I swipe?

is there a way to stop fragment 1 going to fragment 2 when I swipe right/left
I noticed recently that if I swipe left my fragment changes from 1 to 2.
I searched on web but I couldn't find an answer
Edit:
My code for adapter
public ViewPager viewPager;
private ViewPagerAdapter adapter
adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.AddFragment(new Fragment_Sign_In_To_Firebase(), "Firebase Sign In");
adapter.AddFragment(new Fragment_Create_Account(), "Create Account");
adapter.AddFragment(new Fragment_Verify_Email(), "Verify Email");
viewPager = (ViewPager) findViewById(R.id.viewpager_id_chat);
viewPager.setAdapter(adapter);
viewPager.arrowScroll(View.FOCUS_RIGHT);
ViewPaggerAdapter class:
public class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> lstFragment = new ArrayList<>();
private final List<String> lstTitles = new ArrayList<>();
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return lstFragment.get(position);
}
#Override
public int getCount() {
return lstTitles.size();
}
#Override
public CharSequence getPageTitle(int position) {
return lstTitles.get(position);
}
public void AddFragment(Fragment fragment, String title) {
lstFragment.add(fragment);
lstTitles.add(title);
}
}
As I can see your question is not having a proper query on what you are searching for exactly ,I think you are talking about ViewPager
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;
} }
Use this in your XML
<mypackage.CustomViewPager
android:id="#+id/myViewPager"
android:layout_height="match_parent"
android:layout_width="match_parent" />
OR SIMPLE ADD THIS LINE
myViewPager.beginFakeDrag();

Android: how to disable swiping between tablayout control by button click

i have 2 fragment in my tab layout, i manage to disable the swiping function between tab layout by button click. How to implement the disable function in my onclick button. thanks for advance
MainActivity
public class MainActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener{
private TabLayout tabLayout;
private ViewPager viewPager;
int ans =2;
------FCViewPager disable;------
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
BasePagerAdapter adapter = new BasePagerAdapter(getSupportFragmentManager());
Tab1 tab1 = new Tab1();
Tab2 tab2 = new Tab2();
adapter.addFragment(tab1, "1");
adapter.addFragment(tab2, "2");
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(adapter);
tabLayout.setOnTabSelectedListener(this);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setupWithViewPager(viewPager);
}
----- public void testing( View view) {
disable.setEnableSwipe(false);
}---------
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
public class BasePagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public BasePagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
public void removeFragment(int position) {
mFragmentTitleList.remove(position);
mFragmentList.remove(position);
notifyDataSetChanged();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
Tab1
public class Tab1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View h = inflater.inflate(R.layout.tab1, container, false);
return h;
}
}
Tab2
public class Tab2 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View h = inflater.inflate(R.layout.tab2, container, false);
return h;
}
You can disable swipe feature in your ViewPager. Create custom ViewPager as follow.
public class FCViewPager extends ViewPager {
private boolean enableSwipe;
public FCViewPager(Context context) {
super(context);
init();
}
public FCViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
enableSwipe = true;
}
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
// Never allow swiping to switch between pages
return enableSwipe && super.onInterceptTouchEvent(event);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
// Never allow swiping to switch between pages
return enableSwipe && super.onTouchEvent(event);
}
public void setEnableSwipe(boolean enableSwipe) {
this.enableSwipe = enableSwipe;
}
}
In your MainActivity
//R.id.pager has to be FCViewPager not default ViewPager
viewPager = (FCViewPager) findViewById(R.id.pager);
Your button onClick function
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
pager.setEnableSwipe(false);
}
});
User scroll disable viewpager for disabling and enabling scrolling by default and programmatically
public class ScrollDisabledViewpager extends ViewPager {
private boolean isPagingEnabled = false;
public ScrollDisabledViewpager(Context context) {
super(context);
}
public ScrollDisabledViewpager(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
return this.isPagingEnabled && super.onTouchEvent(event);
}
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
return this.isPagingEnabled && super.onInterceptTouchEvent(event);
}
public void setPagingEnabled(boolean b) {
this.isPagingEnabled = b;
}
}
Button onClick event you can set this true/false to enable/ disable scrolling
viewPager.setPagingEnabled(true/ false);

Swipe ViewPager still lag inner NonSwipeableViewPager Android

I have ViewPager NonSwipeable(cannot swipe) with 2 fragment. In second fragment, I set normal ViewPager(can swipe) with Tab Indicator. But when I swipe that ViewPager , its look lag.
Parent Activity
public NonSwipeableViewPager mPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);y;
setContentView(R.layout.activity_search);
....
mAdapter = new FragmentSearchAdapter(getSupportFragmentManager(), SearchActivity.this);
mAdapter.addFragment("search histori");
mAdapter.addFragment("search result");
mPager.setAdapter(mAdapter);
mPager.setOffscreenPageLimit(2);
....
}
this is Custom ViewPager NonSwipeable :
public class NonSwipeableViewPager extends ViewPager {
public NonSwipeableViewPager(Context context) {
super(context);
}
public NonSwipeableViewPager(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;
}
}
Second Fragment
#Bind(R.id.pager)
public ViewPager mPager;
#Bind(R.id.tabs)
PagerSlidingTabStrip mPagerSlidingTabStrip;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
View rootView = inflater.inflate(R.layout.fragment_search_result2, container, false);
mAdapter = new FragmentSearchResultAdapter(getActivity().getSupportFragmentManager(), getActivity());
mAdapter.addFragment("Business");
mAdapter.addFragment("Product");
mPager.setAdapter(mAdapter);
mPager.setOffscreenPageLimit(2);
mPagerSlidingTabStrip.setViewPager(mPager);
return rootView;
}
so how to solve it ? sorry for my english
My Problem is :
mAdapter = new FragmentSearchResultAdapter(getActivity().getSupportFragmentManager(), getActivity());
I Change with :
mAdapter = new FragmentSearchResultAdapter(getChildFragmentManager(), getActivity());

Disabling swiping on the Android ViewPager

I am developing an android application in which i used android view pager for swipe between 4 fragments.swiping between the fragments performing well.what i need is i want to disable the swipe until operations on the first fragment completed.if all the necessary operations performed at the particular fragment swiping has to enable from that fragment and so on until the last fragment.what should i do for this.can any one have the suggetions?.please remember that i need to control the swiping from all fragments.please help me out.thanks in advance..
public class MyPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public MyPagerAdapter(FragmentManager fm) {
super(fm);
this.fragments = new ArrayList<Fragment>();
fragments.add(new SalesTransactionFragment());
fragments.add(new FragmentCollection());
// fragments.add(new FragmentBlue());
fragments.add(new FragmentGreen());
fragments.add(new FragmentPink());
}
#Override
public Fragment getItem(int position) {
return fragments.get(position);
}
#Override
public int getCount() {
return fragments.size();
}
}
MainActivity
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyPagerAdapter pageAdapter = new MyPagerAdapter(getSupportFragmentManager());
ViewPager pager = (ViewPager)findViewById(R.id.myViewPager);
pager.setOffscreenPageLimit(4);
pager.setAdapter(pageAdapter);
}
One of the Fragment
public class FragmentPink extends Fragment {
Button b;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_pink, container, false);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
b=(Button) getActivity().findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getActivity(),MainActivity.class);
startActivity(i);
}
});
}
}
One approach would be to set OnTouchListener to the ViewPager like this -
mPager.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(mPaginEnabled)
return false;
else
return true;
}
});
You need to subclass ViewPager
public class MyViewPager extends ViewPager {
private boolean mSwipable = true;
public MyViewPager(Context context) {
super(context);
}
public MyViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
return mSwipable ? super.onInterceptTouchEvent(event) : false;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
return mSwipable ? super.onTouchEvent(event) : false;
}
public boolean isSwipable() {
return mSwipable;
}
public void setSwipable(boolean swipable) {
mSwipable = swipable;
}
}
Now you can enable/disable swipes by calling setSwipable method.
UPD: To edit this property in XML the class will be:
public class MyViewPager extends ViewPager {
private boolean mSwipable = true;
public MyViewPager(Context context) {
super(context);
}
public MyViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyViewPager);
try {
mSwipable = a.getBoolean(R.styleable.MyViewPager_swipeable, true);
} finally {
a.recycle();
}
}
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
return mSwipable ? super.onInterceptTouchEvent(event) : false;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
return mSwipable ? super.onTouchEvent(event) : false;
}
public boolean isSwipable() {
return mSwipable;
}
public void setSwipable(boolean swipable) {
mSwipable = swipable;
}
}
And your values/attr.xml:
<declare-styleable name="MyViewPager">
<attr name="swipeable" format="boolean" />
</declare-styleable>
Now you can use it in your layout:
<my.package.name.MyViewPager
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/viewPager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:swipeable="false"/>

Android ViewPager inside Layoutinflator

I build a project. but I'm having problems.
My project has page A , Page B
from Picture http://s20.postimg.org/5pupjbuyl/untitled.png
Page A is FrameLayout (id=layout) , in FrameLayout has LinearLayout and FrameLayout (id=content)
Page B is FrameLayout (id=pager) , in FrameLayout has Viewpager
I would take Framelayout (id=content) in page A containing FrameLayout(id = pager) in page B
(Notice : I try to minimize my code because It many line)
PageIndicatorActivity .java
public class PageIndicatorActivity extends Activity {
...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
..
..
ViewGroup Content = (ViewGroup)findViewById(R.id.layout);
ViewBookAdapter pager = new ViewBookAdapter(this);
Content.addView(pager.getView());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main,menu);
return true;
}}
pager.java
public class Pager extends PagerAdapter {
Activity activity;
private static String content[] = {"file:///android_asset/html/page1.html","file:///android_asset/html/page2.html"};
public Pager(Activity act) {
activity = act;
}
public int getCount() {
return content.length;
}
public Object instantiateItem(View collection, int position) {
WebView view = new WebView(activity);
view.loadUrl(content[position]);
view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
((ViewPager) collection).addView(view, 0);
return view;
}
#Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
#Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
#Override
public Parcelable saveState() {
return null;
}}
and ViewBookAdapter.java
class ViewBookAdapter {
Activity activity;
private LayoutInflater minflate;
private ViewPager myPager;
public ViewBookAdapter(Activity act){
activity = act;
myPager = (ViewPager)act.findViewById(R.id.viewPager);
Pager p = new Pager(act);
myPager.setAdapter(p); // <--- THIS PROBLEM ---
}
public View getView(){
LayoutInflater minflate = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = minflate.inflate(R.layout.pager, null);
return v;
}}
I have a probleam at myPager.setAdapter(p);
Android Report Bug is NullPointerException
How to solve?
THANK YOU FOR ANSWER ^^
PS. sorry english .
This is happening because your Activity does not have the ViewPager in its layout. Its in the other layout. You will need to do setAdapter in getView()
Your current PageIndicatorActivity layout does not have a ViewPager. If you need to continue this way you have to move to Activity(Page B) and follow your implementation.

Categories

Resources