• android ViewPager 与Fragment


    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);
        }
    
    
    }
  • 相关阅读:
    C字符串处理函数
    C语言字符串函数大全
    那些闪亮的日子
    牛客网在线编程:幸运数
    牛客网在线编程:水仙花数
    [LeetCode]617.Merge Two Binary Trees
    [LeetCode]657.Judge Route Circle
    [LeetCode]141. Linked List Cycle
    五大算法:分治,贪心,动态规划,回溯,分支界定
    [LeetCode]387.First Unique Character in a String
  • 原文地址:https://www.cnblogs.com/newlangwen/p/5544324.html
Copyright © 2020-2023  润新知