7月

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 CountDownTimer(100000,2000){
//
//            @Override
//            public void onTick(long millisUntilFinished) {
//                //设置当前ViewPager显示的那个页面
//                viewpag.setCurrentItem(index);
//                ++index;
//                if (index == list.size()){
//                    timer.cancel();
//                }
//
//            }
//
//            @Override
//            public void onFinish() {
//
//            }
//        };
//        timer.start();

        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("留言");

//        MyFragmentAdapter myFragmentAdapter = new MyFragmentAdapter(getSupportFragmentManager(),list,titles);
//        viewpag.setAdapter(myFragmentAdapter);

        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;

/**
 * A simple {@link Fragment} subclass.
 */
public class FourFragment extends Fragment {

    private TextView text1;
    private Button button;
    private int index = 3;
    private Handler handler = new Handler();

    public FourFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值