ViewPage+Fragment+TabLayout

欢迎页面布局

<androidx.viewpager.widget.ViewPager
        android:id="@+id/viewPage"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </androidx.viewpager.widget.ViewPager>
    <RadioGroup
        android:id="@+id/radioGroup"
        android:orientation="horizontal"
        android:layout_marginBottom="80dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:gravity="center_horizontal">
        <RadioButton
            android:id="@+id/one"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:text="○"
            android:textColor="@drawable/layout_item_button"
            android:textSize="30dp"
            android:gravity="center" />
        <RadioButton
            android:id="@+id/two"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:text="○"
            android:textColor="@drawable/layout_item_button"
            android:textSize="30dp"
            android:gravity="center"/>
        <RadioButton
            android:id="@+id/three"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:text="○"
            android:textColor="@drawable/layout_item_button"
            android:textSize="30dp"
            android:gravity="center"/>
    </RadioGroup>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="立即跳转"
        android:textSize="20dp"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="20dp"
        android:visibility="gone"
        />
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="倒计时"
        android:layout_alignParentRight="true"
        android:textSize="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp"
        android:visibility="gone"
        />

功能页布局

<LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabLayout_zhu"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:tabTextColor="#B4B4B4"
            app:tabSelectedTextColor="#000"
            app:tabIndicatorColor="#FF0000"
            app:tabIndicatorHeight="5dp"
            app:tabMode="fixed">
        </com.google.android.material.tabs.TabLayout>

        <androidx.viewpager.widget.ViewPager
            android:id="@+id/viewPage_zhu"
            android:layout_weight="6"
            android:layout_width="match_parent"
            android:layout_height="0dp">

        </androidx.viewpager.widget.ViewPager>
        <RadioGroup
            android:orientation="horizontal"
            android:id="@+id/radioGroup_zhu"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="0dp">
            <RadioButton
                android:id="@+id/Button1"
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@drawable/layout_item_button2"
                android:text="联系人"
                android:drawableTop="@mipmap/ic_launcher"
                android:gravity="center"
                android:button="@null"
                />
            <RadioButton
                android:id="@+id/Button2"
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@drawable/layout_item_button2"
                android:text="动态"
                android:drawableTop="@mipmap/ic_launcher"
                android:gravity="center"
                android:button="@null"
                />
            <RadioButton
                android:id="@+id/Button3"
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@drawable/layout_item_button2"
                android:text="设置"
                android:drawableTop="@mipmap/ic_launcher"
                android:gravity="center"
                android:button="@null"
                />
            <RadioButton
                android:id="@+id/Button4"
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@drawable/layout_item_button2"
                android:text="我的"
                android:drawableTop="@mipmap/ic_launcher"
                android:gravity="center"
                android:button="@null"
                />
        </RadioGroup>
    </LinearLayout>

ListView 页面布局

<ImageView
        android:id="@+id/icon_id"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@mipmap/ic_launcher"/>
    <TextView
        android:id="@+id/title_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="标题"
        android:textSize="20sp"
        android:layout_toRightOf="@+id/icon_id"
        android:layout_marginTop="40dp"
        android:layout_marginLeft="20dp"/>

Fragment 主页布局

<LinearLayout
        android:id="@+id/line"
        android:layout_gravity="center"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center">
        <Button
            android:id="@+id/buttonWrite"
            android:layout_weight="1"
            android:text="存入SD卡数据"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <Button
            android:id="@+id/buttonRead"
            android:layout_weight="1"
            android:text="读取SD卡数据"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <Button
            android:id="@+id/buttonTime"
            android:layout_weight="1"
            android:text="获取当前日期"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

MainActivity 引导页

private RadioGroup radioGroup;
    private RadioButton one;
    private RadioButton two;
    private RadioButton three;
    private ViewPager viewPage;
    private Button button;
    private TextView textView;
    private Timer timer;
    private Timer timer1;
    private List<Fragment> list = new ArrayList<>();
    private int index = 0;
    private int num = 5;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    viewPage.setCurrentItem(index);
                    index++;
                    if (index == list.size()) {
                        index = 0;
                        //实现一个倒计时
                        timer1 = new Timer();
                        timer1.schedule(new TimerTask() {
                            @Override
                            public void run() {
                                Message message = new Message();
                                message.what = 2;
                                handler.sendMessage(message);
                            }
                        }, 0, 1000);
                    }
                    break;
                case 2:
                        textView.setText("倒计时:"+(num--));
                        if (num == 0){
                            Intent intent = new Intent(MainActivity.this,Main2Activity.class);
                            startActivity(intent);
                            timer1.cancel();
                            //在SD卡设置一个标记 用来判断引导页
                            SharedPreferences flag = getSharedPreferences("flag", MODE_PRIVATE);
                            SharedPreferences.Editor edit = flag.edit();
                            edit.putBoolean("isCome",true);
                            edit.commit();

                            finish();
                        }
                    break;
            }
        }
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SharedPreferences flag = getSharedPreferences("flag", MODE_PRIVATE);
        boolean isCome = flag.getBoolean("isCome", false);

        if (isCome){
            Intent intent = new Intent(MainActivity.this,Main2Activity.class);
            startActivity(intent);
            this.finish();
        }

        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        viewPage = (ViewPager) findViewById(R.id.viewPage);
        button = (Button) findViewById(R.id.button);
        textView = (TextView) findViewById(R.id.textView);
        button.setOnClickListener(this);
        one = (RadioButton) findViewById(R.id.one);
        one.setChecked(true);
        one.setOnClickListener(this);
        two = (RadioButton) findViewById(R.id.two);
        two.setOnClickListener(this);
        three = (RadioButton) findViewById(R.id.three);
        three.setOnClickListener(this);
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        radioGroup.setOnClickListener(this);
        //把Fragment添加进集合
        for (int i = 1; i < 4; i++) {
            ContentFragment contentFragment = new ContentFragment();
            Bundle bundle = new Bundle();
            bundle.putString("key", i + "");
            contentFragment.setArguments(bundle);
            list.add(contentFragment);
        }

        //把Fragment加到ViewPage
        viewPage.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            @Override
            public int getCount() {
                return list.size();
            }

            @NonNull
            @Override
            public Fragment getItem(int position) {
                return list.get(position);
            }
        });

        //让Fragment实现轮播效果
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.sendEmptyMessage(1);
            }
        }, 0, 1000);

        //给Fragment设置页面监听
        viewPage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            //把选择器和Fragment页面绑定
            @Override
            public void onPageSelected(int position) {
                if (position == 0){
                    one.setChecked(true);
                }else if (position == 1){
                    two.setChecked(true);
                }else {
                    three.setChecked(true);
                }
                //如果到达第三个页面停止Timer,中断轮播
                if (position == list.size() - 1) {
                    button.setVisibility(View.VISIBLE);
                    textView.setVisibility(View.VISIBLE);
                    timer.cancel();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    //跳转按钮的点击事件
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:
                Intent intent = new Intent(MainActivity.this,Main2Activity.class);
                timer1.cancel();
                startActivity(intent);

                //在SD卡设置一个标记 用来判断引导页
                SharedPreferences flag = getSharedPreferences("flag", MODE_PRIVATE);
                SharedPreferences.Editor edit = flag.edit();
                edit.putBoolean("isCome",true);
                edit.commit();

                this.finish();
                break;
        }
    }

Main2Activity 主页功能

private ViewPager viewPage_zhu;
    private RadioGroup radioGroup_zhu;
    private List<Fragment> list = new ArrayList<>();
    private List<String> listTitle = new ArrayList<>();
    private RadioButton Button1;
    private RadioButton Button2;
    private RadioButton Button3;
    private RadioButton Button4;
    private TabLayout tabLayout_zhu;
    private Fragment_item fragment_item;
    private Fragment_item2 fragment_item2;;
    private Fragment_item3 fragment_item3;;
    private Fragment_item4 fragment_item4;;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initView();
    }

    private void initView() {
        Button1 = (RadioButton) findViewById(R.id.Button1);
        Button1.setChecked(true);
        Button2 = (RadioButton) findViewById(R.id.Button2);
        Button3 = (RadioButton) findViewById(R.id.Button3);
        Button4 = (RadioButton) findViewById(R.id.Button4);
        viewPage_zhu = (ViewPager) findViewById(R.id.viewPage_zhu);
        radioGroup_zhu = (RadioGroup) findViewById(R.id.radioGroup_zhu);
        tabLayout_zhu = (TabLayout) findViewById(R.id.tabLayout_zhu);

        tabLayout_zhu.setupWithViewPager(viewPage_zhu);

        fragment_item = new Fragment_item();
        fragment_item2 = new Fragment_item2();
        fragment_item3 = new Fragment_item3();
        fragment_item4 = new Fragment_item4();
        list.add(fragment_item);
        list.add(fragment_item2);
        list.add(fragment_item3);
        list.add(fragment_item4);
        for (int i = 0; i < 4; i++) {
            listTitle.add("标题" + (i + 1));
        }
        viewPage_zhu.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 listTitle.get(position);
            }
        });
    viewPage_zhu.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            if (position == 0) {
                Button1.setChecked(true);
            } else if (position == 1) {
                Button2.setChecked(true);
            } else if (position == 2) {
                Button3.setChecked(true);
            } else {
                Button4.setChecked(true);
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });



    }

ContentFragment

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_content, container, false);
        textView = inflate.findViewById(R.id.textView_Fragment);
        Bundle bundle = getArguments();
        String key = bundle.getString("key");
        textView.setText(key);
        return inflate;
    }

Fragment_item

public class Fragment_item extends Fragment {

    private Button buttonWrite;
    private Button buttonRead;
    private Button buttonTime;

    SharedPreferences.Editor editor;
    SharedPreferences sharedPreferences;
    String s;

    public Fragment_item() {
        // 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_fragment_item, container, false);
        buttonRead = inflate.findViewById(R.id.buttonRead);
        buttonWrite = inflate.findViewById(R.id.buttonWrite);
        buttonTime = inflate.findViewById(R.id.buttonTime);


        buttonWrite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

//                new Thread(new Runnable() {
//                    @Override
//                    public void run() {
//                       s  = HttpUtils.getJsonFromUrl("http://api.yunzhancn.cn/api/app.interface.php?siteid=78703&act=column&ctype=4");
//                    }
//                }).start();
                OkGo.<String>get("http://api.yunzhancn.cn/api/app.interface.php?siteid=78703&act=column&ctype=4").execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {

                        Toast.makeText(getActivity(), response.body(), Toast.LENGTH_SHORT).show();

                        sharedPreferences = getActivity().getSharedPreferences("one", MODE_PRIVATE);
                        editor = sharedPreferences.edit();
                        editor.putString("json", response.body());
                        editor.commit();
                    }
                });

            }
        });

        buttonRead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String string = sharedPreferences.getString("json","");
                Toast.makeText(getActivity(), string, Toast.LENGTH_SHORT).show();
            }
        });
     }

Fragment_item2

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_fragment_item2, container, false);
        listView = inflate.findViewById(R.id.listView_item2);
        new MyAsyncTask(getActivity(),listView).execute("http://api.yunzhancn.cn/api/app.interface.php?siteid=78703&act=column&ctype=4");
        return inflate;
    }
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值