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"/>
Related
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();
As i have Fragment there is ViewPager in which i am displaying three another fragment, now the onCreateView method is called from all three fragment.
Here is my code.
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
<com.customView.CustomViewPager
android:id="#+id/newOrderContainerPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/layout_next_previous"
android:layout_below="#+id/newTitle"
android:layout_marginBottom="#dimen/ten_dp">
</com.customView.CustomViewPager>
Here is adapter
public class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {`enter code here`
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
return super.instantiateItem(container, position);
}
}
Main Fragment which display ViewPager
#Override
public View setContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_new_order, container, false);
setupViewPager(customViewPager);
customViewPager.setPagingEnabled(false);
return view;
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());
adapter.addFrag(new NewOrderFragmentFirstPage(), NewOrderFragmentFirstPage.class.getName());
adapter.addFrag(new NewOrderFragmentSecondPage(), NewOrderFragmentFirstPage.class.getName());
adapter.addFrag(new NewOrderFragmentThirdPage(), NewOrderFragmentThirdPage.class.getName());
viewPager.setAdapter(adapter);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.txt_Next:
txt_Previous.setVisibility(View.VISIBLE);
if (count < 3) {
count++;
}
customViewPager.setCurrentItem(customViewPager.getCurrentItem() + 1);
textView.setText(count + "/3");
break;
case R.id.txt_previous:
if (count > 1) {
count--;
}
if (count == 1) {
txt_Previous.setVisibility(View.INVISIBLE);
}
customViewPager.setCurrentItem(customViewPager.getCurrentItem() - 1);
textView.setText(count + "/3");
break;
}
}
Please help.
Thanks
You can add the code that you want to execute only single time in onActivityCreated() this will prevent its call from all the 3 fragments
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);
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());
I developed an android application, as you see in this picture,
I used sherlock action bar for searching, the only problem is that when the user search something in web tab if he/she wants to see the image result, he should type and search again in image tab, is there any way to pass the edittext parameter to other tabs and do searching when user changed the tabs?
MainActivity:
public class MainActivity extends SherlockFragmentActivity implements
TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private com.actionbarsherlock.app.ActionBar actionBar;
private String[] tabs = { "Sound", "Image", "Web" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getSupportActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
actionBar.setHomeButtonEnabled(false);
viewPager.setAdapter(mAdapter);
viewPager.setOffscreenPageLimit(3);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.addTab(actionBar.newTab().setText(tabs[0])
.setTabListener(this), false);
actionBar.addTab(actionBar.newTab().setText(tabs[1])
.setTabListener(this), false);
actionBar.addTab(actionBar.newTab().setText(tabs[2])
.setTabListener(this), true);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabSelected(com.actionbarsherlock.app.ActionBar.Tab tab,
android.support.v4.app.FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(com.actionbarsherlock.app.ActionBar.Tab tab,
android.support.v4.app.FragmentTransaction ft) {
}
#Override
public void onTabReselected(com.actionbarsherlock.app.ActionBar.Tab tab,
android.support.v4.app.FragmentTransaction ft) {
}
}
Adapter:
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new Sound();
case 1:
return new Image();
case 2:
return new Search();
}
return null;
}
#Override
public int getCount() {
return 3;
}
}
Sound Frgment:
public class Sound extends Fragment {
private String URL_ADDRESS, Servlet;
private ListView listview;
private ListViewAdapter adapter;
private List<Spanned> numberlist = new ArrayList<Spanned>();
private List<Ava2> avaList = new ArrayList<Ava2>();
private Boolean isInternetPresent = false;
private ConnectionDetector detector;
private View rootView;
private RelativeLayout loading, layout;
private int counter = 10;
private EditText editText;
private String a = "http://***";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.ava, container, false);
ImageButton voiceSearch = (ImageButton) rootView
.findViewById(R.id.imageButton2);
ImageButton buttonSearch = (ImageButton) rootView
.findViewById(R.id.imageButton1);
listview = (ListView) rootView.findViewById(R.id.listview);
loading = (RelativeLayout) rootView.findViewById(R.id.loading);
layout = (RelativeLayout) rootView.findViewById(R.id.layout);
editText = (EditText) rootView.findViewById(R.id.editText1);
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
search();
return true;
}
return false;
}
});
buttonSearch.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
search();
}
});
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long l) {
try {
Intent i = new Intent(getActivity(), Play.class);
startActivity(i);
} catch (Exception e) {
}
}
}
});
return rootView;
}
First,you could write a interface:
public interface OnProfileDataPass {
public String getCurrentSearchword();
public void setCurrentSearchword(String searchword);
}
Then in your fragments:
public class WebFragment extends Fragment {
private OnProfileDataPass dataPasser;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
dataPasser = (OnProfileDataPass) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnProfileDataPass");
}
searchword = dataPasser.getCurrentSearchword();
}
}
In your activity:
public class MainActivity implements OnProfileDataPass {
String currentSearchword;
#Override
public String getCurrentSearchword() {
return currentSearchword;
}
#Override
public String setCurrentSearchword(String searchword) {
this.currentSearchword = searchword;
}
}
That's my solution.When you change the tap,you could listen the proper event and set the word with getActivity.setCurrentSearchword(String newword),then you could get the newword in the fragment's dataPasser variable's method getCurrentSearchword() to get the search word. I have test it in my app. Wish that helps you a little.
you should save the value of the editText in your activity and then pass it as an argument when you call your fragment, using the same technique as the one described here: http://developer.android.com/reference/android/app/Fragment.html