• Android Fragment分页显示的实现


    分页显示有两种方式 一种是使用ViewPager 另一种是使用FragmentTransaction

    上代码

    1 FragmentTransaction实现方式

    public class MainActivity extends FragmentActivity implements View.OnClickListener {
    
      private AllPhotosFragment allPhotosFragment;
      private MyAlbumFragment myAlbumFragment;
      private ShareTimeFragment shareTimeFragment;
      private ShotTimeFragment shotTimeFragment;
      private ThemeAlbumFragment themeAlbumFragment;
      private PersonalFragment personalFragment;
    
      private ImageView test1;
      private ImageView test2;
      private ImageView test3;
      
      private FragmentManager fragmentManager;
      
      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
        setContentView(R.layout.activity_main);
    
        test1 = (ImageView) findViewById(R.id.test1);
        test2 = (ImageView) findViewById(R.id.test2);
        test3 = (ImageView) findViewById(R.id.test3);
        test1.setOnClickListener(this);
        test2.setOnClickListener(this);
        test3.setOnClickListener(this);
        
        fragmentManager = getSupportFragmentManager();
    
      }
    
    
    
      @Override
      public void onClick(View v) {
        
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideFragments(transaction);
    
        switch (v.getId()) {
          case R.id.test1:
            if (allPhotosFragment == null) {
              allPhotosFragment = new AllPhotosFragment();
              transaction.add(R.id.id_content, allPhotosFragment);
            }else {
              transaction.show(allPhotosFragment);
            }
            break;
          case R.id.test2:
            if (shareTimeFragment == null) {
              shareTimeFragment = new ShareTimeFragment();
              transaction.add(R.id.id_content, shareTimeFragment);
            }else {
              transaction.show(shareTimeFragment);
            }
            break;
          case R.id.test3:
            if (personalFragment == null) {
              personalFragment = new PersonalFragment();
              transaction.add(R.id.id_content, personalFragment);
            }else {
              transaction.show(personalFragment);
            }
            break;
        }
        
        transaction.commit();
    
      }
      
      private void hideFragments(FragmentTransaction transaction) {
        if (allPhotosFragment != null) {
          transaction.hide(allPhotosFragment);
        }
        if (shareTimeFragment != null) {
          transaction.hide(shareTimeFragment);
        }
        if (personalFragment != null) {
          transaction.hide(personalFragment);
        }
      }
      
      
    }
    <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"
        tools:context="com.example.testfragment.MainActivity" >
    
        <FrameLayout
            android:id="@+id/id_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#FFFFFF" />
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:layout_alignParentTop="true"
            android:background="#123456" >
        </RelativeLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:layout_alignParentBottom="true"
            android:background="#123456"
            android:gravity="center_vertical"
            android:orientation="horizontal" >
    
            <ImageView
                android:id="@+id/test1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:src="@drawable/test1" />
    
            <ImageView
                android:id="@+id/test2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:src="@drawable/test2" />
    
            <ImageView
                android:id="@+id/test3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:src="@drawable/test3" />
        </LinearLayout>
    
    </RelativeLayout>
    public class PersonalFragment extends Fragment{
      
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        
        View view = inflater.inflate(R.layout.fragment_test, container, false);
        TextView textView = (TextView) view.findViewById(R.id.tv);
        textView.setText("PersonalFragment");
        return view;
      }
      
    }

    Code见https://github.com/huanyi0723/TestFragmentTransaction

    2 FragmentPagerAdapter实现方式

    public class MainActivity extends FragmentActivity {
      
      private ArrayList<Fragment> fragments;
      private ViewPager viewPager;
      private MyViewPagerAdapter myViewPagerAdapter;
      
      private AllPhotosFragment allPhotosFragment;
      private MyAlbumFragment myAlbumFragment;
      private ShareTimeFragment shareTimeFragment;
      private ShotTimeFragment shotTimeFragment;
      private ThemeAlbumFragment themeAlbumFragment;
      private PersonalFragment personalFragment;
      
    
      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
        setContentView(R.layout.activity_main);
        
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        
        fragments = new ArrayList<Fragment>();
        allPhotosFragment = new AllPhotosFragment();
        myAlbumFragment = new MyAlbumFragment();
        shareTimeFragment = new ShareTimeFragment();
        shotTimeFragment = new ShotTimeFragment();
        themeAlbumFragment = new ThemeAlbumFragment();
        personalFragment = new PersonalFragment();
        fragments.add(allPhotosFragment);
        fragments.add(myAlbumFragment);
        fragments.add(shareTimeFragment);
        fragments.add(shotTimeFragment);
        fragments.add(themeAlbumFragment);
        fragments.add(personalFragment);
        myViewPagerAdapter = new MyViewPagerAdapter(getSupportFragmentManager(), fragments);
        
        viewPager.setAdapter(myViewPagerAdapter);
        viewPager.setCurrentItem(0);
      }
      
    }
    <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"
        tools:context="com.example.testfragment.MainActivity" >
    
        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#FFFFFF" />
        
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:layout_alignParentTop="true"
            android:background="#123456" >
        </RelativeLayout>
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:layout_alignParentBottom="true"
            android:background="#123456" >
        </RelativeLayout>
    
    </RelativeLayout>
    public class MyViewPagerAdapter extends FragmentPagerAdapter{
      
      private ArrayList<Fragment> fragments;
    
      public MyViewPagerAdapter(FragmentManager fm, ArrayList<Fragment> fragments) {
        super(fm);
        this.fragments = fragments;
      }
    
      @Override
      public Fragment getItem(int arg0) {
        return fragments.get(arg0);
      }
    
      @Override
      public int getCount() {
        return fragments.size();
      }
      
      
    }
    public class AllPhotosFragment extends Fragment {
    
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_test, container, false);
        TextView textView = (TextView) view.findViewById(R.id.tv);
        textView.setText("AllPhotosFragment");
        return view;
      }
      
    }

    Code见https://github.com/huanyi0723/TestFragmentPagerAdapter/

  • 相关阅读:
    Sql 复习(1)
    记录一次git issue
    JWT自校验框架
    分布式事务分布式锁的常用解决方式
    SpringBoot开发文档
    SpringCloud的使用以及五大核心组件
    SpringMVC
    关于开发中使用AOP的坑
    SpringCloud使用feign远程调用服务注入映射接口失败问题
    springBoot使用Restful跳转路径回显异常问题
  • 原文地址:https://www.cnblogs.com/huanyi0723/p/5233769.html
Copyright © 2020-2023  润新知