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());
Related
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 am facing the very weird problem,i found the similar kind of problem over the net but none of the solution helps me to get rid of this.
Problem ::
I am having a one fragment which have 3 tabs,(tabs created using fragment tab host).Now every tabs have different fragment.
From first tab,i have set one fragment where i have used view pager and circle page indicator to display the images with dots(circle).
Now when i swipe the image from 1st to 2nd then 3rd and so on,it works fine.
But after reaching on image 2nd,when i click on tab2 and again came back to tab1,my images lost.
Your guidance or any help/links will be appreciated..
Many many thanks in advance.
For your understanding here i am posting code below ::
FragmentwithTabHost ::
public class FragmentwithTabHost extends Fragment{
private View rootView;
private FragmentTabHost mTabHost;
private Context mContext;
public FragmentwithTabHost() {
}
public FragmentwithTabHost(Context ctx) {
mContext=getActivity();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.mycardswithtabs, container,
false);
mTabHost = (FragmentTabHost) rootView.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(), android.R.id.tabcontent);
mTabHost.setBackground(getActivity().getResources().getDrawable(R.drawable.normal));
mTabHost.addTab(
mTabHost.newTabSpec("tab1").setIndicator("tab1", null),
Fragment1.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab2").setIndicator("tab2", null),
Fragment2.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab3").setIndicator("tab3", null),
Fragment3.class, null);
return rootView;
}
}
Fragment1 ::
public class Fragment1 extends Fragment{
// private Context mContext;
private View rootView;
SlidesFragmentAdapter mAdapter;
ViewPager mPager;
PageIndicator mIndicator;
LinearLayout llSponseredImage,llSponserdByText;
ImageView imgsponser1,imgsponser2,imgsponser3,imgsponser4;
public Fragment1() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.my_card, container,false);
mAdapter = new SlidesFragmentAdapter(getActivity()
.getSupportFragmentManager());
llSponseredImage=(LinearLayout)rootView.findViewById(R.id.mycardbottomViewSponseredImage);
llSponserdByText=(LinearLayout)rootView.findViewById(R.id.mycardbottomsponsredby);
imgsponser1=(ImageView)rootView.findViewById(R.id.imgsponser1);
imgsponser2=(ImageView)rootView.findViewById(R.id.imgsponser2);
imgsponser3=(ImageView)rootView.findViewById(R.id.imgsponser3);
imgsponser4=(ImageView)rootView.findViewById(R.id.imgsponser4);
mPager = (ViewPager) rootView.findViewById(R.id.pager);
mPager.setOffscreenPageLimit(4);
mPager.setClipToPadding(false);
mPager.setPageMargin(12);
mPager.setAdapter(mAdapter);
mIndicator = (CirclePageIndicator) rootView.findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
((CirclePageIndicator) mIndicator).setSnap(true);
mPager.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("3242353652523");
MyCardsWithOffersFragment myCardsWithOffersFragment=new MyCardsWithOffersFragment();
}
});
mIndicator
.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
/*Toast.makeText(getActivity(),
"Changed to page " + position,
Toast.LENGTH_SHORT).show();*/
if(position==0 || position==2){
llSponseredImage.setVisibility(View.GONE);
llSponserdByText.setVisibility(View.GONE);
}else if(position==1){
llSponseredImage.setVisibility(View.VISIBLE);
llSponserdByText.setVisibility(View.VISIBLE);
imgsponser1.setImageResource(R.drawable.sponser1);
imgsponser2.setImageResource(R.drawable.sponser2);
imgsponser3.setImageResource(R.drawable.sponser3);
imgsponser4.setImageResource(R.drawable.sponser4);
}else if(position==3){
llSponseredImage.setVisibility(View.VISIBLE);
llSponserdByText.setVisibility(View.VISIBLE);
imgsponser1.setImageResource(R.drawable.sponser5);
imgsponser2.setImageResource(R.drawable.sponser6);
imgsponser3.setImageResource(R.drawable.sponser7);
imgsponser4.setImageResource(R.drawable.sponser8);
}
}
#Override
public void onPageScrolled(int position,
float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
return rootView;
}
public class SlidesFragmentAdapter extends FragmentStatePagerAdapter implements IconPagerAdapter {
private int[] Images = new int[] { R.drawable.first_image, R.drawable.second_image,
R.drawable.third_image, R.drawable.four_image
};
private int[] ICONS = new int[] { R.drawable.first_image, R.drawable.second_image,
R.drawable.third_image, R.drawable.four_image };
private int mCount = Images.length;
public SlidesFragmentAdapter(android.support.v4.app.FragmentManager fragmentManager) {
super(fragmentManager);
}
#Override
public Fragment getItem(int position) {
return new DemoFragment(Images[position]);
}
#Override
public int getCount() {
return mCount;
}
#Override
public int getIconResId(int index) {
return ICONS[index % ICONS.length];
}
public float GetPageWidth (int position) {
return 0.93f;
}
public void setCount(int count) {
if (count > 0 && count <= 10) {
mCount = count;
notifyDataSetChanged();
}
}
}
}
I think this can be achieved using show() or add() method. If it is a Activity without Tabhost then its easy. This case while tab switching we have to apply the same logic.
Check this solution
I guess this will give you some idea.
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"/>
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.
I have a problem. As said i have ViewPager with 5 Fragments inside. On the last fragment i have two relative layouts with some widgets inside. On button click i have animations that changes relative layouts to visible.
My problem is that when i scroll back the view that is gone becomes visible all by himself... Anybody experienced something similar???
public class WelcomeActivity extends FragmentActivity{
private FragmentAdapter adapter;
private ViewPager viewPager;
#Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.welcome_activity);
viewPager = (ViewPager) findViewById(R.id.pager);
adapter = new FragmentAdapter(getSupportFragmentManager(), viewPager);
viewPager.setAdapter(adapter);
}
public class FragmentAdapter extends FragmentPagerAdapter{
private ViewPager pager;
private Fragment mFragmentAtPos0;
private FragmentManager mFragmentManager;
public FragmentAdapter(FragmentManager fm, ViewPager p) {
super(fm);
this.pager = p;
this.mFragmentManager = fm;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new ScreenWelcome(pager, this);
case 1:
return new ScreenTutorial1();
case 2:
return new ScreenTutorial2();
case 3:
return new ScreenTutorial3();
case 4:
return new ScreenTutorial4();
case 5:
return new FlipAnimationFragment();
default:
return null;
}
}
#Override
public int getItemPosition(Object object) {
return super.getItemPosition(object);
}
#Override
public int getCount() {
return 6;
}
public class FlipAnimationFragment extends Fragment{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.flip_animation, container, false);
layout = (RelativeLayout) view.findViewById(R.id.layout);
sign_up = (RelativeLayout) view.findViewById(R.id.relativeLayoutSignUp);
log_in = (RelativeLayout) view.findViewById(R.id.relativeLayoutLogIn);
create_account_page = (Button) view.findViewById(R.id.buttonCreateAccount);
log_in_page = (Button) view.findViewById(R.id.buttonSignUpLogIn);
log_in_page.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!pressed){
pressed = true;
layout.startAnimation(flip);
}
}
});
create_account_page.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!pressed){
pressed = true;
layout.startAnimation(flip);
}
}
});
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
flip = new FlipAnimator(sign_up, log_in, 240, 400);
}