53 Android fragment ViewPager 滑动效果
来源:互联网 发布:淘宝刷到钻石多少钱 编辑:程序博客网 时间:2024/06/09 13:55
layout 文件下 三个 xml 布局文件 (View)
f1.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/imageView1" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/a1" /></LinearLayout>
f2.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/a2" /></LinearLayout>
f3.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/a4" /></LinearLayout>
activity_main.xml (主布局 里面加 ViewPager)
<LinearLayout 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:orientation="vertical" > <!-- 看作一个控件 --> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" > <!-- 标题 --> <android.support.v4.view.PagerTitleStrip android:id="@+id/pagerTitleStrip" android:layout_width="match_parent" android:layout_height="20dp" android:layout_gravity="top" > </android.support.v4.view.PagerTitleStrip> </android.support.v4.view.ViewPager> </LinearLayout>
MainActivity.java (主要代码)
package com.example.android_fragment_viewpager;import java.util.ArrayList;import java.util.List;import android.os.Bundle;import android.app.Activity;import android.support.v4.view.PagerAdapter;import android.support.v4.view.PagerTitleStrip;import android.support.v4.view.ViewPager;import android.view.LayoutInflater;import android.view.Menu;import android.view.View;import android.view.ViewGroup;/** * 侧滑的实现 * @author Administrator * */public class MainActivity extends Activity {private ViewPager viewPager;//表示ViewPager 显示的内容private List<View> contentlist;//表示ViewPager 显示的标题private List<String> title;private LayoutInflater inflater;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);viewPager=(ViewPager)findViewById(R.id.viewpager);inflater=LayoutInflater.from(this);//使用静态contentlist=new ArrayList<View>();//给每一个 Page 添加一个 ViewView firstView=inflater.inflate(R.layout.f1, null);View secondView=inflater.inflate(R.layout.f2, null);View thirdView=inflater.inflate(R.layout.f3, null);contentlist.add(firstView);contentlist.add(secondView);contentlist.add(thirdView);//给每一页添加标题title=new ArrayList<String>();title.add("体育");title.add("娱乐");title.add("新闻");//设置适配器viewPager.setAdapter(new MyAdapter());}/**自定义适配器 * * @author Administrator * */public class MyAdapter extends PagerAdapter{@Overridepublic Object instantiateItem(ViewGroup container, int position) {// TODO Auto-generated method stub ((ViewPager)container).addView(contentlist.get(position));;return contentlist.get(position);}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn contentlist.size();}@Overridepublic boolean isViewFromObject(View arg0, Object arg1) {// TODO Auto-generated method stubreturn arg0==arg1;}@Overridepublic void destroyItem(ViewGroup container, int position, Object object) {// TODO Auto-generated method stub((ViewPager)container).removeView(contentlist.get(position));//super.destroyItem(container, position, object);}//至少需要以上四个@Overridepublic CharSequence getPageTitle(int position) {// TODO Auto-generated method stubreturn title.get(position);}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
//专门用于 ViewPage 的适配器 FragmentPagerAdapter FragmentStatePageAdapter
0 0
- 53 Android fragment ViewPager 滑动效果
- Android ViewPager + Fragment 实现滑动标签效果
- viewpager+fragment实现滑动效果
- Android 利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效果
- Android 利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效果
- Android 利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效果
- Android ViewPager和Fragment实现顶部导航界面滑动效果
- Android 利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效果
- Android ViewPager和Fragment实现顶部导航界面滑动效果
- Android ViewPager和Fragment实现顶部导航界面滑动效果
- Android :ViewPager+Fragment 实现顶部导航滑动效果
- android viewpager和fragment相结合,实现菜单的滑动效果
- Android ViewPager和Fragment实现顶部导航界面滑动效果
- Android ViewPager和Fragment实现顶部导航界面滑动效果
- android开发步步为营之53:viewpager+fragment构造可左右滑动标签页效果
- ViewPager+Fragment 滑动菜单效果 实现步骤
- viewpager+Fragment实现简单滑动切换效果
- ViewPager+Fragment+RadioGruop实现滑动效果
- 在Spring3中,配置DataSource的方法有6种。
- 瞅瞅Windows Server 2012那些新功能
- C指针原理(55)-C语言-pvm并行计算
- Linux vmstat 命令 - 报告虚拟内存统计的工具
- 内存分为哪些区
- 53 Android fragment ViewPager 滑动效果
- Notes模板说明
- 一张图明白Activity和Fragment对应的生命周期
- sdafbsdf
- 十步完全理解SQL
- iphone开发 IOS 组织架构图
- httpclient 学习笔记
- JS作用域
- cocos2dx之onEnter,onExit