android中Fragment和Actvity相互跳转

本文详细介绍了如何在Android应用中使用ViewPager配合Fragment构建选项卡界面,并实现了Fragment与Activity之间的通信及跳转功能。通过在onActivityCreated方法中获取布局文件中的控件进行事件监听,以及通过Intent实现Fragment与Activity间的跳转。同时,展示了如何通过Activity间接跳转到特定的Fragment,提供了完整的示例代码。

   最近项目有用到ViewPager+Fragment来构建选项卡,在实现Fragment和Activity跳转当中花了很多时间,下面来介绍下相互之间的通信及跳转:

     1、Fragment跳转到Activity

      由于我使用的ViewPager+Fragment来做的切换选项卡操作,其实也就是将多个Fragment加载到一个Activity中,这样我们可以在

  onCreateView方法中获得布局文件中的控件,来进行事件监听,不过再获取中和Activity不同我们得通过getActivty来获得该Fragment的Acticity,其他和在Activity实现跳转操作一样,使用intent即可。
  
 public void onActivityCreated(Bundle savedInstanceState) {
        about=(RelativeLayout)getActivity().findViewById(R.id.me_about);
        login=(RelativeLayout)getActivity().findViewById(R.id.me_login);
        setting=(RelativeLayout)getActivity().findViewById(R.id.me_setting);
        qingjia=(RelativeLayout)getActivity().findViewById(R.id.me_qingjia);
        ynnUrl=(RelativeLayout)getActivity().findViewById(R.id.ynnu_url);
        gisUrl=(RelativeLayout)getActivity().findViewById(R.id.gis_url);

        login.setOnClickListener(new MyOclickListener(0));
        qingjia.setOnClickListener(new MyOclickListener(1));
        setting.setOnClickListener(new MyOclickListener(2));
        about.setOnClickListener(new MyOclickListener(3));
        ynnUrl.setOnClickListener(new MyOclickListener(4));
        gisUrl.setOnClickListener(new MyOclickListener(5));
        super.onActivityCreated(savedInstanceState);
    }
2、Actvity跳转到Fragment:
   Activity是不能直接跳转到Fragment的,我们只能通过Actvity跳转到加载Fragment的Activity中,通过传递相关的值到加载的Activity上,然后通过判断,跳转到相应的Fragment,具体实现如下:
About.java
package com.example.gis.gis;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;


public class AboutActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.about);
    }

    //监听顶部返回按钮
    public void back(View v)
    {   GisMainActivity.index=4;
        Intent intent=new Intent(AboutActivity.this,GisMainActivity.class);
        startActivity(intent);
        this.finish();
    }
}
对应界面:

GisMainActivity.java
package com.example.gis.gis;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;


public class GisMainActivity extends FragmentActivity implements View.OnClickListener {

    private ViewPager tabPager;
    public static int index=0;
    public static final int TAB_HOME = 0;
    public static final int TAB_GK = 1;
    public static final int TAB_KY = 2;
    public static final int TAB_Message = 3;
    public static final int TAB_Me = 4;

    private int curreIndex=0;
    private LinearLayout tab_home,tab_gk,tab_ky,tab_message,tab_me;
    private ImageView gisTab1,gisTab2,gisTab3,gisTab4,gisTab5;
    private TextView textTab1,textTab2,textTab3,textTab4,textTab5;
    private LayoutInflater inflater;
    HomeFragment homefragment = new HomeFragment();
    GkFragment gkFragment = new GkFragment();
    KyFragment kyFragment = new KyFragment();
    MessageFragment messageFragment = new MessageFragment();
    MeFragment meFragment = new MeFragment();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initView();
        addListener();
    }
        //绑定组件

    public void initView() {
        tabPager = (ViewPager) findViewById(R.id.tabpager);
//        tabPager.setOnPageChangeListener(new PageChangeListener());
        tab_home=(LinearLayout)findViewById(R.id.tab_home);
        tab_gk=(LinearLayout)findViewById(R.id.tab_gk);
        tab_ky=(LinearLayout)findViewById(R.id.tab_ky);
        tab_message=(LinearLayout)findViewById(R.id.tab_message);
        tab_me=(LinearLayout)findViewById(R.id.tab_me);

        gisTab1 = (ImageView) findViewById(R.id.home);
        gisTab2 = (ImageView) findViewById(R.id.gk);
        gisTab3 = (ImageView) findViewById(R.id.ky);
        gisTab4 = (ImageView) findViewById(R.id.message);
        gisTab5 = (ImageView) findViewById(R.id.me);
        gisTab1 = (ImageView) findViewById(R.id.home);
        gisTab2 = (ImageView) findViewById(R.id.gk);
        gisTab3 = (ImageView) findViewById(R.id.ky);
        gisTab4 = (ImageView) findViewById(R.id.message);
        gisTab5 = (ImageView) findViewById(R.id.me);
        tab_home.setOnClickListener(this);
        tab_gk.setOnClickListener(this);
        tab_ky.setOnClickListener(this);
        tab_message.setOnClickListener(this);
        tab_me.setOnClickListener(this);
        textTab1 = (TextView) findViewById(R.id.text_home);
        textTab2 = (TextView) findViewById(R.id.text_gk);
        textTab3 = (TextView) findViewById(R.id.text_ky);
        textTab4 = (TextView) findViewById(R.id.text_message);
        textTab5 = (TextView) findViewById(R.id.text_me);
        if(index==0) {
            gisTab1.setImageDrawable(getResources().getDrawable(R.mipmap.ic_home));
            textTab1.setTextColor(android.graphics.Color.RED);
        }
        else if(index==1){
            gisTab2.setImageDrawable(getResources().getDrawable(R.mipmap.ic_gk));
            textTab2.setTextColor(android.graphics.Color.RED);

        }else if(index==2){
            gisTab3.setImageDrawable(getResources().getDrawable(R.mipmap.ic_ky));
            textTab3.setTextColor(android.graphics.Color.RED);

        }else if(index==3){
            gisTab4.setImageDrawable(getResources().getDrawable(R.mipmap.ic_message));
            textTab4.setTextColor(android.graphics.Color.RED);
        }else{
            gisTab5.setImageDrawable(getResources().getDrawable(R.mipmap.ic_me));
            textTab5.setTextColor(android.graphics.Color.RED);
        }

        //获取四个tabview 并装入数组
//        inflater = LayoutInflater.from(this);
//        View view1 = inflater.inflate(R.layout.main_tab_home, null);
//        View view2 = inflater.inflate(R.layout.main_tab_gk, null);
//        View view3 = inflater.inflate(R.layout.main_tab_research, null);
//        View view4 = inflater.inflate(R.layout.main_tab_message, null);
//        View view5 = inflater.inflate(R.layout.main_tab_me, null);

        ArrayList<Fragment> fragments=new ArrayList<Fragment>();
        fragments.add(new HomeFragment());
        fragments.add(new GkFragment());
        fragments.add(new KyFragment());
        fragments.add(new MessageFragment());
        fragments.add(new MeFragment());
        tabPager.setAdapter(new FragmentAdapter(getSupportFragmentManager(),fragments));
        tabPager.setCurrentItem(index);
//        tabPager.setAdapter(adapter);
    }


    public void addListener() {



        tabPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {


            public void onPageSelected(int i) {
                switch (i) {
                    case 0:
                        gisTab1.setImageDrawable(getResources().getDrawable(R.mipmap.ic_home));
                        textTab1.setTextColor(android.graphics.Color.RED);
                        gisTab2.setImageDrawable(getResources().getDrawable(R.mipmap.ic_gk_normal));
                        textTab2.setTextColor(Color.BLACK);
                        gisTab3.setImageDrawable(getResources().getDrawable(R.mipmap.ic_ky_normal));
                        textTab3.setTextColor(Color.BLACK);
                        gisTab4.setImageDrawable(getResources().getDrawable(R.mipmap.ic_message_normal));
                        textTab4.setTextColor(Color.BLACK);
                        gisTab5.setImageDrawable(getResources().getDrawable(R.mipmap.ic_me_normal));
                        textTab5.setTextColor(Color.BLACK);

                        break;
                    case 1:
                        gisTab2.setImageDrawable(getResources().getDrawable(R.mipmap.ic_gk));
                        textTab2.setTextColor(android.graphics.Color.RED);
                        gisTab1.setImageDrawable(getResources().getDrawable(R.mipmap.ic_home_normal));
                        textTab1.setTextColor(Color.BLACK);
                        gisTab3.setImageDrawable(getResources().getDrawable(R.mipmap.ic_ky_normal));
                        textTab3.setTextColor(Color.BLACK);
                        gisTab4.setImageDrawable(getResources().getDrawable(R.mipmap.ic_message_normal));
                        textTab4.setTextColor(Color.BLACK);
                        gisTab5.setImageDrawable(getResources().getDrawable(R.mipmap.ic_me_normal));
                        textTab5.setTextColor(Color.BLACK);
                        break;
                    case 2:
                        gisTab3.setImageDrawable(getResources().getDrawable(R.mipmap.ic_ky));
                        textTab3.setTextColor(android.graphics.Color.RED);
                        gisTab2.setImageDrawable(getResources().getDrawable(R.mipmap.ic_gk_normal));
                        textTab2.setTextColor(Color.BLACK);
                        gisTab1.setImageDrawable(getResources().getDrawable(R.mipmap.ic_home_normal));
                        textTab1.setTextColor(Color.BLACK);
                        gisTab4.setImageDrawable(getResources().getDrawable(R.mipmap.ic_message_normal));
                        textTab4.setTextColor(Color.BLACK);
                        gisTab5.setImageDrawable(getResources().getDrawable(R.mipmap.ic_me_normal));
                        textTab5.setTextColor(Color.BLACK);
                        break;
                    case 3:
                        gisTab4.setImageDrawable(getResources().getDrawable(R.mipmap.ic_message));
                        textTab4.setTextColor(android.graphics.Color.RED);
                        gisTab2.setImageDrawable(getResources().getDrawable(R.mipmap.ic_gk_normal));
                        textTab2.setTextColor(Color.BLACK);
                        gisTab1.setImageDrawable(getResources().getDrawable(R.mipmap.ic_home_normal));
                        textTab1.setTextColor(Color.BLACK);
                        gisTab3.setImageDrawable(getResources().getDrawable(R.mipmap.ic_ky_normal));
                        textTab3.setTextColor(Color.BLACK);
                        gisTab5.setImageDrawable(getResources().getDrawable(R.mipmap.ic_me_normal));
                        textTab5.setTextColor(Color.BLACK);
                        break;
                    case 4:
                        gisTab5.setImageDrawable(getResources().getDrawable(R.mipmap.ic_me));
                        textTab5.setTextColor(android.graphics.Color.RED);
                        gisTab2.setImageDrawable(getResources().getDrawable(R.mipmap.ic_gk_normal));
                        textTab2.setTextColor(Color.BLACK);
                        gisTab1.setImageDrawable(getResources().getDrawable(R.mipmap.ic_home_normal));
                        textTab1.setTextColor(Color.BLACK);
                        gisTab3.setImageDrawable(getResources().getDrawable(R.mipmap.ic_ky_normal));
                        textTab3.setTextColor(Color.BLACK);
                        gisTab4.setImageDrawable(getResources().getDrawable(R.mipmap.ic_message_normal));
                        textTab4.setTextColor(Color.BLACK);
                        break;


                }
            }


            public void onPageScrolled(int i, float v, int i1) {

            }


            public void onPageScrollStateChanged(int i) {

            }


        });}


    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tab_home:
                tabPager.setCurrentItem(TAB_HOME);
                break;
            case R.id.tab_gk:
                tabPager.setCurrentItem(TAB_GK);
                break;
            case R.id.tab_ky:
                tabPager.setCurrentItem(TAB_KY);
                break;
            case R.id.tab_message:
                tabPager.setCurrentItem(TAB_Message);
                break;
            case R.id.tab_me:
                tabPager.setCurrentItem(TAB_Me);
                break;

            default:
                break;
        }
    }

}

相关界面:




  


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值