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>
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>
相关文章推荐
- Android 简单易理解的ViewPager嵌套Fragment展示页面滑动效果
- Android使用TabLayou+fragment+viewpager实现滑动切换页面效果
- 【Android 界面效果27】利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效果
- Android 利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效果
- Android 利用ViewPager实现底部圆点导航左右滑动效果以及Fragment页面切换
- 【Android 界面效果27】利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效果
- Fragment嵌套ViewPager加上RadioButton实现微信页面滑动点击效果
- Android 利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效果
- TabLayout 和ViewPager和Fragment的多页面滑动(主要实现一个水平的布局用来展示Tabs加上ViewPager实现联动效果)
- Android 利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效果(转载)
- Android 利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效果
- Android ViewPager + Fragment实现滑动页面效果
- Android 利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效果
- Android 利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效果
- Android开发之ViewPager结合Fragment实现滑动页面的效果(源代码分享)
- android之ViewPager简单实现局部页面滑动效果
- Android主页面 多层Fragment嵌套(Fragment+ViewPager)滑动
- Android 利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效果(牛逼)
- Android ViewPager多页面滑动切换以及动画效果
- Android中用ViewPager实现多页面滑动切换及动画效果的实例