您的位置:首页 > 其它

通过ViewPager的使用,实现滑屏

2017-03-24 11:29 148 查看
我的项目布局为:




除了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:gravi
4000
ty="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();
}
});

}
}


其他页面默认

感想:最悲伤的话就是,这辈子只能陪你到这了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: