zoukankan      html  css  js  c++  java
  • 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);
        }
    
    
    }
  • 相关阅读:
    修改silverlight DataGrid当前选中行及选中列的背景色
    转 Introduction to SharePoint Feature Stapling – Part 2
    File.ReadAllText方法,File.WriteAllText方法修改文件内容
    用xlst将xml转换html
    简单在线编辑器<转>
    随机数生成
    asp.net 2.0 的代码隐藏
    Microsoft .NET Pet Shop 3.x: .NET Pet Shop 的设计模式与体系结构
    并发术语解释
    如何在Render方法中修改替换HtmlTextWriter中的内容
  • 原文地址:https://www.cnblogs.com/newlangwen/p/5544324.html
Copyright © 2011-2022 走看看