Advertisement
xavadonx

Task 1

Sep 29th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.01 KB | None | 0 0
  1. package com.example.zer.task2_1;
  2.  
  3. import android.os.Handler;
  4. import android.support.v4.app.FragmentManager;
  5. import android.support.v4.app.FragmentTransaction;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.os.Bundle;
  8.  
  9. public class MainActivity extends AppCompatActivity {
  10.  
  11.     static int i = 1;
  12.     static int previ = 0;
  13.  
  14.     Handler handler = new Handler();
  15.     Runnable r;
  16.  
  17.     @Override
  18.     protected void onCreate(Bundle savedInstanceState) {
  19.         super.onCreate(savedInstanceState);
  20.         setContentView(R.layout.activity_main);
  21.  
  22.         r = new Runnable() {
  23.             @Override
  24.             public void run() {
  25.  
  26.                 if (i > previ && i < 4) {
  27.                     previ = i;
  28.                     i++;
  29.                 } else if (i > 1) {
  30.                     previ = i;
  31.                     i--;
  32.                 } else {
  33.                     previ = i;
  34.                     i++;
  35.                 }
  36.  
  37.                 tts(i);
  38.                 handler.postDelayed(r, 1000);
  39.             }
  40.         };
  41.         FragmentManager fm = getSupportFragmentManager();
  42.         FragmentTransaction ft = fm.beginTransaction();
  43.         Fragment1 fragment = Fragment1.newInstance();
  44.         ft.add(R.id.container, fragment, Fragment1.class.getSimpleName());
  45.         ft.commit();
  46.         handler.postDelayed(r, 1000);
  47.     }
  48.  
  49.     private void tts(int j) {
  50.         FragmentManager fm1 = getSupportFragmentManager();
  51.         FragmentTransaction ft1 = fm1.beginTransaction();
  52.  
  53.         switch (j) {
  54.             case 1:
  55.                 Fragment1 fragment1 = Fragment1.newInstance();
  56.                 ft1.replace(R.id.container, fragment1, Fragment1.class.getSimpleName());
  57.                 ft1.commit();
  58.                 break;
  59.             case 2:
  60.                 Fragment2 fragment2 = Fragment2.newInstance();
  61.                 ft1.replace(R.id.container, fragment2, Fragment2.class.getSimpleName());
  62.                 ft1.commit();
  63.                 break;
  64.             case 3:
  65.                 Fragment3 fragment3 = Fragment3.newInstance();
  66.                 ft1.replace(R.id.container, fragment3, Fragment3.class.getSimpleName());
  67.                 ft1.commit();
  68.                 break;
  69.             case 4:
  70.                 Fragment4 fragment4 = Fragment4.newInstance();
  71.                 ft1.replace(R.id.container, fragment4, Fragment4.class.getSimpleName());
  72.                 ft1.commit();
  73.                 break;
  74.         }
  75.     }
  76. }
  77.  
  78. package com.example.zer.task2_1;
  79.  
  80. import android.os.Bundle;
  81. import android.support.v4.app.Fragment;
  82. import android.view.LayoutInflater;
  83. import android.view.View;
  84. import android.view.ViewGroup;
  85.  
  86. public class Fragment1 extends Fragment {
  87.  
  88.     public static Fragment1 newInstance() {
  89.         Fragment1 fr = new Fragment1();
  90.         return fr;
  91.     }
  92.  
  93.     @Override
  94.     public void onCreate(Bundle savedInstanceState) {
  95.         super.onCreate(savedInstanceState);
  96.     }
  97.  
  98.     @Override
  99.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  100.         View root = inflater.inflate(R.layout.fragment_1, container, false);
  101.         return root;
  102.     }
  103. }
  104.  
  105. package com.example.zer.task2_1;
  106.  
  107. import android.os.Bundle;
  108. import android.support.v4.app.Fragment;
  109. import android.view.LayoutInflater;
  110. import android.view.View;
  111. import android.view.ViewGroup;
  112.  
  113. public class Fragment2 extends Fragment {
  114.  
  115.     public static Fragment2 newInstance() {
  116.         Fragment2 fr = new Fragment2();
  117.         return fr;
  118.     }
  119.  
  120.     @Override
  121.     public void onCreate(Bundle savedInstanceState) {
  122.         super.onCreate(savedInstanceState);
  123.     }
  124.  
  125.     @Override
  126.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  127.         View root = inflater.inflate(R.layout.fragment_2, container, false);
  128.         return root;
  129.     }
  130. }
  131.  
  132. package com.example.zer.task2_1;
  133.  
  134. import android.os.Bundle;
  135. import android.support.v4.app.Fragment;
  136. import android.view.LayoutInflater;
  137. import android.view.View;
  138. import android.view.ViewGroup;
  139.  
  140. public class Fragment3 extends Fragment {
  141.  
  142.     public static Fragment3 newInstance() {
  143.         Fragment3 fr = new Fragment3();
  144.         return fr;
  145.     }
  146.  
  147.     @Override
  148.     public void onCreate(Bundle savedInstanceState) {
  149.         super.onCreate(savedInstanceState);
  150.     }
  151.  
  152.     @Override
  153.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  154.         View root = inflater.inflate(R.layout.fragment_3, container, false);
  155.         return root;
  156.     }
  157. }
  158.  
  159. package com.example.zer.task2_1;
  160.  
  161. import android.os.Bundle;
  162. import android.support.v4.app.Fragment;
  163. import android.view.LayoutInflater;
  164. import android.view.View;
  165. import android.view.ViewGroup;
  166.  
  167. public class Fragment4 extends Fragment {
  168.  
  169.     public static Fragment4 newInstance() {
  170.         Fragment4 fr = new Fragment4();
  171.         return fr;
  172.     }
  173.  
  174.     @Override
  175.     public void onCreate(Bundle savedInstanceState) {
  176.         super.onCreate(savedInstanceState);
  177.     }
  178.  
  179.     @Override
  180.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  181.         View root = inflater.inflate(R.layout.fragment_4, container, false);
  182.         return root;
  183.     }
  184. }
  185.  
  186. <?xml version="1.0" encoding="utf-8"?>
  187. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  188.     xmlns:tools="http://schemas.android.com/tools"
  189.     android:id="@+id/activity_main"
  190.     android:layout_width="match_parent"
  191.     android:layout_height="match_parent"
  192.     android:paddingBottom="@dimen/activity_vertical_margin"
  193.     android:paddingLeft="@dimen/activity_horizontal_margin"
  194.     android:paddingRight="@dimen/activity_horizontal_margin"
  195.     android:paddingTop="@dimen/activity_vertical_margin"
  196.     tools:context="com.example.zer.task2_1.MainActivity">
  197.  
  198.     <FrameLayout
  199.         android:layout_width="match_parent"
  200.         android:layout_height="match_parent"
  201.         android:id="@+id/container"
  202.         android:layout_alignParentTop="true"
  203.         android:layout_alignParentStart="true">
  204.  
  205.     </FrameLayout>
  206. </RelativeLayout>
  207.  
  208. <?xml version="1.0" encoding="utf-8"?>
  209. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  210.     android:layout_width="match_parent"
  211.     android:layout_height="match_parent">
  212.  
  213.     <TextView
  214.         android:text="fragment_1"
  215.         android:layout_width="wrap_content"
  216.         android:layout_height="wrap_content"
  217.         android:layout_centerVertical="true"
  218.         android:layout_centerHorizontal="true"
  219.         android:textSize="48sp"/>
  220. </RelativeLayout>
  221.  
  222. <?xml version="1.0" encoding="utf-8"?>
  223. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  224.     android:layout_width="match_parent"
  225.     android:layout_height="match_parent">
  226.  
  227.     <TextView
  228.         android:text="fragment_2"
  229.         android:layout_width="wrap_content"
  230.         android:layout_height="wrap_content"
  231.         android:layout_centerVertical="true"
  232.         android:layout_centerHorizontal="true"
  233.         android:textSize="48sp"/>
  234. </RelativeLayout>
  235.  
  236. <?xml version="1.0" encoding="utf-8"?>
  237. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  238.     android:layout_width="match_parent" android:layout_height="match_parent">
  239.  
  240.     <TextView
  241.         android:text="fragment_3"
  242.         android:layout_width="wrap_content"
  243.         android:layout_height="wrap_content"
  244.         android:layout_centerVertical="true"
  245.         android:layout_centerHorizontal="true"
  246.         android:textSize="48sp"/>
  247. </RelativeLayout>
  248.  
  249. <?xml version="1.0" encoding="utf-8"?>
  250. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  251.     android:layout_width="match_parent" android:layout_height="match_parent">
  252.  
  253.     <TextView
  254.         android:text="fragment_4"
  255.         android:layout_width="wrap_content"
  256.         android:layout_height="wrap_content"
  257.         android:layout_centerVertical="true"
  258.         android:layout_centerHorizontal="true"
  259.         android:textSize="48sp"/>
  260. </RelativeLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement