Fragment 를 사용 하여 구현 되 는 슬라이딩 구성 요소 ViewPager


  • 어댑터 Fragment PagerAdapter 의 실현
  • 두 개의 Fragment 류
  • OrderFragmentjava
  • fragment_orderxml

  • MeFragmentjava
  • fragment_mexml



  • 주 활동 실현
  • activity_mainxml





  • 1 어댑터 Fragment PagerAdapter 의 실현
    Fragment PagerAdapter 의 파생 클래스 에 대해 서 는 getItem(int)과 getCount()를 다시 쓰 면 됩 니 다.
    public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
        private List<Fragment> list;  
        public MyFragmentPagerAdapter(FragmentManager fm, List<Fragment> list) {  
            super(fm);  
            this.list = list;
        }
        //         View    
        @Override  
        public int getCount() {  
            return list.size();  
        }  
        // getItem(int arg0) ,       arg0,         fragment
        @Override  
        public Fragment getItem(int arg0) {  
            return list.get(arg0);  
        }
    }

    2.두 개의 Fragment 류
    OrderFragment.java
    public class OrderFragment extends Fragment {
        // onCreateView()       View
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            View view= inflater.inflate(R.layout.fragment_order, container, false);
            //View        
            refresh = view.findViewById(R.id.order_count_refresh);
            refresh.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    //    
                    countStatus();
                }
            });
            return view;
        }
    }

    fragment_order.xml
    <?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:background="#ffffff" android:orientation="vertical" >
    
        <Button android:id="@+id/fragment1_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="show toast" />
    </LinearLayout>

    MeFragment.java
    public class MeFragment extends Fragment {
        // onCreateView()       View
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            View view= inflater.inflate(R.layout.fragment_me, container, false);
            //View        
            login = view.findViewById(R.id.me_login);
            login.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(MeFragment.this.getActivity(),
                            LoginActivity.class));
                }
            });
            return view;
        }
    }

    fragment_me.xml
    <?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:background="#ffff00" android:orientation="vertical" >
    
    
    </LinearLayout>

    3 주 activity 실현
    public class MainActivity extends FragmentActivity {
        private ViewPager mPager;
        private List<Fragment> fragmentList;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mPager = (ViewPager) findViewById(R.id.viewpager);
            //   fragment
            List<Fragment> fragmentList=new ArrayList<Fragment>();
            fragmentList.add(new OrderFragment());
            fragmentList.add(new MeFragment());
            FragAdapter adapter = new FragAdapter(getSupportFragmentManager(), fragments);
    
            // ViewPager     
            mPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentList));
            mPager.setCurrentItem(0);//              
            //MyOnPageChangeListener()        
            mPager.setOnPageChangeListener(new MyOnPageChangeListener());
            radioGroup = (RadioGroup) findViewById(R.id.main_tab);
            // radioGroup     
            radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    if(checkedId == R.id.tab_me){
                        mPager.setCurrentItem(INDEX_ME);
                    }else if(checkedId == R.id.tab_shoujian){
                        mPager.setCurrentItem(INDEX_SHOUJIAN);
                    }
                }
            });
        }
        //         OnPageChangeListener
        public class MyOnPageChangeListener implements OnPageChangeListener {
            @Override
            //               ,        ,          。
            /** * arg0:    ,          * arg1:           *arg2:            */
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub
            }
    
            @Override 
            //            
              /** * arg0 * 1:       * 2:       * 0:       ,      */  
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub
            }
    
            @Override
            //           
            /** * arg0             Position(    )。 */  
            public void onPageSelected(int arg0) {
                // TODO Auto-generated method stub
                switch (arg0) {
                case INDEX_ME:
                    radioGroup.check(R.id.tab_me);
                    break;
                case INDEX_SHOUJIAN:
                    radioGroup.check(R.id.tab_shoujian);
                    break;
                }
            }
        }
    
    }

    activity_main.xml
    <?xml version="1.0" encoding="utf-8"?>  
      <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">  
    
      <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_alignParentTop="true" android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_gravity="center" android:layout_marginBottom="60dip" android:flipInterval="30" android:persistentDrawingCache="animation"/> 
    
        <RadioGroup android:id="@+id/main_tab" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_alignParentBottom="true" android:layout_height="60dip">  
    
            <RadioButton  style="@style/main_tab" android:id="@+id/tab_shoujian" <!--        -->
                android:drawableTop="@drawable/tab_shoujian_selector"
                android:layout_height="match_parent"
                android:checked="true" 
                android:text="****" /> 
            <RadioButton  style="@style/main_tab" android:id="@+id/tab_me" android:drawableTop="@drawable/tab_me_selector" android:layout_height="match_parent" android:text="*" />  
        </RadioGroup>  
    </RelativeLayout> 

    좋은 웹페이지 즐겨찾기