package com.example.app2;
import android.os.Bundle;
import android.os.Handler;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;
import com.example.app2.fragment.OneFragment;
import com.example.app2.fragment.ThreeFragment;
import com.example.app2.fragment.TwoFragment;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
private ViewPager vp;
private List<Fragment> lists = new ArrayList<>();
private int index = 0;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vp = (ViewPager) findViewById(R.id.vp);
lists.add(new OneFragment());
lists.add(new TwoFragment());
lists.add(new ThreeFragment());
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
vp.setCurrentItem(index++%lists.size());
if (index==3){
timer.cancel();
}
}
});
}
},0,1000);
vp.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
@NonNull
@Override
public Fragment getItem(int position) {
return lists.get(position);
}
@Override
public int getCount() {
return lists.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return super.getPageTitle(position);
}
});
}
}
package com.example.app2;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;
import android.os.Bundle;
import android.view.Window;
import android.widget.LinearLayout;
import com.example.app2.fragment.OFragment;
import com.example.app2.fragment.TFragment;
import com.example.app2.fragment.ThFragment;
import com.google.android.material.tabs.TabLayout;
import java.util.ArrayList;
import java.util.List;
public class Main2Activity extends AppCompatActivity {
private TabLayout tab;
private ViewPager ll;
private List<Fragment> list = new ArrayList<>();
private List<String> titls = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main2);
tab = (TabLayout) findViewById(R.id.tab);
ll = (ViewPager) findViewById(R.id.ll);
list.add(new OFragment());
list.add(new TFragment());
list.add(new ThFragment());
titls.add("We");
titls.add("EDG");
titls.add("SKT");
ll.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
@NonNull
@Override
public Fragment getItem(int position) {
return list.get(position);
}
@Override
public int getCount() {
return list.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return titls.get(position);
}
});
tab.setupWithViewPager(ll);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Main2Activity">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:tabIndicatorColor="@color/colorPrimaryDark"
app:tabTextColor="@color/colorPrimary"
app:tabMode="fixed"
/>
<androidx.viewpager.widget.ViewPager
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="8">
</androidx.viewpager.widget.ViewPager>
</LinearLayout>
package com.example.app2.fragment;
import android.content.Intent;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.example.app2.Main2Activity;
import com.example.app2.MainActivity;
import com.example.app2.R;
import java.util.Timer;
import java.util.TimerTask;
public class ThreeFragment extends Fragment {
private TextView tv;
private Button bt;
private int index = 3;
private Handler handler = new Handler();
public ThreeFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_three, container, false);
tv = inflate.findViewById(R.id.tv);
bt = inflate.findViewById(R.id.bt);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), Main2Activity.class);
startActivity(intent);
}
});
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
index--;
if (index==0){
handler.post(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(getActivity(), Main2Activity.class);
startActivity(intent);
}
});
cancel();
}else{
handler.post(new Runnable() {
@Override
public void run() {
tv.setText(index+"");
}
});
}
}
},0,1000);
return inflate;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.viewpager.widget.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/vp"
/>
</LinearLayout>