Android 專案首頁的Fragment切換例項(一)
阿新 • • 發佈:2019-02-02
一、首頁的Activity的佈局:
<?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:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent"> <RadioGroupandroid:id="@+id/rg_bottom" android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentBottom="true" android:orientation="horizontal"> <RadioButton android:id="@+id/rb_one" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:button="@null" android:checked="true" android:gravity="center_horizontal|center_vertical" android:text="第一" android:textColor="@drawable/slt_rb_text_color" /> <RadioButton android:id="@+id/rb_two" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:button="@null" android:gravity="center_horizontal|center_vertical" android:text="第二" android:textColor="@drawable/slt_rb_text_color" /> <RadioButton android:id="@+id/rb_three" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:button="@null" android:gravity="center_horizontal|center_vertical" android:text="第三" android:textColor="@drawable/slt_rb_text_color" /> <RadioButton android:id="@+id/rb_four" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:button="@null" android:gravity="center_horizontal|center_vertical" android:text="第四" android:textColor="@drawable/slt_rb_text_color" /> <RadioButton android:id="@+id/rb_five" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:button="@null" android:gravity="center_horizontal|center_vertical" android:text="第五" android:textColor="@drawable/slt_rb_text_color" /> </RadioGroup> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_above="@+id/rg_bottom" android:background="#AFAFAF" /> <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/rg_bottom" android:layout_marginTop="1dp" /> </RelativeLayout>
程式碼中使用到的資原始檔slt_rb_text_color的程式碼內容是:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="#000000" android:state_checked="true"/> <item android:color="#f0f" android:state_checked="false"/> </selector>
二、再Activity中進行控制元件的初始化和頁面的集合等等的一些操作:
package com.switchfragment; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.RadioButton; import com.switchfragment.adapter.MyFragmentPagerAdapter; import com.switchfragment.fragment.CustomListFragment; import com.switchfragment.fragment.Fragment1; import com.switchfragment.fragment.Fragment2; import com.switchfragment.fragment.Fragment3; import com.switchfragment.fragment.Fragment4; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener, View.OnClickListener { private ViewPager pager; private List<Fragment> fragList; private RadioButton rbOne; private RadioButton rbTwo; private RadioButton rbThree; private RadioButton rbFour; private RadioButton rbFive; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } //初始化控制元件、資料 private void init(){ rbOne = (RadioButton) findViewById(R.id.rb_one); rbOne.setOnClickListener(this); rbTwo = (RadioButton) findViewById(R.id.rb_two); rbTwo.setOnClickListener(this); rbThree = (RadioButton) findViewById(R.id.rb_three); rbThree.setOnClickListener(this); rbFour = (RadioButton) findViewById(R.id.rb_four); rbFour.setOnClickListener(this); rbFive = (RadioButton) findViewById(R.id.rb_five); rbFive.setOnClickListener(this); pager = (ViewPager) findViewById(R.id.pager); fragList = new ArrayList<>(); fragList.add(new Fragment1()); fragList.add(new Fragment2()); fragList.add(new Fragment3()); fragList.add(new Fragment4()); fragList.add(new CustomListFragment()); /** * 使用getSupportFragmentManager()該Activity必須繼承FragmentActivity */ MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragList); pager.setAdapter(adapter); pager.setOnPageChangeListener(this); pager.setOffscreenPageLimit(1); } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { switch (position) { case 0: rbOne.setChecked(true); break; case 1: rbTwo.setChecked(true); break; case 2: rbThree.setChecked(true); break; case 3: rbFour.setChecked(true); break; case 4: rbFive.setChecked(true); break; } } @Override public void onPageScrollStateChanged(int state) { } @Override public void onClick(View view) { switch (view.getId()) { case R.id.rb_one: pager.setCurrentItem(0); break; case R.id.rb_two: pager.setCurrentItem(1); break; case R.id.rb_three: pager.setCurrentItem(2); break; case R.id.rb_four: pager.setCurrentItem(3); break; case R.id.rb_five: pager.setCurrentItem(4); break; } } }
三、其中的介面卡的MyFragmentPagerAdapter的程式碼是:
package com.switchfragment.adapter; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentStatePagerAdapter; import java.util.List; public class MyFragmentPagerAdapter extends FragmentStatePagerAdapter { private List<Fragment> fragList; public MyFragmentPagerAdapter(FragmentManager fm, List<Fragment> fragList) { super(fm); this.fragList = fragList; } @Override public Fragment getItem(int position) { return fragList.get(position); } @Override public int getCount() { return fragList.size(); } }
四、五個Fragment頁面以及基類BaseFragment如下:
BaseFragment:
package com.switchfragment.fragment; import android.support.v4.app.Fragment; /** * Created by think on 2017/3/28. */ public class BaseFragment extends Fragment { /** * 當前介面是否呈現給使用者的狀態標誌 */ protected boolean isVisible; /** * 重寫Fragment父類生命週期方法,在onCreate之前呼叫該方法,實現Fragment資料的緩載入. * @param isVisibleToUser 當前是否已將介面顯示給使用者的狀態 */ @Override public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); if(getUserVisibleHint()) { isVisible = true; onVisible(); } else { isVisible = false; onInvisible(); } } /** * 當介面呈現給使用者,即設定可見時執行,進行載入資料的方法 * 在使用者可見時載入資料,而不在使用者不可見的時候載入資料,是為了防止控制元件物件出現空指標異常 */ protected void onVisible(){ setlazyLoad(); } /** * 當介面還沒呈現給使用者,即設定不可見時執行 */ protected void onInvisible(){ } /** * 載入資料方法 */ protected void setlazyLoad(){ } }
Fragment1,Fragment2,Fragment3,Fragment4,Fragment5的頁面程式碼相同:
public class Fragment1 extends BaseFragment { private Context context; private TextView tv1; private boolean isPrepared; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.view1, container, false); context = getActivity(); initView(view); isPrepared = true; setlazyLoad(); return view; } private void initView(View view) { tv1 = (TextView) view.findViewById(R.id.tv_1); } @Override protected void setlazyLoad() { super.setlazyLoad(); if (!isPrepared || !isVisible) { return; } /** * 在這裡進行的介面的訪問和控制元件的適配。 */ tv1.setText("第一頁"); } }
其中的五個Fragment的佈局檔案都省略了。