1. 程式人生 > >Fragment巢狀Fragment和Viewpager

Fragment巢狀Fragment和Viewpager

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioGroup;

import java.util.ArrayList;
import java.util.List;

import 
l.framgsradiogroupviewpager.Framgs.framg1; import l.framgsradiogroupviewpager.Framgs.framg2; import l.framgsradiogroupviewpager.Framgs.framg3; //首先建立fragment在MainActivity中和RadioGroup一起使用,之後再第一個Fragment中 佈局ViewPaegr再ViewPager總新增Fragment,在佈局ListView新增介面卡即可, //fragment中巢狀子fragment要用getChildFragmentManager()
public class MainActivity extends AppCompatActivity { private RadioGroup rg; private android.support.v4.app.FragmentManager supportFragmentManager; private android.support.v4.app.FragmentTransaction fragmentTransaction; private framg1 f1; private framg2 f2; private framg3 f3
; private List<Fragment> list = new ArrayList<Fragment>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //獲取控制元件 rg = (RadioGroup) findViewById(R.id.rg); //獲取管理者 supportFragmentManager = getSupportFragmentManager(); //開啟事務 fragmentTransaction = supportFragmentManager.beginTransaction(); //例項化碎片 f1 = new framg1(); //預設一個頁面 fragmentTransaction.add(R.id.fl, f1).commit(); //獲取點選事件 rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) { //隱藏所用的Frangment hideFrag(); //在獲取一個開啟事務 android.support.v4.app.FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction(); switch (i) { case R.id.rb: fragmentTransaction.show(f1).commit(); break; case R.id.rb1: //如果等於空 if (f2 == null) { f2 = new framg2();//例項化一個Frang2 fragmentTransaction.add(R.id.fl, f2).commit();//添加布局裡 } else { fragmentTransaction.show(f2).commit();//有的話就show } break; case R.id.rb2: if (f3 == null) { f3 = new framg3(); fragmentTransaction.add(R.id.fl, f3).commit(); } else { fragmentTransaction.show(f3).commit(); } break; } } }); } //隱藏所用的Frangment private void hideFrag() { //在獲取一個開啟事務 android.support.v4.app.FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction(); //不等於空或者是否新增 if (f1 != null && f1.isAdded()) { fragmentTransaction.hide(f1);//隱藏 } //不等於空或者是否新增 if (f2 != null && f2.isAdded()) { fragmentTransaction.hide(f2);//隱藏 } if (f3 != null && f3.isAdded()) { fragmentTransaction.hide(f3); } fragmentTransaction.commit(); } }

  1. //在第一個Fragment中新增Viewpager,載入多個Fragment
package l.framgsradiogroupviewpager.Framgs;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import l.framgsradiogroupviewpager.R;

/**
 * Created by lenovo on 2017/11/9.
 */
public class framg1 extends Fragment {

    private ViewPager vp;
    List<Fragment> fragmentlist=new ArrayList<>();

    //只寫載入佈局和找到控制元件
@Nullable
    @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View inflate = inflater.inflate(R.layout.f1, container, false);
        return inflate;
    }
    /**
     * 寫fragment的邏輯;
     * @param view
* @param savedInstanceState
*/
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
         //集合資料
getlist();

        vp = (ViewPager)view.findViewById(R.id.vp);
         //fragment中巢狀子fragment要用getChildFragmentManager()
Myadapter my=new Myadapter(getChildFragmentManager());

        vp.setAdapter(my);

    }
    private void getlist() {
        charfrang1 c1=new charfrang1();
        charfrang2 c2=new charfrang2();

        fragmentlist.add(c1);
        fragmentlist.add(c2);

    }
    class Myadapter extends FragmentPagerAdapter{


        public Myadapter(FragmentManager fm) {
            super(fm);
        }

        @Override
public Fragment getItem(int position) {
            return fragmentlist.get(position);
        }

        @Override
public int getCount() {
            return fragmentlist.size();
        }
    }
}