I create an android app using ViewPager
and Fragments
. In here I use FragmentTransaction
to sliding fragments among each other using a button action. Also I use a method to shifting among fragments . It is in MainActivity class.
This is MainActivity class,
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
public class MainActivity extends FragmentActivity {
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager=(ViewPager)findViewById(R.id.viewPager);
viewPager.setAdapter(new MyPagerAdapter2(getSupportFragmentManager()));
viewPager.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (viewPager.getCurrentItem() == 0) {
return true;
}
if (viewPager.getCurrentItem() == 1) {
return true;
}
if (viewPager.getCurrentItem() == 2) {
return true;
}
if (viewPager.getCurrentItem() == 3) {
return true;
}
if (viewPager.getCurrentItem() == 4) {
return true;
}
return false;
}
});
}
private class MyPagerAdapter2 extends FragmentPagerAdapter {
public MyPagerAdapter2(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int pos) {
switch(pos) {
case 0: return FirstFragment1.newInstance("FirstFragment_1");
case 1: return SecondFragment1.newInstance("SecondFragment_1");
case 2: return ThirdFragment1.newInstance("ThirdFragment_1");
case 3: return FourthFragment1.newInstance("FourthFragment_1");
case 4: return FifthFragment1.newInstance("FifthFragment_1");
default: return FirstFragment1.newInstance("DefaultFragment_1");
}
}
@Override
public int getCount() {
return 5;
}
}
public void setCurrentItem(int which) {
if(viewPager != null && which >= 0 && which <= 4) {
viewPager.setCurrentItem(which);
}
}
}
And here is an sample code for my purpose,
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
/**
* Created by user on 5/11/2016.
*/
public class SecondFragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.second_frag_1, container, false);
TextView tv = (TextView) v.findViewById(R.id.tvFragSecond);
tv.setText(getArguments().getString("msg"));
Button button=(Button)v.findViewById(R.id.printButton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(getActivity() != null) {
FragmentManager fragmentManager=getActivity().getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.setCustomAnimations(R.anim.from_right, R.anim.to_left);
FirstFragment1 firstFragment1=FirstFragment1.newInstance("Fragment Main");
transaction.replace(R.id.firstFragment, firstFragment1, "first_fragment");
transaction.commit();
( (MainActivity)getActivity()).setCurrentItem(0);
}
}
});
Button button1=(Button)v.findViewById(R.id.nextButton);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (getActivity() != null) {
( (MainActivity)getActivity()).setCurrentItem(2); // fourth fragment index is 3
}
}
});
return v;
}
public static SecondFragment1 newInstance(String text) {
SecondFragment1 f = new SecondFragment1();
Bundle b = new Bundle();
b.putString("msg", text);
f.setArguments(b);
return f;
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser) {
Activity a = getActivity();
if(a != null) a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
}
}
}
These are my animation files ,
from_left.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="-100%" android:toXDelta="0%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="500"/>
</set>
from_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="100%" android:toXDelta="0%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="500" />
</set>
to_left.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="0%" android:toXDelta="-100%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="500"/>
</set>
to_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="0%" android:toXDelta="100%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="500" />
</set>
Problem :- When use this animation files to shift among fragments is not working .
When I click on button in SecondFragment1 , 0 th fragment should load to the ViewPager
from right to left direction . But , the process is difference in here . First , 0th fragment load to the ViewPager
from left to right direction then again it load from right to left direction .
My purpose is load 0th fragment to the ViewPager
from right to left direction at the begining .
How could I get this in a correct view ? Have any ideas ?
Thanks in deep.
From what I understand by the question you just want to replace the fragment when a button is pressed? So to start with I would remove the view pager and replace it with a FrameLayout.
Declare framelayout at top of class:
FrameLayout fl_frag_container = (FrameLayout) findViewById(R.id.fl_frag_container);
Then create a method in the MainActivity you can call from the fragment to change the fragment being displayed:
In my case i use:
public void setSecondaryFrag(int page, String name, String url, String id) {
transaction = fragMan.beginTransaction();
switch (page) {
case Constants.VIEW_EVENTS:
transaction.replace(fl_frag_container.getId(), new ViewEventsFragment());
break;
case Constants.VIEW_ARTIST_PROFILE:
transaction.replace(fl_frag_container.getId(), BrandArtistProfileFragment.newInstance(Constants.ARTISTS, name, url, id));
break;
case Constants.VIEW_BRAND_PROFILE:
transaction.replace(fl_frag_container.getId(), BrandArtistProfileFragment.newInstance(Constants.BRANDS,name, url, id) );
break;
case Constants.VIEW_TICKETS:
transaction.replace(fl_frag_container.getId(), new ViewTicketFragment());
break;
}
transaction.addToBackStack(null);
transaction.setCustomAnimations(R.anim.from_right, R.anim.to_left);
transaction.commitAllowingStateLoss();
}
And then in the fragment i would call
((MainActivity) getActivity).setSecondaryFrag(page, "", "", "");
This can be modified to suit your needs
Using the frame layout to replace the fragments removes the need to use adapters for the viewpager
Hope this helps
If you require that a viewpager is used then look at this SO post to do with overriding the viewpager animation
Android ViewPager With different Animation like zoom in,fade etc
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.