day07ViewPager+Fragment+TabLayout新闻头部标题导航

简易实现

引导页布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#E745E4">
        <android.support.v4.view.ViewPager
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/guide_view_pager">

        </android.support.v4.view.ViewPager>
        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="50dp"
            android:id="@+id/guide_radio_group"
            android:layout_marginTop="20dp">
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:button="@null"
                android:drawableTop="@drawable/guide_choose"
                android:id="@+id/guide_radiobutton1"
                android:checked="true"/>
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:button="@null"
                android:drawableTop="@drawable/guide_choose"
                android:id="@+id/guide_radiobutton2"/>
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:button="@null"
                android:drawableTop="@drawable/guide_choose"
                android:id="@+id/guide_radiobutton3"/>
        </RadioGroup>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@id/guide_radio_group"
            android:background="#1588EC"
            android:text="立即体验"
            android:layout_marginLeft="100dp"
            android:layout_marginRight="100dp"
            android:id="@+id/guide_button"
            android:visibility="gone"/>
    </RelativeLayout>

</android.support.constraint.ConstraintLayout>

引导页实现及界面跳转

package com.example.week02_practice;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

    ViewPager viewPager;
    RadioButton r1,r2,r3;
    Button btn;
    ArrayList<View> arrayList = new ArrayList<>();
    Handler handler;
    int p = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = findViewById(R.id.guide_view_pager);
        r1 = findViewById(R.id.guide_radiobutton1);
        r2 = findViewById(R.id.guide_radiobutton2);
        r3 = findViewById(R.id.guide_radiobutton3);
        btn = findViewById(R.id.guide_button);
        ImageView img1 = new ImageView(this);
        img1.setImageResource(R.mipmap.guide1);
        ImageView img2 = new ImageView(this);
        img2.setImageResource(R.mipmap.guide2);
        ImageView img3 = new ImageView(this);
        img3.setImageResource(R.mipmap.guide3);
        arrayList.add(img1);
        arrayList.add(img2);
        arrayList.add(img3);
        Guide_ViewPager_Adapter guide_viewPager_adapter = new Guide_ViewPager_Adapter(arrayList);
        viewPager.setAdapter(guide_viewPager_adapter);

        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.sendEmptyMessage(1001);
                if(p == arrayList.size()){
                    p = -1;
                }
            }
        },2000,1500);
        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if(msg.what == 1001){
                    p++;
                    viewPager.setCurrentItem(p);
                }
            }
        };

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                if (i == 0){
                    r1.setChecked(true);
                }
                if (i == 1){
                    r2.setChecked(true);
                }
                if (i == 2){
                    r3.setChecked(true);
                    btn.setVisibility(View.VISIBLE);
                    btn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(MainActivity.this, Home_activity.class);
                            timer.cancel();
                            MainActivity.this.finish();
                            startActivity(intent);
                        }
                    });
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }
}

引导页ViewPager适配器

public class Guide_ViewPager_Adapter extends PagerAdapter {

    ArrayList<View> arrayList = new ArrayList<>();

    public Guide_ViewPager_Adapter(ArrayList<View> arrayList) {
        this.arrayList = arrayList;
    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return view == o;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView(arrayList.get(position));
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        container.addView(arrayList.get(position));
        return arrayList.get(position);
    }
}

主界面布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".Home_activity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@id/home_radio_group"
            android:id="@+id/home_frame_layout">

        </FrameLayout>

        <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:id="@+id/home_radio_group"
            android:layout_alignParentBottom="true">
            <RadioButton
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:button="@null"
                android:drawableTop="@drawable/home_home"
                android:text="首页"
                android:gravity="center"
                android:textColor="@drawable/home_radio_button_text"
                android:checked="true"
                android:id="@+id/home_radio_button1"/>
            <RadioButton
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:button="@null"
                android:drawableTop="@drawable/home_love"
                android:text="关注"
                android:gravity="center"
                android:textColor="@drawable/home_radio_button_text"
                android:id="@+id/home_radio_button2"/>
            <RadioButton
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:button="@null"
                android:drawableTop="@drawable/home_me"
                android:text="我的"
                android:gravity="center"
                android:textColor="@drawable/home_radio_button_text"
                android:id="@+id/home_radio_button3"/>
        </RadioGroup>

    </RelativeLayout>
</android.support.constraint.ConstraintLayout>

主界面

package com.example.week02_practice;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class Home_activity extends AppCompatActivity {

    FrameLayout frameLayout;
    RadioGroup radioGroup;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_activity);
        frameLayout = findViewById(R.id.home_frame_layout);
        radioGroup = findViewById(R.id.home_radio_group);

        Home_home home_home = new Home_home();
        getSupportFragmentManager().beginTransaction().replace(R.id.home_frame_layout,home_home).addToBackStack("home_home").commit();
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.home_radio_button1:
                        Home_home home_home = new Home_home();
                        getSupportFragmentManager().beginTransaction().replace(R.id.home_frame_layout,home_home).commit();
                        break;
                    case R.id.home_radio_button2:
                        Home_love home_love = new Home_love();
                        getSupportFragmentManager().beginTransaction().replace(R.id.home_frame_layout,home_love).commit();
                        break;
                    case R.id.home_radio_button3:
                        Home_me home_me = new Home_me();
                        getSupportFragmentManager().beginTransaction().replace(R.id.home_frame_layout,home_me).commit();
                        break;
                }
            }
        });
    }
}

首页布局

package com.example.week02_practice;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

public class Home_home extends Fragment {

    TabLayout tabLayout;
    ViewPager viewPager;
    ArrayList<Fragment> list = new ArrayList<>();
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.home_home_fragment,null);
        tabLayout = view.findViewById(R.id.home_home_tab_layout);
        viewPager = view.findViewById(R.id.home_home_view_pager);
        Home_Home_fragment01 home_home_fragment01 = new Home_Home_fragment01();
        Home_Home_fragment02 home_home_fragment02 = new Home_Home_fragment02();
        Home_Home_fragment03 home_home_fragment03 = new Home_Home_fragment03();
        list.add(home_home_fragment01);
        list.add(home_home_fragment02);
        list.add(home_home_fragment03);
        Home_ViewPager_Adapter home_viewPager_adapter = new Home_ViewPager_Adapter(getChildFragmentManager(), list);
        viewPager.setAdapter(home_viewPager_adapter);
        //将TabLayout与Vierpager绑定到一起
        tabLayout.setupWithViewPager(viewPager);
        return view;
    }
}

首页适配器

public class Home_ViewPager_Adapter extends FragmentPagerAdapter {

    String[] strings = {"全部","最近","热门"};
    ArrayList<Fragment> list = new ArrayList<>();

    public Home_ViewPager_Adapter(FragmentManager fm, ArrayList<Fragment> list) {
        super(fm);
        this.list = list;
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return strings[position];
    }

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

    @Override
    public int getCount() {
        return list.size();
    }
}
TabView导包

implementation ‘com.android.support:design:28.0.0’

<android.support.design.widget.TabLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/home_home_tab_layout"
    app:tabTextColor="#38BBBB"
    app:tabSelectedTextColor="#4E1AE2"<!-- 设置选中时文字颜色 -->
    app:tabIndicatorHeight="4dp"<!-- 设置下面选中条的高度 -->
    app:tabIndicatorColor="#4E1AE2"<!-- 设置下面选中条的颜色 -->
    app:tabMode="scrollable"<!-- 设置可滑动 --> >
</android.support.design.widget.TabLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值