通过ViewPager的使用,实现滑屏

本文介绍了一个使用一个Activity和三个Fragment实现的应用项目布局方案。主Activity包含标题栏、按钮区域和主要内容显示区域,通过ViewPager组件切换不同的Fragment内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 我的项目布局为:

项目布局

 除了MainActivity是Activity,其他三个均为Fragment.
**一、效果 **

这里写图片描述

这里写图片描述
这里写图片描述
这里写图片描述
二、代码
1、activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="rj.smxy.myviews.MainActivity">

    <TextView
        android:id="@+id/titleliner"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="便签管理"
        android:gravity="center"
        />
    <LinearLayout
        android:id="@+id/buttonliner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="vertical"
        >
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Add"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="新建便签"
            android:gravity="center"
            />

    </LinearLayout>
    <android.support.v4.view.ViewPager
        android:id="@+id/content"
        android:layout_below="@id/titleliner"
        android:layout_above="@id/buttonliner"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

    </android.support.v4.view.ViewPager>
</RelativeLayout>

2、fragment_first.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorAccent"
    tools:context="rj.smxy.myviews.First">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="40dp"
        android:text="第一页"/>

</FrameLayout>

3、fragment_second.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    tools:context="rj.smxy.myviews.Second">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="40dp"
        android:text="第二页"/>

</FrameLayout>

4、fragment_third.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/My"
    tools:context="rj.smxy.myviews.Third">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="40dp"
        android:text="第三页" />

</FrameLayout>

5、MainActivity

package rj.smxy.myviews;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
   private First first;
    private Second second;
    private  Third third;
    private List<Fragment> listFragment;
    private ViewPager viewPager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewPager=(ViewPager)findViewById(R.id.content);
        first=new First();
        second=new Second();
        third=new Third();

        listFragment=new ArrayList<Fragment>();
        listFragment.add(first);
        listFragment.add(second);
        listFragment.add(third);

        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return listFragment.get(position);
            }

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

    }
}

其他页面默认

感想:最悲伤的话就是,这辈子只能陪你到这了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值