ViewPager
由普通layout構成的viewPager
一般用於展示類的頁面(如新的app開啟出現的引導頁)
activity_main.xml
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<android.support.v4.view.ViewPager
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/pager"
>
<android.support.v4.view.PagerTabStrip
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:id="@+id/tab">
</android.support.v4.view.PagerTabStrip>
</android.support.v4.view.ViewPager>
</RelativeLayout>
定義介面卡
package com.example.administrator.viewpager;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
/**
* Created by Administrator on 2015/7/22.
*/
public class MyPagerAdapter extends PagerAdapter{
private List<View> viewList;
private List<String> titleList;
public MyPagerAdapter(List<View> viewList,List<String> titleList){
this.viewList = viewList;
this.titleList = titleList;
}
//返回viewPager的個數
@Override
public int getCount() {
return viewList.size();
}
//view 是否來源於物件
@Override
public boolean isViewFromObject(View view, Object o) {
return view == o;
}
//例項化一個 頁卡
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(viewList.get(position));
return viewList.get(position);
}
//銷燬一個 頁卡
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(viewList.get(position));
}
@Override
public CharSequence getPageTitle(int position) {
return titleList.get(position);
}
}
MainActivity.java
package com.example.administrator.viewpager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewParent;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends ActionBarActivity {
private List<View> viewList;
private List<String> titleList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view1 = View.inflate(this,R.layout.view1,null);
View view2 = View.inflate(this,R.layout.view2,null);
View view3 = View.inflate(this,R.layout.view3,null);
View view4 = View.inflate(this,R.layout.view4,null);
viewList = new ArrayList<View>();
viewList.add(view1);
viewList.add(view2);
viewList.add(view3);
viewList.add(view4);
titleList = new ArrayList<String>();
titleList.add("第一頁");
titleList.add("第二頁");
titleList.add("第三頁");
titleList.add("第四頁");
MyPagerAdapter adapter = new MyPagerAdapter(viewList,titleList);
ViewPager viewPager = (ViewPager)findViewById(R.id.pager);
viewPager.setAdapter(adapter);
}
}
由Fragment構成的ViewPager1
使用FragmentPagerAdapter介面卡(不會自動銷燬超過三個的pager<目前理解>)
MyFragmentPagerAdapter
package com.example.administrator.viewpager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.List;
/**
* Created by Administrator on 2015/7/22.
*/
public class MyFragmentPagerAdapter extends FragmentPagerAdapter{
private List<Fragment> fragmentList;
private List<String> titleList;
public MyFragmentPagerAdapter(FragmentManager fm, List<Fragment> fragmentList,List<String> titleList) {
super(fm);
this.fragmentList = fragmentList;
this.titleList = titleList;
}
@Override
public Fragment getItem(int i) {
return fragmentList.get(i);
}
@Override
public int getCount() {
return fragmentList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return titleList.get(position);
}
}
MainActivity
MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(getSupportFragmentManager(),fragmentList,titleList);
ViewPager viewPager = (ViewPager)findViewById(R.id.pager);
viewPager.setAdapter(adapter);
由Fragment構成的ViewPager2
使用FragmentStatePagerAdapter介面卡(超過三個會自動銷燬)
只需要介面卡繼承FragmentStatePagerAdapter即可
相關文章
- ViewPager(通過反射修改viewpager切換速度)Viewpager反射
- ViewPager 系列之 打造一個通用的 ViewPagerViewpager
- ViewPager之indicatorViewpagerIndicator
- AndroidBanner - ViewPager 03AndroidViewpager
- ViewPager最佳實踐Viewpager
- ViewPager的那些事Viewpager
- ViewPager 要被廢棄?官方 ViewPager2 升級版來臨Viewpager
- Android Banner - ViewPager 02AndroidViewpager
- viewpager 動態增減Viewpager
- 在Fragment裡使用viewpagerFragmentViewpager
- FragmentContainerView、ViewPager、ViewPager2與 BottomNaviagtionView結合使用的區別FragmentAIViewpager
- 自定義控制元件ViewPager控制元件Viewpager
- 自定義ViewPager指示器Viewpager
- TabLayout+ViewPager 懶載入TabLayoutViewpager
- Android ViewPager使用詳解AndroidViewpager
- RecyclerView 、ViewPager 左右滑動衝突Viewpager
- 1-VIII–ViewPager的基本使用Viewpager
- ViewPager巢狀fragment簡單使用Viewpager巢狀Fragment
- tablayout+viewpager+fragment懶載入TabLayoutViewpagerFragment
- Android 禁止ViewPager左右滑動AndroidViewpager
- ViewPager之標籤的自定義Viewpager
- 左右迴圈滑動的viewpagerViewpager
- Android ViewPager 的使用總結AndroidViewpager
- Android使用(TabLayout+ViewPager+fragment)與(FragmentTabHost+ViewPager+Fragment)實現底部狀態列切換AndroidTabLayoutViewpagerFragment
- Android ViewPager2 + Fragment 聯動AndroidViewpagerFragment
- ViewPager+Fragment LazyLoad 最優解ViewpagerFragment
- ViewPager 禁止左右滑動完美解決Viewpager
- Android開發之ViewPager+FragmentAndroidViewpagerFragment
- 【Android ViewPager】解決ViewPager巢狀時在API 13及其以下版本中不能滑動的問題AndroidViewpager巢狀API
- ViewPager、Fragment和TabLayout實現切頁效果ViewpagerFragmentTabLayout
- ViewPager2重大更新,支援offscreenPageLimitViewpagerMIT
- TabLayout+ViewPager+Fragment實現切頁展示TabLayoutViewpagerFragment
- ViewPager最簡單的無限輪播Viewpager
- TabLayout-ViewPager填充Fragment和懶載入TabLayoutViewpagerFragment
- Android實現雙層ViewPager巢狀AndroidViewpager巢狀
- TabLayout+ViewPager+fragment實現懶載入TabLayoutViewpagerFragment
- TabLayout+ViewPager+Fragment懶載入實現TabLayoutViewpagerFragment
- 用Scroller完成一個簡單的ViewPagerViewpager