關鍵代碼:html
xml佈局:linux
<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"
tools:context=".MainActivity" >android
<android.support.v4.view.ViewPager
android:id="@+id/mViewPager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" >數組
<android.support.v4.view.PagerTitleStrip
android:id="@+id/pagertitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top" >
</android.support.v4.view.PagerTitleStrip>
</android.support.v4.view.ViewPager>ide
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="center_horizontal"
android:orientation="horizontal" >佈局
<ImageView
android:id="@+id/img1"
android:layout_width="40dp"
android:layout_height="40dp" />學習
<ImageView
android:id="@+id/img2"
android:layout_width="40dp"
android:layout_height="40dp" />this
<ImageView
android:id="@+id/img3"
android:layout_width="40dp"
android:layout_height="40dp" />
</LinearLayout>spa
</FrameLayout>.net
MainActivity:
private ViewPager mViewPager;
private PagerTitleStrip mPagerTitleStrip;
private List<ImageView> imgs;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = (ViewPager) findViewById(R.id.mViewPager);
mPagerTitleStrip = (PagerTitleStrip)findViewById(R.id.pagertitle);
//每頁顯示的內容 也能夠用LayoutInflater mLi = LayoutInflater.from(this);View view1 = mLi.inflate((R.layout.view1, null);添加布局
final List<ImageView> views = new ArrayList<ImageView>();
ImageView img = new ImageView(this); img.setImageResource(R.drawable.ic_launcher);
ImageView img2 = new ImageView(this); img2.setImageResource(R.drawable.ic_launcher);
ImageView img3 = new ImageView(this); img3.setImageResource(R.drawable.ic_launcher);
views.add(img);
views.add(img2);
views.add(img3);
//初始化導航點ImageView數組
imgs=new ArrayList<ImageView>();
ImageView i1=(ImageView)findViewById(R.id.img1);
ImageView i2=(ImageView)findViewById(R.id.img2);
ImageView i3=(ImageView)findViewById(R.id.img3);
//數組中添加數據
imgs.add(i1);
imgs.add(i2);
imgs.add(i3);
for (int j = 0; j < views.size(); j++)
{
if (j==0)
{
imgs.get(j).setBackgroundResource(R.drawable.a);
}
else {
imgs.get(j).setBackgroundResource(R.drawable.b);
}
}
/*// 每一個頁面的Title數據 添加標題方法一:
final ArrayList<String> titles = new ArrayList<String>();
titles.add("tab1");
titles.add("tab2");
titles.add("tab3");*/
//適配器
PagerAdapter mPagerAdapter = new PagerAdapter()
{
//用於判斷是否由對象生成界面
@Override
public boolean isViewFromObject(View arg0, Object arg1)
{
return arg0 == arg1;
}
//Return the number of views 總共有幾頁?
@Override
public int getCount()
{
return views.size();
}
//Remove a page for the given position從container中移除當前view
@Override
public void destroyItem(View container, int position, Object object)
{
//刪除頁面
((ViewPager) container).removeView((View) object);//等價於:((ViewPager) container).removeView(views.get(position));
}
//Returns an Object representing the new page返回一個對象表示新的一頁
@Override
public Object instantiateItem(View container, int position)
{
//添加頁面
((ViewPager) container).addView(views.get(position));
return views.get(position);
}
@Override
public CharSequence getPageTitle(int position)
{
//添加標題方法二:
switch (position)
{
case 0:
return "tab1";
case 1:
return "tab2";
case 2:
return "tab3";
default:
break;
}
//return titles.get(position); //添加標題方法一:
return null;
}
};
mViewPager.setAdapter(mPagerAdapter);
//事件處理
mViewPager.setOnPageChangeListener(new OnPageChangeListener()
{
@Override
public void onPageSelected(int position)
{
for (int i = 0; i < views.size(); i++)
{
if (i==position)//選中時
{
imgs.get(i).setBackgroundResource(R.drawable.a);
}
else {
imgs.get(i).setBackgroundResource(R.drawable.b);
}
}
}
});
}
關於自動輪播:
// 當前輪播頁
private int currentItem = 0;
final Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
mViewPager.setCurrentItem(currentItem);
}
};
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
synchronized (mViewPager) {
currentItem = (currentItem + 1) % mList.size();
handler.obtainMessage().sendToTarget();
}
}
}, 0, 5, TimeUnit.SECONDS);
參考:http://www.it165.net/pro/html/201406/16227.html
http://www.apkbus.com/android-83696-1-1.html
一個學習網:http://www.godcoder.com/
//輪播公告
for (int i = 1; i <= 5; i++) {
final int j=i;
TextView t=new TextView(this);
t.setText("優惠公告"+i);
t.setTextColor(Color.WHITE);
mFlipper.addView(t);
t.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "優惠公告 "+j, Toast.LENGTH_SHORT).show();
}
});
}
mFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_up_in));
mFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_up_out));
mFlipper.startFlipping();
http://www.cnblogs.com/ok-lanyan/archive/2012/08/02/2619701.html 寫的超好!
http://www.linuxidc.com/Linux/2012-05/60763.htm Android中利用ViewFliper實現屏幕切換效果
Android垂直滑動新特性:應用歡迎界面