通过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(); } }); } }
其他页面默认
感想:最悲伤的话就是,这辈子只能陪你到这了。
相关文章推荐
- Android中如何使用ViewPager实现类似laucher左右拖动效果
- 使用ViewPager实现左右循环滑动
- 使用viewpager实现QQ界面切换效果
- ViewPager的使用方法和实现过程
- Android中如何使用ViewPager实现类似laucher左右拖动效果 3ff8
- Android中如何使用ViewPager实现类似laucher左右拖动效果
- Android中如何使用ViewPager实现类似laucher左右拖动效果
- 使用viewpager实现QQ界面切换效果
- Android使用ViewPager实现左右滑动效果
- 使用自定义RadioButton和ViewPager实现TabHost效果和带滑动的页卡效果。
- Android 中的通过 ViewPager 实现左右滑屏
- android 使用ViewPager实现左右滑动
- 使用ViewPager实现几个界面的切换,类似QQ
- 使用ViewPager实现欢迎页面左右拖动效果
- 使用ViewPager实现高仿launcher拖动效果
- 使用ViewPager实现高仿launcher拖动效果
- Android中如何使用ViewPager实现类似laucher左右拖动效果
- Android-使用ViewPager实现高仿launcher拖动效果(转)
- 使用ViewPager实现高仿launcher左右拖动效果
- Android中通过ViewPager实现左右滑屏(下)