Fragment侧滑翻页

该种翻页一定要到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>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值