Android Fragment簡單切換用法

目的:創建5個左右滑動的fragment,實現各自功能

效果:

實現:

  1. 創建類FragmentAdapter繼承FragmentPagerAdapter:
    package denghuashan.filetransfer;
    
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentPagerAdapter;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * Created by mountain_hua on 2019/1/18.
     */
    
    public class FragmentAdapter extends FragmentPagerAdapter {
    
        //創建一個Fragment的list
        List<Fragment> fragmentList = new ArrayList<Fragment>();
    
    
        public FragmentAdapter(FragmentManager fragmentManager, List<Fragment> fragmentList) {
            super(fragmentManager);
            this.fragmentList = fragmentList;
        }
    
        @Override
        //通過position設置對應的Fragment,
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }
    
        @Override
        //fragment集合的參數長度
        public int getCount() {
            return fragmentList.size();
        }
    
    }
    

     

  2. 創建5個fragment及對應的xml,如下:
    fragment:
    public class Fragment02 extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            //引用創建好的xml佈局
            View view = inflater.inflate(R.layout.fragment02,container,false);
            return view;
        }
    }

    xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/img02"
        tools:context=".ToSend">
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="圖片" />
    </LinearLayout>

     

  3. 在activity中引用fragment,並實現滑動效果:
    activity:
    /**
     * Created by mountain_hua on 2019/1/18.
     */
    
    public class ToSend extends FragmentActivity implements View.OnClickListener {
        private ViewPager viewPager;
        private List<Fragment> mFragmentList = new ArrayList<Fragment>();
        private FragmentAdapter mFragmentAdapter;
        //5個fragment
        private Fragment01 fragment01;
        private Fragment02 fragment02;
        private Fragment03 fragment03;
        private Fragment04 fragment04;
        private Fragment05 fragment05;
    
    
        private TextView textView01,textView02,textView03,textView04,textView05;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.tosend);
            setView();
    
    
        }
    
        //初始化view
        private void setView() {
            textView01= (TextView) findViewById(R.id.textView01);
            textView01.setOnClickListener(this);
            textView02= (TextView) findViewById(R.id.textView02);
            textView02.setOnClickListener(this);
            textView03= (TextView) findViewById(R.id.textView03);
            textView03.setOnClickListener(this);
            textView04= (TextView) findViewById(R.id.textView04);
            textView04.setOnClickListener(this);
            textView05= (TextView) findViewById(R.id.textView05);
            textView05.setOnClickListener(this);
    
            fragment01 = new Fragment01();
            fragment02 = new Fragment02();
            fragment03 = new Fragment03();
            fragment04 = new Fragment04();
            fragment05 = new Fragment05();
            mFragmentList.add(fragment01);
            mFragmentList.add(fragment02);
            mFragmentList.add(fragment03);
            mFragmentList.add(fragment04);
            mFragmentList.add(fragment05);
    
            mFragmentAdapter = new FragmentAdapter(this.getSupportFragmentManager(), mFragmentList);
            //初始化viewPager
            viewPager = (ViewPager)findViewById(R.id.viewp_01);
    
            //添加OnPageChangeListener的API
            viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                //頁面在滑動過程中不停觸發該方法:position:當前滑動到的位置,positionOffset:偏移量的百分比,positionOffsetPixels:偏移量的數值
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                }
                @Override
                //ViewPager跳轉到新頁面時觸發該方法,position表示新頁面的位置。
                public void onPageSelected(int position) {
                    switch (position){
                        case 0:
                            textView01.setTextColor(android.graphics.Color.parseColor("#ffffff"));
                            textView02.setTextColor(android.graphics.Color.parseColor("#ff0000"));
                            textView03.setTextColor(android.graphics.Color.parseColor("#ff0000"));
                            textView04.setTextColor(android.graphics.Color.parseColor("#ff0000"));
                            textView05.setTextColor(android.graphics.Color.parseColor("#ff0000"));
    
                            textView01.setBackgroundColor(android.graphics.Color.parseColor("#FF4040"));
                            textView02.setBackgroundColor(android.graphics.Color.parseColor("#ffffff"));
                            textView03.setBackgroundColor(android.graphics.Color.parseColor("#ffffff"));
                            textView04.setBackgroundColor(android.graphics.Color.parseColor("#ffffff"));
                            textView05.setBackgroundColor(android.graphics.Color.parseColor("#ffffff"));
    
                            break;
    
                        case 1:
                            textView01.setTextColor(android.graphics.Color.parseColor("#ff0000"));
                            textView02.setTextColor(android.graphics.Color.parseColor("#ffffff"));
                            textView03.setTextColor(android.graphics.Color.parseColor("#ff0000"));
                            textView04.setTextColor(android.graphics.Color.parseColor("#ff0000"));
                            textView05.setTextColor(android.graphics.Color.parseColor("#ff0000"));
    
                            textView01.setBackgroundColor(android.graphics.Color.parseColor("#ffffff"));
                            textView02.setBackgroundColor(android.graphics.Color.parseColor("#FF4040"));
                            textView03.setBackgroundColor(android.graphics.Color.parseColor("#ffffff"));
                            textView04.setBackgroundColor(android.graphics.Color.parseColor("#ffffff"));
                            textView05.setBackgroundColor(android.graphics.Color.parseColor("#ffffff"));
    
                            break;
    
                        case 2:
                            textView01.setTextColor(android.graphics.Color.parseColor("#ff0000"));
                            textView02.setTextColor(android.graphics.Color.parseColor("#ff0000"));
                            textView03.setTextColor(android.graphics.Color.parseColor("#ffffff"));
                            textView04.setTextColor(android.graphics.Color.parseColor("#ff0000"));
                            textView05.setTextColor(android.graphics.Color.parseColor("#ff0000"));
    
                            textView01.setBackgroundColor(android.graphics.Color.parseColor("#ffffff"));
                            textView02.setBackgroundColor(android.graphics.Color.parseColor("#ffffff"));
                            textView03.setBackgroundColor(android.graphics.Color.parseColor("#FF4040"));
                            textView04.setBackgroundColor(android.graphics.Color.parseColor("#ffffff"));
                            textView05.setBackgroundColor(android.graphics.Color.parseColor("#ffffff"));
    
                            break;
                        case 3:
                            textView01.setTextColor(android.graphics.Color.parseColor("#ff0000"));
                            textView02.setTextColor(android.graphics.Color.parseColor("#ff0000"));
                            textView03.setTextColor(android.graphics.Color.parseColor("#ff0000"));
                            textView04.setTextColor(android.graphics.Color.parseColor("#ffffff"));
                            textView05.setTextColor(android.graphics.Color.parseColor("#ff0000"));
    
                            textView01.setBackgroundColor(android.graphics.Color.parseColor("#ffffff"));
                            textView02.setBackgroundColor(android.graphics.Color.parseColor("#ffffff"));
                            textView03.setBackgroundColor(android.graphics.Color.parseColor("#ffffff"));
                            textView04.setBackgroundColor(android.graphics.Color.parseColor("#FF4040"));
                            textView05.setBackgroundColor(android.graphics.Color.parseColor("#ffffff"));
    
                            break;
                        case 4:
                            textView01.setTextColor(android.graphics.Color.parseColor("#ff0000"));
                            textView02.setTextColor(android.graphics.Color.parseColor("#ff0000"));
                            textView03.setTextColor(android.graphics.Color.parseColor("#ff0000"));
                            textView04.setTextColor(android.graphics.Color.parseColor("#ff0000"));
                            textView05.setTextColor(android.graphics.Color.parseColor("#ffffff"));
    
                            textView01.setBackgroundColor(android.graphics.Color.parseColor("#ffffff"));
                            textView02.setBackgroundColor(android.graphics.Color.parseColor("#ffffff"));
                            textView03.setBackgroundColor(android.graphics.Color.parseColor("#ffffff"));
                            textView04.setBackgroundColor(android.graphics.Color.parseColor("#ffffff"));
                            textView05.setBackgroundColor(android.graphics.Color.parseColor("#FF4040"));
    
                            break;
                    }
                }
                @Override
                //當頁面的滑動狀態改變時該方法會被觸發,頁面的滑動狀態有3個:“0”表示什麼都不做,“1”表示開始滑動,“2”表示結束滑動。
                public void onPageScrollStateChanged(int state) {
                }
            });
    
            viewPager.setAdapter(mFragmentAdapter);
        }
    
    
        //導航欄點擊
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.textView01:
                    //綁定viewopager對應的頁卡
                    viewPager.setCurrentItem(0);
                    break;
                case R.id.textView02:
                    viewPager.setCurrentItem(1);
                    break;
                case R.id.textView03:
                    viewPager.setCurrentItem(2);
                    break;
                case R.id.textView04:
                    viewPager.setCurrentItem(3);
                    break;
                case R.id.textView05:
                    viewPager.setCurrentItem(4);
                    break;
            }
        }
    }

    layout:
     

    <?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:orientation="vertical"
        android:background="#E5E5E5">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:orientation="horizontal">
    
            <TextView
                android:id="@+id/textView01"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="1"
                android:background="#FF4040"
                android:gravity="center"
                android:text="文件"
                android:textColor="#EEE685" />
    
            <View
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="#EEDFCC">
            </View>
    
            <TextView
                android:id="@+id/textView02"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="圖片"
                android:textColor="#ff0000" />
            <View
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="#EEDFCC">
            </View>
    
            <TextView
                android:id="@+id/textView03"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="音樂"
                android:textColor="#ff0000" />
            <View
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="#EEDFCC">
            </View>
    
            <TextView
                android:id="@+id/textView04"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="視頻"
                android:textColor="#ff0000" />
            <View
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="#EEDFCC">
            </View>
    
            <TextView
                android:id="@+id/textView05"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="應用"
                android:textColor="#ff0000" />
    
        </LinearLayout>
    
    
        <android.support.v4.view.ViewPager
            android:id="@+id/viewp_01"
            android:layout_width="match_parent"
            android:layout_height="match_parent"></android.support.v4.view.ViewPager>
    </LinearLayout>
    

     

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章