package com.example.app4;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
import com.example.app4.fragment.FourFragment;
import com.example.app4.fragment.OneFragment;
import com.example.app4.fragment.ThreeFragment;
import com.example.app4.fragment.TwoFragment;
import com.google.android.material.tabs.TabLayout;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
private TabLayout tablayout;
private ViewPager viewpag;
private Timer timer;
private Handler handler = new Handler();
private List<Fragment> list = new ArrayList<>();
private List<String> titles = new ArrayList<>();
int index = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
tablayout = (TabLayout) findViewById(R.id.tablayout);
viewpag = (ViewPager) findViewById(R.id.viewpag);
list.add(new OneFragment());
list.add(new TwoFragment());
list.add(new ThreeFragment());
list.add(new FourFragment());
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
index++;
viewpag.setCurrentItem(index);
}
});
}
},0,1000);
titles.add("视频");
titles.add("新闻");
titles.add("图片");
titles.add("留言");
viewpag.setAdapter(new FragmentPagerAdapter(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 titles.get(position);
}
});
tablayout.setupWithViewPager(viewpag);
}
}
package com.example.app4;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import java.util.List;
public class MyFragmentAdapter extends FragmentStatePagerAdapter {
private List<Fragment> list;
private List<String> titles;
public MyFragmentAdapter(@NonNull FragmentManager fm,List<Fragment> list,List<String> titles) {
super(fm);
this.list = list;
this.titles = titles;
}
@NonNull
@Override
public int getCount() {
return list.size();
}
@Override
public Fragment getItem(int i) {
return list.get(i);
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return titles.get(position);
}
}
package com.example.app4.fragment;
import android.content.Intent;
import android.os.Bundle;
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 androidx.fragment.app.Fragment;
import com.example.app4.Main2Activity;
import com.example.app4.R;
import java.util.Timer;
import java.util.TimerTask;
public class FourFragment extends Fragment {
private TextView text1;
private Button button;
private int index = 3;
private Handler handler = new Handler();
public FourFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_four, container, false);
text1 = (TextView) inflate.findViewById(R.id.text1);
button = (Button) inflate.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), Main2Activity.class);
startActivity(intent);
}
});
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
index--;
text1.setText(index+"");
if (index==0){
handler.post(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(getActivity(), Main2Activity.class);
startActivity(intent);
}
});
cancel();
}
}
});
}
},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:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="0dp"
app:tabSelectedTextColor="@color/colorPrimaryDark"
app:tabTextColor="@color/colorPrimary"
app:tabIndicatorColor="@color/colorPrimaryDark"
app:tabMode="fixed"
android:layout_weight="1"
/>
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewpag"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8"
/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:gravity="center"
android:id="@+id/rg"
>
<RadioButton
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="空间"
android:gravity="center"
android:button="@null"/>
<RadioButton
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="联系人"
android:gravity="center"
android:button="@null"/>
<RadioButton
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="信息"
android:gravity="center"
android:button="@null"/>
<RadioButton
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="朋友圈"
android:gravity="center"
android:button="@null"/>
</RadioGroup>
</LinearLayout>
<?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:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="0dp"
app:tabSelectedTextColor="@color/colorPrimaryDark"
app:tabTextColor="@color/colorPrimary"
app:tabIndicatorColor="@color/colorPrimaryDark"
app:tabMode="fixed"
android:layout_weight="1"
/>
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewpag"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8"
/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:gravity="center"
android:id="@+id/rg"
>
<RadioButton
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="空间"
android:gravity="center"
android:button="@null"/>
<RadioButton
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="联系人"
android:gravity="center"
android:button="@null"/>
<RadioButton
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="信息"
android:gravity="center"
android:button="@null"/>
<RadioButton
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="朋友圈"
android:gravity="center"
android:button="@null"/>
</RadioGroup>
</LinearLayout>