ViewPager
MainActivity 类
package org.aking86.armyknife; import java.text.DateFormat; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.BatteryManager; import android.os.Bundle; import android.os.PowerManager; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { ViewPager mViewPager; MainPagerAdapter pagerAdapter; LayoutInflater mInflater; List<View> mListViews; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initPagerView(); } private void initPagerView() { mViewPager = (ViewPager) findViewById(R.id.viewpager_main); mListViews = new ArrayList<View>(); mInflater = this.getLayoutInflater(); mListViews.add(mInflater.inflate(R.layout.ly_me1, null)); mListViews.add(mInflater.inflate(R.layout.ly_me2, null)); mListViews.add(mInflater.inflate(R.layout.ly_szdt, null)); mListViews.add(mInflater.inflate(R.layout.ly5, null)); mListViews.add(mInflater.inflate(R.layout.ly6, null)); pagerAdapter = new MainPagerAdapter(mListViews); mViewPager.setAdapter(pagerAdapter); } void init() { mFilter = new IntentFilter(); mFilter.addAction(Intent.ACTION_BATTERY_CHANGED); mFilter.addAction(Intent.ACTION_BATTERY_LOW); mFilter.addAction(Intent.ACTION_BATTERY_OKAY); mFilter.addAction(Intent.ACTION_POWER_CONNECTED); } @Override protected void onStart() { super.onStart(); getBatteryLevel(); } @Override protected void onStop() { super.onStop(); } @Override protected void onDestroy() { super.onDestroy(); android.os.Process.killProcess(android.os.Process.myPid()); } }
PagerAdapter 类
package org.aking86.armyknife;
import java.util.List;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
public class MainPagerAdapter extends PagerAdapter {
List<View> mListViews;
public MainPagerAdapter(List<View> mListViews) {
super();
this.mListViews = mListViews;
}
@Override
public int getCount() {
return mListViews.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(mListViews.get(position), 0);
// return super.instantiateItem(container, position);
return mListViews.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(mListViews.get(position));
// super.destroyItem(container, position, object);
}
}
import java.util.List;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
public class MainPagerAdapter extends PagerAdapter {
List<View> mListViews;
public MainPagerAdapter(List<View> mListViews) {
super();
this.mListViews = mListViews;
}
@Override
public int getCount() {
return mListViews.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(mListViews.get(position), 0);
// return super.instantiateItem(container, position);
return mListViews.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(mListViews.get(position));
// super.destroyItem(container, position, object);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txtTitle"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<!-- 分页 -->
<android.support.v4.view.ViewPager
android:id="@+id/viewpager_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txtTitle"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<!-- 分页 -->
<android.support.v4.view.ViewPager
android:id="@+id/viewpager_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
ly_me1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:contentDescription="@string/desc"
android:src="@drawable/me_v3" />
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:contentDescription="@string/desc"
android:src="@drawable/me_v3" />
</RelativeLayout>
相关推荐
在Android开发中,ViewPager是一种非常常用的组件,它允许用户通过左右滑动来浏览多个页面,常用于实现类似轮播图或者Tab切换的效果。而ViewPager指示器则是与ViewPager配合使用,用以显示当前所选页面的状态,通常...
在Android开发中,ViewPager是一个非常实用的基础组件,它允许用户通过左右滑动在多个界面之间切换,通常用于引导页、图片浏览、页面切换等场景。随着社交媒体应用的流行,如微信朋友圈的图片和视频轮播功能受到了...
在Android开发中,ViewPager是一个非常重要的组件,常用于实现页面滑动切换的效果,例如在应用的引导页、图片轮播或者Tab布局中。本篇将深入讲解如何在ViewPager中实现各种自定义滑动动画,以及如何优化性能并保持...
在Android开发中,ViewPager是一个非常常用的组件,它允许用户通过左右滑动来浏览多个页面,通常用于实现Tab切换或者图片轮播等效果。然而,在实际使用过程中,有时默认的滑动灵敏度可能并不符合我们的需求,例如,...
在Android开发中,ViewPager是一个非常常用的组件,它用于展示多个页面并允许用户通过滑动来切换页面。在本文中,我们将深入探讨ViewPager的切换动画,特别是标题提到的“三种ViewPager切换动画”。首先,我们理解...
标题中的“仿ViewPager”指的是开发者基于Android平台创建的一个自定义视图组件,它模仿了原生ViewPager的功能,但可能在某些方面进行了定制或优化,以满足特定需求。事件分发应用则表明这个组件着重处理触摸事件的...