该种翻页一定要到v4包中的库,不然会出错,
效果图
主UI代码
package com.xiaoke.fragment;
import java.util.ArrayList;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
//Fragment用ViewPager实现滑动翻页,该控件都应该是v4包中的
public class MainActivity extends FragmentActivity {
private ArrayList<Fragment> list=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list=new ArrayList<Fragment>();
list.add(new Yaowen());
list.add(new ShiPing());
list.add(new Tiyu());
list.add(new Yule());
ViewPager vp=(ViewPager) findViewById(R.id.viewpager_id);
MyPagerAdaper fragment=new MyPagerAdaper(this.getSupportFragmentManager());
vp.setAdapter(fragment);
}
public class MyPagerAdaper extends FragmentPagerAdapter{
public MyPagerAdaper(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
//添加进相应界面
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
}
//返回页面数
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
}
}
其他界面代码
package com.xiaoke.fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
//Fragment应该是v4包中的
public class ShiPing extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//获取自己创建的布局文件
View iv=(View) inflater.inflate(R.layout.a, null);
iv.setBackgroundColor(Color.BLUE);
return iv;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
//找到布局文件中的控件并对布局文件中的控件赋值
ImageView iv=(ImageView)view.findViewById(R.id.iv_a1_id);
TextView tv=(TextView) view.findViewById(R.id.tv_a1_id);
tv.setText("视频");
iv.setImageResource(R.drawable.ic_launcher);
}
}
主界面UI代码
<LinearLayout 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:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/viewpager_id"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
其他界面UI代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/iv_a1_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv_a1_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>