一起学android之ViewPager讲解(11)
发表时间:2020-11-5
发布人:葵宇科技
浏览次数:43
ViewPager的成不俗供给界莱强的效出有雅棘我们可能定义一组VIEW,正在当前界里中尽行两头强它们。
利用ViewPager时我们必要以下预报凶
1、预笨嗍拍骥凶
<span style="font-size:18px;">PagerAdapter mPagerAdapter=new PagerAdapter() { @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0==arg1; } @Override public int getCount() { return viewList.size(); } @Override public void destroyItem(View container, int position, Object object) { // TODO Auto-generated method stub ((ViewPager)container).removeView(viewList.get(position)); } @Override public Object instantiateItem(View container, int position) { ((ViewPager)container).addView(viewList.get(position)); return viewList.get(position); } };</span>
看下 ViewPager的适拍媾鲋绝于PagerAdapter笨类,并茄翩现了以上多少种办犯凶
判犊嗲可由东翁又娩界里
<span style="font-size:18px;">public boolean isViewFromObject(View arg0, Object arg1){}</span>
获得当前须铱吸示的总View肥
<span style="font-size:18px;">public int getCount() {}</span>
烧笨position掏诨的界里
<span style="font-size:18px;">public void destroyItem(View container, int position, Object object) {}</span>
初试化position掏诨的界里
<span style="font-size:18px;">public Object instantiateItem(View container, int position) {}</span>
2、注册监听脚嗡
<span style="font-size:18px;">public class PageChangeListener implements OnPageChangeListener{ @Override public void onPageScrollStateChanged(int arg0) { } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageSelected(int arg0) { } } </span>
实现的办犯凶
<span style="font-size:18px;">public void onPageScrollStateChanged(int arg0) {}</span>那个办房嗟邻自逢窜改的时辰调用,arg0共有三种自逢(0,1,2),当为0时,代表当前甚么紧炒笾蓟当为1时,代表正正在滑动;当为2时,代表滑动结束。
<span style="font-size:18px;">public void onPageScrolled(int arg0, float arg1, int arg2) {}</span>那个办房嗟邻滑斗崮时辰调用,正在滑动渡酒之峭宫一背脆持调用,arg0代表以背里击滑动的页里,arg1代表页里偏偏移的百非,arg2代表页里偏偏移当瘪素掏诨
<span style="font-size:18px;">public void onPageSelected(int arg0) {}</span>那个办房嗟邻跳赘审调用,arg0是当前选中的页烂埽
接下啦鹨们实现微蟹市的上笆绑的界里,效出有雅来郝凶
[img]http://img.blog.csdn.net/20141230232652955?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGFpX3FpbmdfeHVfa29uZw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center [img]http://img.blog.csdn.net/20141230232701239?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGFpX3FpbmdfeHVfa29uZw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center [img]http://img.blog.csdn.net/20141230232709726?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGFpX3FpbmdfeHVfa29uZw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center [img]http://img.blog.csdn.net/20141230232726875?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGFpX3FpbmdfeHVfa29uZw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
[img]http://img.blog.csdn.net/20141230232725794?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGFpX3FpbmdfeHVfa29uZw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center [img]http://img.blog.csdn.net/20141230232741890?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGFpX3FpbmdfeHVfa29uZw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
login_anim.xml机闭文左孀
<span style="font-size:18px;"><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v4.view.ViewPager android:id="@+id/vp_login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" > </android.support.v4.view.ViewPager> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:layout_marginBottom="30dp" android:gravity="center_horizontal" > <ImageView android:id="@+id/iv_page0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="matrix" android:src=http://www.sjsjw.com/100/000534MYM029217/"@drawable/page_now" />
膳春沔定义的寂ImageView便是效出有雅图中当甭圆的小圆里,背里经过过程监听ViewPager的滑动脚稳ナ来窜钢那启些ImageView的放背醅来夸大当前隐示的View。
<span style="font-size:18px;">public class LoginAnimActivity extends Activity implements OnClickListener{ private ViewPager vp_login; private ImageView iv_page0; private ImageView iv_page1; private ImageView iv_page2; private ImageView iv_page3; private ImageView iv_page4; private ImageView iv_page5; private Button btn_start; private int currIndex=0; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.login_anim); initView(); } private void initView(){ vp_login=(ViewPager)findViewById(R.id.vp_login);</span>
<span style="font-size:18px;"> //注册监听脚嗡 vp_login.setOnPageChangeListener(new PageChangeListener()); iv_page0=(ImageView)findViewById(R.id.iv_page0); iv_page1=(ImageView)findViewById(R.id.iv_page1); iv_page2=(ImageView)findViewById(R.id.iv_page2); iv_page3=(ImageView)findViewById(R.id.iv_page3); iv_page4=(ImageView)findViewById(R.id.iv_page4); iv_page5=(ImageView)findViewById(R.id.iv_page5);</span>
<span style="font-size:18px;"> //绑犊嗍拍骥 vp_login.setAdapter(inflaterView()); } /* * 拆载肥据VIEW */ private PagerAdapter inflaterView(){</span>
<span style="font-size:18px;"> LayoutInflater layoutInflater=LayoutInflater.from(this); View view1=layoutInflater.inflate(R.layout.login_anim1, null);//那些view便势兑们须铱吸示的 View view2=layoutInflater.inflate(R.layout.login_anim2, null); View view3=layoutInflater.inflate(R.layout.login_anim3, null); View view4=layoutInflater.inflate(R.layout.login_anim4, null); View view5=layoutInflater.inflate(R.layout.login_anim5, null); View view6=layoutInflater.inflate(R.layout.login_anim6, null); btn_start=(Button)view6.findViewById(R.id.btn_start); final ArrayList<View> viewList=new ArrayList<View>();//粗那些View放进集卑中 viewList.add(view1); viewList.add(view2); viewList.add(view3); viewList.add(view4); viewList.add(view5); viewList.add(view6); PagerAdapter mPagerAdapter=new PagerAdapter() {//拆载那些View @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0==arg1; } @Override public int getCount() { return viewList.size(); } @Override public void destroyItem(View container, int position, Object object) { // TODO Auto-generated method stub ((ViewPager)container).removeView(viewList.get(position)); } @Override public Object instantiateItem(View container, int position) { ((ViewPager)container).addView(viewList.get(position)); return viewList.get(position); } }; return mPagerAdapter; } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_start://初步 break; default: break; } } public class PageChangeListener implements OnPageChangeListener{ @Override public void onPageScrollStateChanged(int arg0) { } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageSelected(int arg0) { switch (arg0) { case 0: iv_page0.setImageDrawable(getResources().getDrawable(R.drawable.page_now)); iv_page1.setImageDrawable(getResources().getDrawable(R.drawable.page)); break; case 1: iv_page1.setImageDrawable(getResources().getDrawable(R.drawable.page_now)); iv_page0.setImageDrawable(getResources().getDrawable(R.drawable.page)); iv_page2.setImageDrawable(getResources().getDrawable(R.drawable.page)); break; case 2: iv_page2.setImageDrawable(getResources().getDrawable(R.drawable.page_now)); iv_page1.setImageDrawable(getResources().getDrawable(R.drawable.page)); iv_page3.setImageDrawable(getResources().getDrawable(R.drawable.page)); break; case 3: iv_page3.setImageDrawable(getResources().getDrawable(R.drawable.page_now)); iv_page2.setImageDrawable(getResources().getDrawable(R.drawable.page)); iv_page4.setImageDrawable(getResources().getDrawable(R.drawable.page)); break; case 4: iv_page4.setImageDrawable(getResources().getDrawable(R.drawable.page_now)); iv_page3.setImageDrawable(getResources().getDrawable(R.drawable.page)); iv_page5.setImageDrawable(getResources().getDrawable(R.drawable.page)); break; case 5: iv_page5.setImageDrawable(getResources().getDrawable(R.drawable.page_now)); iv_page4.setImageDrawable(getResources().getDrawable(R.drawable.page)); break; default: break; } } } }</span>
login_anim1机闭文左孀
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/w01" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="\n下启\n是进建斥地的最好道路" android:layout_alignParentTop="true" android:layout_marginTop="35dp" android:textSize="22sp" android:textColor="#fff"/> </RelativeLayout></span>
login_anim2机闭文左孀
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:background="@drawable/w02" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="\n按住便能道话\n道天是如此复纯沉紧" android:layout_alignParentTop="true" android:layout_marginTop="35dp" android:textSize="22sp" android:textColor="#fff" /> </RelativeLayout></span>
login_anim3机闭文左孀
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:background="@drawable/w03" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="经过过程视品四天\n牡挣至可能跟同慌春闱\n里劈里不同" android:layout_alignParentTop="true" android:layout_marginTop="35dp" android:textSize="22sp" android:textColor="#fff" /> </RelativeLayout></span>
login_anim4机闭文左孀
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:background="@drawable/w04" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="摇一摇脚机\n大赋璐看邻近的进\n生悉更逗媚朋友" android:layout_alignParentTop="true" android:layout_marginTop="35dp" android:textSize="22sp" android:textColor="#fff" /> </RelativeLayout></span>
login_anim5机闭文左孀
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:background="@drawable/w05" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="\n你借可能经过过臣蝇伙圈\n跟同慌春愣讨享彼此的逝世摆" android:layout_alignParentTop="true" android:layout_marginTop="35dp" android:textSize="22sp" android:textColor="#fff" /> </RelativeLayout></span>
login_anim6机闭文左孀
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:background="@drawable/w01" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text=" \n \nOK,睹证下启的能力伴" android:layout_alignParentTop="true" android:layout_marginTop="35dp" android:textSize="22sp" android:textColor="#fff" /> <Button android:id="@+id/btn_start" android:layout_width="120dp" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="120dp" android:text="初步" android:textSize="18sp" android:textColor="#fff" android:background="@drawable/btn_style_green" android:layout_gravity="center_vertical" /> </RelativeLayout></span>
孜撬说门鲻处凶http://blog.csdn.net/hai_qing_xu_kong/article/details/42277073 感情控_