使用intent从另一个片段调用片段

时间:2014-09-20 05:35:07

标签: java android android-intent android-fragments

我正在使用一个viewpager控件来托管三个片段。我在每个片段中放了一个按钮来转到下一个片段。所以我想知道如何使用intent从一个片段转到另一个片段?

主要目的是创建一个已分为三个片段的注册表单。代码如下所示:

主要活动:

package pl.looksok.viewpagerdemo;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class MainActivity extends FragmentActivity {
    ViewPager pager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MyPagerAdapter pageAdapter = new MyPagerAdapter(getSupportFragmentManager());
         pager = (ViewPager)findViewById(R.id.myViewPager);
        pager.setAdapter(pageAdapter);

    }

}

第一个片段:

package pl.looksok.viewpagerdemo;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.support.v4.app.Fragment;

public class FragmentBlue extends Fragment {
Button btnnext1;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_blue, container, false);
        btnnext1=(Button) view.findViewById(R.id.btnnext1);

        btnnext1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                 Intent intent = new Intent(view.getContext(), FragmentGreen.class);
                    view.getContext().startActivity(intent);
                    getActivity().finish();

            }
        });

        return view;
    }
}

第二个片段:

package pl.looksok.viewpagerdemo;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.support.v4.app.Fragment;

public class FragmentGreen extends Fragment {
Button btnnext2;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_green, container, false);
        btnnext2=(Button) view.findViewById(R.id.btnnext2);
        btnnext2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                Intent intent = new Intent(view.getContext(), FragmentPink.class);
                view.getContext().startActivity(intent);
                getActivity().finish();

            }
        });
        return view;
    }
}

第三个片段:

package pl.looksok.viewpagerdemo;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.support.v4.app.Fragment;

public class FragmentPink extends Fragment {
Button btnnext3;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_pink, container, false);
        btnnext3=(Button) view.findViewById(R.id.btnnext3);
        btnnext3.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {


            }
        });
        return view;
    }
}

viewpager的适配器:

package pl.looksok.viewpagerdemo;

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

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class MyPagerAdapter extends FragmentPagerAdapter {

    private List<Fragment> fragments;

    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
        this.fragments = new ArrayList<Fragment>();
        fragments.add(new FragmentBlue());
        fragments.add(new FragmentGreen());
        fragments.add(new FragmentPink());
    }

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

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

viewpager的自定义类:

package pl.looksok.viewpagerdemo;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

public class CustomViewPager extends ViewPager {

    private boolean isPagingEnabled = false;

    public CustomViewPager(Context context) {
        super(context);
    }

    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return this.isPagingEnabled && super.onTouchEvent(event);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        return this.isPagingEnabled && super.onInterceptTouchEvent(event);
    }

    public void setPagingEnabled(boolean b) {
        this.isPagingEnabled = b;
    }
}

我现在使用的代码在点击第一个按钮时显示以下错误:

09-20 10:54:18.632: E/AndroidRuntime(9401): FATAL EXCEPTION: main
09-20 10:54:18.632: E/AndroidRuntime(9401): android.content.ActivityNotFoundException: Unable to find explicit activity class {pl.looksok.viewpagerdemo/pl.looksok.viewpagerdemo.FragmentGreen}; have you declared this activity in your AndroidManifest.xml?
09-20 10:54:18.632: E/AndroidRuntime(9401):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
09-20 10:54:18.632: E/AndroidRuntime(9401):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
09-20 10:54:18.632: E/AndroidRuntime(9401):     at android.app.Activity.startActivityForResult(Activity.java:2827)
09-20 10:54:18.632: E/AndroidRuntime(9401):     at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:824)
09-20 10:54:18.632: E/AndroidRuntime(9401):     at android.app.Activity.startActivity(Activity.java:2933)
09-20 10:54:18.632: E/AndroidRuntime(9401):     at pl.looksok.viewpagerdemo.FragmentBlue$1.onClick(FragmentBlue.java:25)
09-20 10:54:18.632: E/AndroidRuntime(9401):     at android.view.View.performClick(View.java:2538)
09-20 10:54:18.632: E/AndroidRuntime(9401):     at android.view.View$PerformClick.run(View.java:9152)
09-20 10:54:18.632: E/AndroidRuntime(9401):     at android.os.Handler.handleCallback(Handler.java:587)
09-20 10:54:18.632: E/AndroidRuntime(9401):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-20 10:54:18.632: E/AndroidRuntime(9401):     at android.os.Looper.loop(Looper.java:130)
09-20 10:54:18.632: E/AndroidRuntime(9401):     at android.app.ActivityThread.main(ActivityThread.java:3689)
09-20 10:54:18.632: E/AndroidRuntime(9401):     at java.lang.reflect.Method.invokeNative(Native Method)
09-20 10:54:18.632: E/AndroidRuntime(9401):     at java.lang.reflect.Method.invoke(Method.java:507)
09-20 10:54:18.632: E/AndroidRuntime(9401):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
09-20 10:54:18.632: E/AndroidRuntime(9401):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
09-20 10:54:18.632: E/AndroidRuntime(9401):     at dalvik.system.NativeStart.main(Native Method)

所以问题是如何在片段之间使用意图。有没有更好的方法将注册表单分成三部分而不使用viewpager?

3 个答案:

答案 0 :(得分:13)

你不能像这样打电话给你的片段,

  Intent intent = new Intent(view.getContext(), FragmentGreen.class);
  view.getContext().startActivity(intent);
  getActivity().finish();

你需要像这样调用你的片段

    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    FragmentGreen llf = new FragmentGreen();
    ft.replace(R.id.listFragment, llf);
    ft.commit();

Intent主要用于从另一个调用一个活动。要添加新Fragment,您无法使用Intent。为此,您必须使用FragmentManager和开放片段FragmentTransaction

有关详细信息,请转到Here

答案 1 :(得分:5)

如果所有片段都在同一个ViewPager中,并且您只想导航到该片段,那么您不需要任何Intents。你需要的只是:

mViewPager.setCurrentItem(POSITION, true);

这里POSITION是一个整数。

现在,视图寻呼机位于Activity,但Button位于Fragment内,因此您需要在活动中创建此方法

public void selectFragment(int position){
mViewPager.setCurrentItem(position, true); 
// true is to animate the transaction
}

并在OnClickListener

中进行调用
((MainActivity)getActivity()).selectFragment(POSITION_YOU_WANNA_SELECT);

希望它有所帮助。

答案 2 :(得分:0)

公共类Fragment3扩展了Fragment {

Button button;
@Nullable
@Override
public View onCreateView(@NonNull final LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {
    final View view;
    Fragment fragment;
    view = inflater.inflate(R.layout.page_4, container, false);
    button=view.findViewById(R.id.exit);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FragmentManager fm = getFragmentManager();
            assert fm != null;
            FragmentTransaction ft = fm.beginTransaction();
            FragmentLogin llf = new FragmentLogin();
            ft.replace(R.id.FrameButton, llf);
            ft.commit();
        }


    });



    return view;


}

}