Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I am editing your question and converting it into a very simple tutorial for you.</p> <p>Here you go:</p> <p>Main.xml</p> <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:background="@drawable/back" android:layout_height="match_parent" &gt; &lt;android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="60dp" android:layout_height="fill_parent" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/button1" &gt; &lt;/android.support.v4.view.ViewPager&gt; &lt;Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button1" android:layout_alignRight="@+id/button1" android:layout_below="@+id/button1" android:layout_marginTop="15dp" android:text="Movies" /&gt; &lt;Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button2" android:layout_below="@+id/button2" android:layout_marginTop="16dp" android:layout_toLeftOf="@+id/viewpager" android:text="Music Videos" /&gt; &lt;Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="18dp" android:layout_marginTop="110dp" android:text="Tv Shows" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>And then fragment.xml; put these files into the res/layout folder.</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/back" android:orientation="horizontal" &gt; &lt;TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginTop="24dp" /&gt; &lt;RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_below="@+id/textView" android:background="@drawable/back" &gt; &lt;RelativeLayout android:id="@+id/imageView1" android:layout_width="280dp" android:layout_height="380dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:background="@drawable/movie1" &gt; &lt;TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginBottom="37dp" android:layout_marginRight="85dp" android:text="SIMPSONS" /&gt; &lt;/RelativeLayout&gt; &lt;RelativeLayout android:id="@+id/RelativeLayout15" android:layout_width="280sp" android:layout_height="230dp" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignRight="@+id/imageView1" android:background="@drawable/movie" android:orientation="vertical" &gt; &lt;/RelativeLayout&gt; &lt;RelativeLayout android:id="@+id/RelativeLayout1" android:layout_width="20dp" android:layout_height="150dp" android:layout_alignParentBottom="true" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/imageView1" android:orientation="vertical" &gt; &lt;/RelativeLayout&gt; &lt;RelativeLayout android:id="@+id/RelativeLayout2" android:layout_width="250dp" android:layout_height="150dp" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/RelativeLayout1" android:background="@drawable/avatar" android:orientation="vertical" &gt; &lt;/RelativeLayout&gt; &lt;RelativeLayout android:id="@+id/RelativeLayout3" android:layout_width="250dp" android:layout_height="10dp" android:layout_alignLeft="@+id/RelativeLayout2" android:layout_below="@+id/RelativeLayout2" android:orientation="vertical" &gt; &lt;/RelativeLayout&gt; &lt;RelativeLayout android:id="@+id/RelativeLayout4" android:layout_width="250dp" android:layout_height="150dp" android:layout_alignLeft="@+id/RelativeLayout3" android:layout_below="@+id/RelativeLayout3" android:background="@drawable/movienight" android:orientation="vertical" &gt; &lt;/RelativeLayout&gt; &lt;RelativeLayout android:id="@+id/RelativeLayout5" android:layout_width="250dp" android:layout_height="10dp" android:layout_alignLeft="@+id/RelativeLayout4" android:layout_below="@+id/RelativeLayout4" android:orientation="vertical" &gt; &lt;/RelativeLayout&gt; &lt;RelativeLayout android:id="@+id/RelativeLayout6" android:layout_width="250sp" android:layout_height="230dp" android:layout_alignLeft="@+id/RelativeLayout5" android:layout_below="@+id/RelativeLayout5" android:background="@drawable/movie3" android:orientation="vertical" &gt; &lt;/RelativeLayout&gt; &lt;RelativeLayout android:id="@+id/RelativeLayout7" android:layout_width="70dp" android:layout_height="600dp" android:layout_alignParentBottom="true" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/RelativeLayout2" android:orientation="vertical" &gt; &lt;/RelativeLayout&gt; &lt;RelativeLayout android:id="@+id/RelativeLayout8" android:layout_width="280dp" android:layout_height="380dp" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/RelativeLayout7" android:background="@drawable/band" &gt; &lt;/RelativeLayout&gt; &lt;RelativeLayout android:id="@+id/RelativeLayout9" android:layout_width="10sp" android:layout_height="20dp" android:layout_alignLeft="@+id/RelativeLayout8" android:layout_below="@+id/RelativeLayout8" android:background="@drawable/movie3" android:orientation="vertical" &gt; &lt;/RelativeLayout&gt; &lt;RelativeLayout android:id="@+id/RelativeLayout20" android:layout_width="280sp" android:layout_height="230dp" android:layout_alignLeft="@+id/RelativeLayout9" android:layout_below="@+id/RelativeLayout9" android:background="@drawable/movie3" android:orientation="vertical" &gt; &lt;/RelativeLayout&gt; &lt;RelativeLayout android:id="@+id/RelativeLayout10" android:layout_width="20dp" android:layout_height="600dp" android:layout_alignParentTop="true" android:layout_marginTop="21dp" android:layout_toRightOf="@+id/RelativeLayout8" android:orientation="vertical" &gt; &lt;/RelativeLayout&gt; &lt;RelativeLayout android:id="@+id/RelativeLayout11" android:layout_width="250dp" android:layout_height="150dp" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/RelativeLayout10" android:background="@drawable/war" android:orientation="vertical" &gt; &lt;/RelativeLayout&gt; &lt;RelativeLayout android:id="@+id/RelativeLayout12" android:layout_width="250dp" android:layout_height="10dp" android:layout_alignLeft="@+id/RelativeLayout11" android:layout_below="@+id/RelativeLayout11" android:orientation="vertical" &gt; &lt;/RelativeLayout&gt; //// &lt;RelativeLayout android:id="@+id/RelativeLayout14" android:layout_width="250dp" android:layout_height="10dp" android:layout_alignLeft="@+id/RelativeLayout11" android:layout_below="@+id/RelativeLayout13" android:orientation="vertical" &gt; &lt;/RelativeLayout&gt; &lt;RelativeLayout android:id="@+id/RelativeLayout13" android:layout_width="250dp" android:layout_height="150dp" android:layout_above="@+id/RelativeLayout5" android:layout_alignLeft="@+id/RelativeLayout12" android:background="@drawable/war" android:orientation="vertical" &gt; &lt;/RelativeLayout&gt; &lt;RelativeLayout android:id="@+id/RelativeLayout16" android:layout_width="250sp" android:layout_height="230dp" android:layout_alignLeft="@+id/RelativeLayout14" android:layout_alignTop="@+id/RelativeLayout6" android:background="@drawable/band" android:orientation="vertical" &gt; &lt;/RelativeLayout&gt; &lt;/RelativeLayout&gt; &lt;TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/textView" android:layout_centerHorizontal="true" android:text="Favourites" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>And then in your main Activity:</p> <pre><code>public class MainActivity extends FragmentActivity { MyPageAdapter pageAdapter; ViewPager pager; static ImageView image1 ; static ImageView image2; static ImageView image3; static ImageView image4; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button1=(Button)findViewById(R.id.button1); Button button2=(Button)findViewById(R.id.button2); Button button3=(Button)findViewById(R.id.button3); List&lt;Fragment&gt; fragments = getFragments(); pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments); pager = (ViewPager)findViewById(R.id.viewpager); pager.setAdapter(pageAdapter); pager.setCurrentItem(0); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub pager.setCurrentItem(0); } }); button2.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub pager.setCurrentItem(1); } }); button3.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub pager.setCurrentItem(2); } }); } private List&lt;Fragment&gt; getFragments(){ List&lt;Fragment&gt; fList = new ArrayList&lt;Fragment&gt;(); fList.add(MyFragment.newInstance("Tv Shows")); fList.add(MyFragment.newInstance("Movies")); fList.add(MyFragment.newInstance("Music Videos")); return fList; } private class MyPageAdapter extends FragmentPagerAdapter { private List&lt;Fragment&gt; fragments; public MyPageAdapter(FragmentManager fm, List&lt;Fragment&gt; fragments) { super(fm); this.fragments = fragments; } @Override public Fragment getItem(int position) { return this.fragments.get(position); } @Override public int getCount() { return this.fragments.size(); } } } </code></pre> <p>And create a new Java class, MyFragment:</p> <pre><code>public class MyFragment extends Fragment implements AnimationListener { public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE"; private Animation animation1; private Animation animation2; private boolean isBackOfCardShowing = true; RelativeLayout image1; RelativeLayout image2; RelativeLayout image3; RelativeLayout image4; RelativeLayout image5; RelativeLayout image6; RelativeLayout image7; RelativeLayout image8; RelativeLayout image9; RelativeLayout image10; View v; Handler updateHandler; public static final MyFragment newInstance(String message) { MyFragment f = new MyFragment(); Bundle bdl = new Bundle(1); bdl.putString(EXTRA_MESSAGE, message); f.setArguments(bdl); return f; } Runnable RecurringTask = new Runnable() { public void run() { // Do whatever you want image1.clearAnimation(); image2.clearAnimation(); image3.clearAnimation(); image4.clearAnimation(); image1.setAnimation(animation1); image1.startAnimation(animation1); image2.setAnimation(animation1); image2.startAnimation(animation1); image3.setAnimation(animation1); image3.startAnimation(animation1); image4.setAnimation(animation1); image4.startAnimation(animation1); // Call this method again every 30 seconds updateHandler.postDelayed(this, 10000); } }; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { String message = getArguments().getString(EXTRA_MESSAGE); v = inflater.inflate(R.layout.myfragment_layout, container, false); TextView messageTextView = (TextView)v.findViewById(R.id.textView); messageTextView.setText(message); image1 = (RelativeLayout)v.findViewById(R.id.imageView1); image2 = (RelativeLayout)v.findViewById(R.id.RelativeLayout2); image3 = (RelativeLayout)v.findViewById(R.id.RelativeLayout15); image4 = (RelativeLayout)v.findViewById(R.id.RelativeLayout4); image5 = (RelativeLayout)v.findViewById(R.id.RelativeLayout6); image6 = (RelativeLayout)v.findViewById(R.id.RelativeLayout8); image7 = (RelativeLayout)v.findViewById(R.id.RelativeLayout20); image8 = (RelativeLayout)v.findViewById(R.id.RelativeLayout11); image9 = (RelativeLayout)v.findViewById(R.id.RelativeLayout13); image10 = (RelativeLayout)v.findViewById(R.id.RelativeLayout16); // animation1 = AnimationUtils.loadAnimation(getActivity(), R.anim.to_middle); animation1.setAnimationListener(this); animation2 = AnimationUtils.loadAnimation(getActivity(), R.anim.from_middle); animation2.setAnimationListener(this); if (message.equals("Tv Shows")) { /* image1.setBackgroundResource(R.drawable.movie1); image2.setBackgroundResource(R.drawable.movie); image3.setBackgroundResource(R.drawable.war); image4.setBackgroundResource(R.drawable.avatar); */ } if (message.equals("Movies")) { /* image1.setBackgroundResource(R.drawable.movie); image2.setBackgroundResource(R.drawable.movie1); image3.setBackgroundResource(R.drawable.movienight); image4.setBackgroundResource(R.drawable.avatar); */ } if (message.equals("Music Videos")) { /* image1.setBackgroundResource(R.drawable.movienight); image2.setBackgroundResource(R.drawable.avatar); image3.setBackgroundResource(R.drawable.movie); image4.setBackgroundResource(R.drawable.movie1); */ } // updateHandler = new Handler(); // Do this first after one second updateHandler.postDelayed(RecurringTask, 1000); return v; } @Override public void onAnimationEnd(Animation animation) { if (animation == animation1) { if (isBackOfCardShowing) { image1.setBackgroundResource(R.drawable.avatar); image2.setBackgroundResource(R.drawable.movie); image3.setBackgroundResource(R.drawable.movienight); image4.setBackgroundResource(R.drawable.movie1); } else { image2.setBackgroundResource(R.drawable.avatar); image1.setBackgroundResource(R.drawable.movie); image3.setBackgroundResource(R.drawable.movienight); image4.setBackgroundResource(R.drawable.movie1); } image1.clearAnimation(); image2.clearAnimation(); image3.clearAnimation(); image4.clearAnimation(); image1.setAnimation(animation2); image2.setAnimation(animation2); image3.setAnimation(animation2); image4.setAnimation(animation2); image1.startAnimation(animation2); image2.startAnimation(animation2); image3.startAnimation(animation2); image4.startAnimation(animation2); } else { isBackOfCardShowing=!isBackOfCardShowing; // v.findViewById(R.id.button1).setEnabled(true); } } @Override public void onAnimationRepeat(Animation arg0) { // TODO Auto-generated method stub } @Override public void onAnimationStart(Animation arg0) { // TODO Auto-generated method stub } } </code></pre> <p>And in the end, write your page adapter and you are good to go:</p> <pre><code>package com.example.pager; public class MyPageAdapter extends FragmentPagerAdapter { private List&lt;Fragment&gt; fragments; public MyPageAdapter(FragmentManager fm, List&lt;Fragment&gt; fragments) { super(fm); this.fragments = fragments; } @Override public Fragment getItem(int position) { return this.fragments.get(position); } @Override public int getCount() { return this.fragments.size(); } } </code></pre> <p>AS: Delete the code chunks which are used for animation.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload