码迷,mamicode.com
首页 > 移动开发 > 详细

android ViewPager 与Fragment

时间:2016-05-31 00:55:29      阅读:351      评论:0      收藏:0      [点我收藏+]

标签:

 

ViewPager

 左右滑动数据显示

1. 整体布局

  FragmentLayout 容器包裹Fragment

技术分享
<?xml version="1.0" encoding="utf-8"?>
<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="demo.yuchen.com.ex04_extras.MainActivity">

    <!--<fragment-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="wrap_content"-->
        <!--android:name="demo.yuchen.com.ex04_extras.MyFragment"-->
        <!--android:id="@+id/fragment"-->
        <!--android:layout_alignParentTop="true"-->
        <!--android:layout_centerHorizontal="true"-->
        <!--android:layout_marginTop="136dp" />-->

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/textView2"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="添加Fragment"
        android:id="@+id/btnAdd"
        android:onClick="btnAdd"
        android:layout_below="@+id/textView2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/btnAdd"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="80dp"
        android:id="@+id/container"></FrameLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="替换"
        android:id="@+id/btnReplace"
        android:onClick="btnReplace"
        android:layout_below="@+id/btnAdd"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>
View Code

 

2.Fragment内容替换

public class MainActivity extends FragmentActivity {
    public void btnAdd(View view) {
        getSupportFragmentManager().beginTransaction()
            .add(R.id.container, new MyFragment()).commit();
    }
    public void btnReplace(View view) {
        getSupportFragmentManager().beginTransaction()
            .replace(R.id.container, new ViewPagerFragment()).commit();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

2. ViewPagerFragment

  



/**
* A simple {@link Fragment} subclass.
*/
public class ViewPagerFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View layout = inflater.inflate(R.layout.fragment_blank, container, false);
ViewPager viewPager = (ViewPager) layout.findViewById(R.id.pager);
viewPager.setAdapter(new MyPagerAdapter(getChildFragmentManager()));
return layout;
}

class MyPagerAdapter extends FragmentPagerAdapter{

List<Fragment> fragmentList = new ArrayList<>();
public MyPagerAdapter(FragmentManager fm) {
super(fm);
fragmentList.add(new PagerItemFragment());
}

@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}

@Override
public int getCount() {
return fragmentList.size();
}

}
}
 

 3. fragment_blank.xml

   

<FrameLayout 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"
    tools:context=".ViewPagerFragment">

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</FrameLayout>

 4. 

public class PagerItemFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_pager_item, container, false);
    }


}

 

android ViewPager 与Fragment

标签:

原文地址:http://www.cnblogs.com/newlangwen/p/5544324.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!