您的位置:首页 > 移动开发 > Android开发

Android 简单易懂的ViewPager嵌套Fragment展示页面滑动效果

2017-06-13 16:11 483 查看
可以说这个控件在现在的app当中经常用到,简单实用,就是fragment与viewpager结合,很简单,具体的都不介绍了。

MainActivity

package com.example.fragment_demo;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
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.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends FragmentActivity implements OnClickListener {
private FragmentHome home;
private FragmentMy my;
private Button mBtn1,mBtn2;
private LinearLayout mline_fragment;
private FragmentManager manager;
private MyAdapter adapter;
private List<Fragment> list;
private ViewPager mVp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
my=new FragmentMy();
home=new FragmentHome();
mBtn1=(Button) findViewById(R.id.mBtn1);
mBtn2=(Button) findViewById(R.id.mBtn2);
mVp=(ViewPager) findViewById(R.id.mLine_Fragment);
mBtn1.setOnClickListener(this);
mBtn2.setOnClickListener(this);
list=new ArrayList<Fragment>();
list.add(my);
list.add(home);
adapter=new MyAdapter(getSupportFragmentManager(), list);
mVp.setAdapter(adapter);
}
@Override
public void onClick(View v) {
int ID = v.getId();
switch (ID) {
case R.id.mBtn1:
//获取焦点下标
mVp.setCurrentItem(0, false);
break;

case R.id.mBtn2:
mVp.setCurrentItem(1,false);
break;
}

}
}

MyAdapter

package com.example.fragment_demo;
import java.util.ArrayList;
import java.util.List;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class MyAdapter extends FragmentPagerAdapter{
private List<Fragment> list=new ArrayList<Fragment>();
private FragmentManager manager;

public MyAdapter(FragmentManager fm,List<Fragment> list) {
super(fm);
this.list=list;
// 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();
}

}

FragmentHome

package com.example.fragment_demo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentHome extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragmenthome, null);
return view;
}
}

FragmentMy

package com.example.fragment_demo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentMy extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragmentmy, null);
return view;
}
}

//以下是xml文件:
activity_main
<RelativeLayout 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: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="com.example.fragment_demo.MainActivity" >

    <LinearLayout
        android:id="@+id/mLine"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal" >
        <Button
            android:id="@+id/mBtn1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="fragmenthome" />
        <Button
            android:id="@+id/mBtn2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="fragmentmy" />
    </LinearLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/mLine_Fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/mLine"
        android:orientation="vertical" />
  
</RelativeLayout>

fragmenthome
<?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" >
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是 1"
        />

</LinearLayout>

fragmentmy
<?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" >
        <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是 2"
        
        />
</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐