1.1android——ui界面之ViewPager控件实现侧面滑动

[color=green]ViewPager控件实现侧面滑动[/color]
要点:
1、因为是用ViewPager控件实现页面的侧面滑动,所以应该先写几个布局文件;分别命名为tab1,tab2,tab3.
2、ViewPager控件必须要写相关的适配器(PagerAdapter),并重写里边的方法。(关键步骤,当然,方法重写不同,效果也不同)
项目:
Xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
   
  
<android.support.v4.view.ViewPager
    android:id="@+id/viewPager1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"  >
    <android.support.v4.view.PagerTitleStrip
        android:id="@+id/pagerTitle1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
       
    </android.support.v4.view.PagerTitleStrip>
</android.support.v4.view.ViewPager>

</LinearLayout>
注意另外写几个布局文件。

[color=green]MainActivity:
[/color]

package com.example.viewpager;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
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.View;
import android.view.ViewGroup;

public class MainActivity extends Activity {
private ViewPager viewPager;
private PagerTitleStrip pagerTitle;
// List集合
private List<View> view; // 用于存储布局文件
private List<String> pageTitle;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) this.findViewById(R.id.viewPager1);
pagerTitle = (PagerTitleStrip) this.findViewById(R.id.pagerTitle1);
// 动态加载布局(用于把其他布局加载到本布局进行处理)
View view1 = LayoutInflater.from(MainActivity.this).inflate(
R.layout.tab1, null);
View view2 = LayoutInflater.from(MainActivity.this).inflate(
R.layout.tab2, null);
View view3 = LayoutInflater.from(MainActivity.this).inflate(
R.layout.tab3, null);
// 将布局添加到list中
view = new ArrayList<View>();
pageTitle = new ArrayList<String>();
view.add(view1);
view.add(view2);
view.add(view3);
pageTitle.add("title1");
pageTitle.add("title2");
pageTitle.add("title3");

// 为ViewPager添加一个适配器
PagerAdapter pagerAdapter = new PagerAdapter() {

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
// 根据传来的key,找到view,判断与传来的参数View arg0是不是同一个视图
return arg0 == view
.get((int) Integer.parseInt(arg1.toString()));
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return view.size();
}

@Override
public void destroyItem(ViewGroup container, int position,
Object object) {
// TODO Auto-generated method stub
container.removeView(view.get(position));
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
container.addView(view.get(position));

// 把当前新增视图的位置(position)作为Key传过去
return position;
}
};

viewPager.setAdapter(pagerAdapter);
}

}

你可能感兴趣的:(android,UI界面)