Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.zer.task2_1;
- import android.os.Handler;
- import android.support.v4.app.FragmentManager;
- import android.support.v4.app.FragmentTransaction;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- public class MainActivity extends AppCompatActivity {
- static int i = 1;
- static int previ = 0;
- Handler handler = new Handler();
- Runnable r;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- r = new Runnable() {
- @Override
- public void run() {
- if (i > previ && i < 4) {
- previ = i;
- i++;
- } else if (i > 1) {
- previ = i;
- i--;
- } else {
- previ = i;
- i++;
- }
- tts(i);
- handler.postDelayed(r, 1000);
- }
- };
- FragmentManager fm = getSupportFragmentManager();
- FragmentTransaction ft = fm.beginTransaction();
- Fragment1 fragment = Fragment1.newInstance();
- ft.add(R.id.container, fragment, Fragment1.class.getSimpleName());
- ft.commit();
- handler.postDelayed(r, 1000);
- }
- private void tts(int j) {
- FragmentManager fm1 = getSupportFragmentManager();
- FragmentTransaction ft1 = fm1.beginTransaction();
- switch (j) {
- case 1:
- Fragment1 fragment1 = Fragment1.newInstance();
- ft1.replace(R.id.container, fragment1, Fragment1.class.getSimpleName());
- ft1.commit();
- break;
- case 2:
- Fragment2 fragment2 = Fragment2.newInstance();
- ft1.replace(R.id.container, fragment2, Fragment2.class.getSimpleName());
- ft1.commit();
- break;
- case 3:
- Fragment3 fragment3 = Fragment3.newInstance();
- ft1.replace(R.id.container, fragment3, Fragment3.class.getSimpleName());
- ft1.commit();
- break;
- case 4:
- Fragment4 fragment4 = Fragment4.newInstance();
- ft1.replace(R.id.container, fragment4, Fragment4.class.getSimpleName());
- ft1.commit();
- break;
- }
- }
- }
- package com.example.zer.task2_1;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- public class Fragment1 extends Fragment {
- public static Fragment1 newInstance() {
- Fragment1 fr = new Fragment1();
- return fr;
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- View root = inflater.inflate(R.layout.fragment_1, container, false);
- return root;
- }
- }
- package com.example.zer.task2_1;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- public class Fragment2 extends Fragment {
- public static Fragment2 newInstance() {
- Fragment2 fr = new Fragment2();
- return fr;
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- View root = inflater.inflate(R.layout.fragment_2, container, false);
- return root;
- }
- }
- package com.example.zer.task2_1;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- public class Fragment3 extends Fragment {
- public static Fragment3 newInstance() {
- Fragment3 fr = new Fragment3();
- return fr;
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- View root = inflater.inflate(R.layout.fragment_3, container, false);
- return root;
- }
- }
- package com.example.zer.task2_1;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- public class Fragment4 extends Fragment {
- public static Fragment4 newInstance() {
- Fragment4 fr = new Fragment4();
- return fr;
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- View root = inflater.inflate(R.layout.fragment_4, container, false);
- return root;
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/activity_main"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context="com.example.zer.task2_1.MainActivity">
- <FrameLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:id="@+id/container"
- android:layout_alignParentTop="true"
- android:layout_alignParentStart="true">
- </FrameLayout>
- </RelativeLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <TextView
- android:text="fragment_1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerVertical="true"
- android:layout_centerHorizontal="true"
- android:textSize="48sp"/>
- </RelativeLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <TextView
- android:text="fragment_2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerVertical="true"
- android:layout_centerHorizontal="true"
- android:textSize="48sp"/>
- </RelativeLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent" android:layout_height="match_parent">
- <TextView
- android:text="fragment_3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerVertical="true"
- android:layout_centerHorizontal="true"
- android:textSize="48sp"/>
- </RelativeLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent" android:layout_height="match_parent">
- <TextView
- android:text="fragment_4"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerVertical="true"
- android:layout_centerHorizontal="true"
- android:textSize="48sp"/>
- </RelativeLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement