关闭

Fragment跳转之Fragment与Activity通信

标签: Fragment跳转Fragment与Activity通信
189人阅读 评论(0) 收藏 举报
分类:
<span style="font-family:Courier New;font-size:10px;">为了解耦,统一让activity去管理fragment间的跳转,fragment只需要提供回调的接口即可</span>
<span style="font-family:Courier New;font-size:10px;">FragmentOne的页面,让其与activity进行绑定</span>
<span style="font-family:Courier New;font-size:10px;">public class FragmentOne extends Fragment implements View.OnClickListener {

    private Button mBtn1;
    private FragmentOneBtnListener mFragmentOneBtnListener;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_one,null);
        mBtn1 = (Button) view.findViewById(R.id.btn1);
        mBtn1.setOnClickListener(this);
        return view;
    }

    @Override
    public void onClick(View v) {
        if (getActivity() instanceof FragmentOneBtnListener) {
           ((FragmentOneBtnListener) getActivity()).FragmentOneBtn();
        }
    }

    public interface FragmentOneBtnListener{
        void FragmentOneBtn();
    }
}</span>
<span style="font-family:Courier New;font-size:10px;"></span>
</pre><pre name="code" class="java"><pre style="background-color: rgb(199, 237, 204);"><pre name="code" class="java">FragmentTwo页面
public class FragmentTwo extends Fragment implements View.OnClickListener {
    private Button mBtn2;
    private FragmentTwoBtnListener mFragmentTwoBtnListener;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_two,null);

        mBtn2 = (Button) view.findViewById(R.id.btn2);
        mBtn2.setOnClickListener(this);
        return view;
    }

    @Override
    public void onClick(View v) {
        if (getActivity() instanceof FragmentTwoBtnListener) {
            mFragmentTwoBtnListener.FragmentTwoBtn();
        }
    }

    public interface FragmentTwoBtnListener {
        void FragmentTwoBtn();
    }

    public void setFragmentTwoBtnListener(FragmentTwoBtnListener listener) {
        mFragmentTwoBtnListener = listener;
    }
}
FragmentThree页面
public class FragmentThree extends Fragment{


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_three,null);

        return view;
    }


}
MainActivity页面

public class MainActivity extends AppCompatActivity implements FragmentOne.FragmentOneBtnListener,FragmentTwo.FragmentTwoBtnListener{

    FragmentOne mFragmentOne;
    FragmentTwo mFragmentTwo;
    FragmentThree mFragmentThree;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
	//添加第一个页面
	if (savedInstanceState == null) {
	    mFragmentOne = new FragmentOne();
	    getSupportFragmentManager().beginTransaction().add(R.id.id_content,mFragmentOne).commit();
	}
    }


    @Override
    public void FragmentOneBtn() {
	if (mFragmentTwo == null) {
	    mFragmentTwo = new FragmentTwo();
	    mFragmentTwo.setFragmentTwoBtnListener(this);
	}
	FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
	fragmentTransaction.hide(mFragmentOne);
	fragmentTransaction.add(R.id.id_content,mFragmentTwo);
	fragmentTransaction.addToBackStack(null);
	fragmentTransaction.commit();

    }

    @Override
    public void FragmentTwoBtn() {
	if (mFragmentThree == null) {
	    mFragmentThree = new FragmentThree();
	}
	FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
	fragmentTransaction.hide(mFragmentTwo);
	fragmentTransaction.add(R.id.id_content,mFragmentThree);
	fragmentTransaction.addToBackStack(null);
	fragmentTransaction.commit();
    }
}






   
1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:5918次
    • 积分:254
    • 等级:
    • 排名:千里之外
    • 原创:18篇
    • 转载:1篇
    • 译文:1篇
    • 评论:0条
    文章分类