View Pager und Page Transformer mit Fragments

  • Antworten:1
  • OffenNicht stickyNicht beantwortet
  • Forum-Beiträge: 184

08.03.2015, 19:08:57 via Website

Hallo zussammen ich versuche verzweifelt eine Animation an laufen zu bekommen.
Hier ist ersteinmal die Animations klasse:

public class ZoomOutPageTranformer implements ViewPager.PageTransformer{
private static final float MIN_SCALE = 0.85f;
private static final float MIN_ALPHA = 0.5f;

public void transformPage(View view, float position) {
    int pageWidth = view.getWidth();
    int pageHeight = view.getHeight();

    if (position < -1) { // [-Infinity,-1)
        // This page is way off-screen to the left.
        view.setAlpha(0);

    } else if (position <= 1) { // [-1,1]
        // Modify the default slide transition to shrink the page as well
        float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
        float vertMargin = pageHeight * (1 - scaleFactor) / 2;
        float horzMargin = pageWidth * (1 - scaleFactor) / 2;
        if (position < 0) {
            view.setTranslationX(horzMargin - vertMargin / 2);
        } else {
            view.setTranslationX(-horzMargin + vertMargin / 2);
        }

        // Scale the page down (between MIN_SCALE and 1)
        view.setScaleX(scaleFactor);
        view.setScaleY(scaleFactor);

        // Fade the page relative to its size.
        view.setAlpha(MIN_ALPHA +
                (scaleFactor - MIN_SCALE) /
                        (1 - MIN_SCALE) * (1 - MIN_ALPHA));

    } else { // (1,+Infinity]
        // This page is way off-screen to the right.
        view.setAlpha(0);
    }
}

}

Und hier ist meine MainActivity:

public class MainActivity extends FragmentActivity {


/**
 * The number of pages.
 */
private static final int NUM_PAGES = 2;

private ViewPager mPager;

private PagerAdapter mPagerAdapter;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mPager = (ViewPager) findViewById(R.id.pager);
    mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
    mPager.setAdapter(mPagerAdapter);

    mPager.setPageTransformer(ture, new ZoomOutPageTranformer());
}

}

/**
* A simple pager adapter that represents 5 ScreenSlidePageFragment objects, in
* sequence.
*/
public class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
    public ScreenSlidePagerAdapter(FragmentManager fm) {
     super(fm);
}

@Override
public Fragment getItem(int position) {
    return new MainFragment();
}

@Override
public int getCount() {
    return NUM_PAGES;
 }

}

Und die dazu gehörige xml datei:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity" tools:ignore="MergeRootFrame" >

<fragment android:name="de.nimaldev.fragments.MainFragment"
    android:id="@+id/mainFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"/>

so Und hier ist meine fragment Klasse:

public class MainFragment extends Fragment {

private Button webshop = (Button) getView().findViewById(R.id.button_webshop);
private Button aktion = (Button) getView().findViewById(R.id.button_aktionen);
private Button news = (Button) getView().findViewById(R.id.button_news);
private Button ueberUns = (Button) getView().findViewById(R.id.button_ueberUns);

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_main, container,false);

    return rootView;
}

private void onClick(View v){
    //TODO fragments + animation
    if(v == webshop){

    }
    else if(v == aktion){

    }
    else if(v == news){

    }
    else if(v == ueberUns){

    }

}

}

So jetzt zu meiner Frage wie bekomme ich es hin das die Animation durchgeführt wird so dass mein zweites Fragment erscheint..

Hier ist mein zweites Fragment:

public class SecondFragment extends Fragment {



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_second, container,false);

    return rootView;
}

private void onClick(View v){



}

}

Ein Fehler ist kein Fehler es ist ein Feature///https://play.google.com/store/apps/developer?id=Nimal+Development

Antworten

Empfohlene Artikel